1 | <?php |
||
27 | abstract class EloquentRepository implements ReadRepository, WriteRepository, PageRepository |
||
28 | { |
||
29 | /** @var Model */ |
||
30 | protected static $instance; |
||
31 | /** @var EloquentReadRepository */ |
||
32 | protected $readRepository; |
||
33 | /** @var EloquentWriteRepository */ |
||
34 | protected $writeRepository; |
||
35 | /** @var EloquentPageRepository */ |
||
36 | protected $pageRepository; |
||
37 | |||
38 | /** |
||
39 | * EloquentRepository constructor. |
||
40 | */ |
||
41 | public function __construct() |
||
49 | |||
50 | /** |
||
51 | * Retrieves an entity by its id. |
||
52 | * |
||
53 | * @param Identity|Model $id |
||
54 | * @param Fields|null $fields |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public function find(Identity $id, Fields $fields = null) |
||
62 | |||
63 | /** |
||
64 | * Returns all instances of the type. |
||
65 | * |
||
66 | * @param Filter|null $filter |
||
67 | * @param Sort|null $sort |
||
68 | * @param Fields|null $fields |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null) |
||
76 | |||
77 | /** |
||
78 | * Returns an EloquentMongoDB Model instance. |
||
79 | * |
||
80 | * @return Model |
||
81 | */ |
||
82 | protected function getModelInstance() |
||
91 | |||
92 | /** |
||
93 | * Must return the EloquentMongoDB Model Fully Qualified Class Name as a string. |
||
94 | * |
||
95 | * eg: return App\Model\User::class |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | abstract protected function modelClassName(); |
||
100 | |||
101 | /** |
||
102 | * Returns whether an entity with the given id exists. |
||
103 | * |
||
104 | * @param Identity|Model $id |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function exists(Identity $id) |
||
112 | |||
113 | /** |
||
114 | * Returns the total amount of elements in the repository given the restrictions provided by the Filter object. |
||
115 | * |
||
116 | * @param Filter|null $filter |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | public function count(Filter $filter = null) |
||
124 | |||
125 | /** |
||
126 | * Adds a new entity to the storage. |
||
127 | * |
||
128 | * @param Identity|Model $value |
||
129 | * |
||
130 | * @return mixed |
||
131 | */ |
||
132 | public function add(Identity $value) |
||
136 | |||
137 | /** |
||
138 | * Adds a collections of entities to the storage. |
||
139 | * |
||
140 | * @param Model[] $values |
||
141 | * |
||
142 | * @return mixed |
||
143 | * |
||
144 | * @throws \Exception |
||
145 | */ |
||
146 | public function addAll(array $values) |
||
150 | |||
151 | /** |
||
152 | * Removes the entity with the given id. |
||
153 | * |
||
154 | * @param Identity|Model $id |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function remove(Identity $id) |
||
162 | |||
163 | /** |
||
164 | * Removes all elements in the repository given the restrictions provided by the Filter object. |
||
165 | * If $filter is null, all the repository data will be deleted. |
||
166 | * |
||
167 | * @param Filter $filter |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function removeAll(Filter $filter = null) |
||
175 | |||
176 | /** |
||
177 | * Returns a Page of entities meeting the paging restriction provided in the Pageable object. |
||
178 | * |
||
179 | * @param Pageable $pageable |
||
180 | * |
||
181 | * @return Page |
||
182 | */ |
||
183 | public function findAll(Pageable $pageable = null) |
||
187 | |||
188 | /** |
||
189 | * Returns all instances of the type meeting $distinctFields values. |
||
190 | * |
||
191 | * @param Fields $distinctFields |
||
192 | * @param Filter|null $filter |
||
193 | * @param Sort|null $sort |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null) |
||
201 | |||
202 | /** |
||
203 | * Repository data is added or removed as a whole block. |
||
204 | * Must work or fail and rollback any persisted/erased data. |
||
205 | * |
||
206 | * @param callable $transaction |
||
207 | * |
||
208 | * @throws \Exception |
||
209 | */ |
||
210 | public function transactional(callable $transaction) |
||
214 | } |
||
215 |