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