1 | <?php |
||
30 | abstract class AbstractRelation extends AbstractRelational |
||
31 | { |
||
32 | use GetTypeTrait; |
||
33 | |||
34 | /** |
||
35 | * @var StorageInterface |
||
36 | */ |
||
37 | protected $storage; |
||
38 | |||
39 | /** |
||
40 | * @var ModelBag |
||
41 | */ |
||
42 | protected $models; |
||
43 | |||
44 | /** |
||
45 | * @var AccessorInterface |
||
46 | */ |
||
47 | protected $accessor; |
||
48 | |||
49 | /** |
||
50 | * @var DefinitionInterface |
||
51 | */ |
||
52 | protected $definition; |
||
53 | |||
54 | protected $conditions = []; |
||
55 | protected $orders = []; |
||
56 | protected $limit; |
||
57 | protected $offset; |
||
58 | |||
59 | /** |
||
60 | * Constructor |
||
61 | * |
||
62 | * @param StorageInterface $storage |
||
63 | * @param DefinitionInterface $relation |
||
64 | * @param ModelBag $models |
||
65 | * @param RelationFactoryInterface $factory |
||
66 | */ |
||
67 | public function __construct(StorageInterface $storage, DefinitionInterface $relation, ModelBag $models, RelationFactoryInterface $factory) |
||
75 | |||
76 | /** |
||
77 | * Returns relation name |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function name() |
||
85 | |||
86 | /** |
||
87 | * Returns model |
||
88 | * |
||
89 | * @return ModelInterface |
||
90 | */ |
||
91 | public function model() |
||
95 | |||
96 | /** |
||
97 | * Returns relation query instance |
||
98 | * |
||
99 | * @return QueryBuilder |
||
100 | */ |
||
101 | public function query() |
||
105 | |||
106 | /** |
||
107 | * Adds where condition to relation |
||
108 | * |
||
109 | * @param mixed $field |
||
110 | * @param mixed $value |
||
111 | * @param string $comparison |
||
112 | * @param string $logical |
||
113 | * |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function where($field, $value, $comparison = '==', $logical = 'and') |
||
122 | |||
123 | /** |
||
124 | * Adds sorting to relation |
||
125 | * |
||
126 | * @param string $field |
||
127 | * @param string|array $order |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function order($field, $order = 'desc') |
||
137 | |||
138 | /** |
||
139 | * Sets limits to relation |
||
140 | * |
||
141 | * @param int $limit |
||
142 | * @param null|int $offset |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function limit($limit, $offset = null) |
||
153 | |||
154 | /** |
||
155 | * Builds local key from field property pairs |
||
156 | * |
||
157 | * @param mixed $entity |
||
158 | * @param array $pairs |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | protected function buildLocalKey($entity, array $pairs) |
||
168 | |||
169 | /** |
||
170 | * Builds foreign key from field property pairs |
||
171 | * |
||
172 | * @param mixed $entity |
||
173 | * @param array $pairs |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | protected function buildForeignKey($entity, array $pairs) |
||
181 | |||
182 | /** |
||
183 | * Builds key from key-value pairs |
||
184 | * |
||
185 | * @param mixed $entity |
||
186 | * @param array $pairs |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | protected function buildKey($entity, array $pairs) |
||
199 | |||
200 | /** |
||
201 | * Fetches collection of entities matching set conditions |
||
202 | * Optionally sorts it and limits it |
||
203 | * |
||
204 | * @param string $entityName |
||
205 | * @param array $conditions |
||
206 | * @param bool $result |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | protected function fetch($entityName, array $conditions, $result = false) |
||
240 | |||
241 | /** |
||
242 | * Throws exception when entity is not required instance |
||
243 | * |
||
244 | * @param mixed $entity |
||
245 | * |
||
246 | * @return bool |
||
247 | * @throws RelationException |
||
248 | */ |
||
249 | protected function assertInstance($entity) |
||
258 | |||
259 | /** |
||
260 | * Removes obsolete entities that match conditions but don't exist in collection |
||
261 | * |
||
262 | * @param string $entityName |
||
263 | * @param array $collection |
||
264 | * @param array $conditions |
||
265 | */ |
||
266 | protected function cleanup($entityName, array $collection, array $conditions) |
||
287 | |||
288 | /** |
||
289 | * Returns array with entities that should be deleted or false otherwise |
||
290 | * |
||
291 | * @param string $entityName |
||
292 | * @param array $conditions |
||
293 | * |
||
294 | * @return array|bool |
||
295 | */ |
||
296 | private function isCleanupNecessary($entityName, $conditions) |
||
310 | |||
311 | /** |
||
312 | * Returns entity identifier |
||
313 | * If more than one primary keys, entity will not be identified |
||
314 | * |
||
315 | * @param object $instance |
||
316 | * @param string $entityName |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | protected function identifyEntity($instance, $entityName) |
||
331 | |||
332 | /** |
||
333 | * Checks if container has array access |
||
334 | * |
||
335 | * @param $container |
||
336 | * |
||
337 | * @throws RelationException |
||
338 | */ |
||
339 | protected function assertArrayAccess($container) |
||
345 | } |
||
346 |