@@ -28,18 +28,26 @@ public function __construct(
2828 }
2929
3030 /** @param T $object */
31- public function save (object $ object ): void
31+ public function persist (object $ object ): void
3232 {
33+ if ($ object ::class !== $ this ->metadata ->className ) {
34+ throw new WrongClass ($ this ->metadata ->className , $ object ::class);
35+ }
36+
3337 $ data = $ this ->hydrator ->extract ($ object );
3438
3539 $ this ->collection ()->insertOne ($ data );
3640 }
3741
38- /** @return T */
39- public function load (string $ id ): object
42+ /** @return T|null */
43+ public function find (string $ id ): object | null
4044 {
4145 $ data = $ this ->collection ()->findOne (['_id ' => $ id ]);
4246
47+ if ($ data === null ) {
48+ return null ;
49+ }
50+
4351 return $ this ->hydrator ->hydrate ($ this ->metadata ->className , $ data );
4452 }
4553
@@ -48,20 +56,57 @@ public function remove(string $id): void
4856 $ this ->collection ()->deleteOne (['_id ' => $ id ]);
4957 }
5058
51- /**
52- * @param array<string, mixed> $filter
53- *
54- * @return iterable<T>
55- */
56- public function find (array $ filter = []): iterable
59+ public function findAll (): iterable
5760 {
58- $ cursor = $ this ->collection ()->find ($ filter );
61+ $ cursor = $ this ->collection ()->find ();
5962
6063 foreach ($ cursor as $ document ) {
6164 yield $ this ->hydrator ->hydrate ($ this ->metadata ->className , $ document );
6265 }
6366 }
6467
68+ public function findBy (array $ filter , array |null $ orderBy = null , int |null $ limit = null , int |null $ offset = null ): iterable
69+ {
70+ $ options = [];
71+
72+ if ($ limit !== null ) {
73+ $ options ['limit ' ] = $ limit ;
74+ }
75+
76+ if ($ offset !== null ) {
77+ $ options ['skip ' ] = $ offset ;
78+ }
79+
80+ if ($ orderBy !== null ) {
81+ $ options ['sort ' ] = array_map (
82+ static fn ($ direction ) => $ direction === 'desc ' ? -1 : 1 ,
83+ $ orderBy ,
84+ );
85+ }
86+
87+ $ cursor = $ this ->collection ()->find ($ filter , $ options );
88+
89+ foreach ($ cursor as $ document ) {
90+ yield $ this ->hydrator ->hydrate ($ this ->metadata ->className , $ document );
91+ }
92+ }
93+
94+ public function findOneBy (array $ filter = [], array |null $ orderBy = null ): object |null
95+ {
96+ $ options = ['limit ' => 1 ];
97+
98+ if ($ orderBy !== null ) {
99+ $ options ['sort ' ] = array_map (
100+ static fn ($ direction ) => $ direction === 'desc ' ? -1 : 1 ,
101+ $ orderBy ,
102+ );
103+ }
104+
105+ $ data = $ this ->collection ()->findOne ($ filter , $ options );
106+
107+ return $ data ? $ this ->hydrator ->hydrate ($ this ->metadata ->className , $ data ) : null ;
108+ }
109+
65110 public function count (): int
66111 {
67112 return $ this ->collection ()->countDocuments ();
@@ -77,9 +122,7 @@ public function database(): Database
77122 return $ this ->database ;
78123 }
79124
80- /**
81- * @return Collection<array<string, mixed>>
82- */
125+ /** @return Collection<array<string, mixed>> */
83126 public function collection (): Collection
84127 {
85128 return $ this ->database ->getCollection ($ this ->metadata ->collection );
0 commit comments