1 | <?php |
||
26 | trait RelationshipTrait |
||
27 | { |
||
28 | use MutatedTrait; |
||
29 | |||
30 | /** |
||
31 | * @var Collection|null |
||
32 | */ |
||
33 | protected $relations; |
||
34 | |||
35 | /** |
||
36 | * @param null $object |
||
37 | * @return int|null |
||
38 | */ |
||
39 | abstract protected function findKey($object = null); |
||
40 | |||
41 | /** |
||
42 | * @param array $criteria |
||
43 | * @return QueryInterface |
||
44 | */ |
||
45 | abstract protected function query(array $criteria = []): QueryInterface; |
||
46 | |||
47 | /** |
||
48 | * @param $object |
||
49 | * @return ActiveRecord |
||
50 | */ |
||
51 | abstract protected function create($object): ActiveRecord; |
||
52 | |||
53 | /** |
||
54 | * @return ActiveRecord[][] |
||
55 | */ |
||
56 | abstract protected function associationDelta(): array; |
||
57 | |||
58 | /** |
||
59 | * @return void |
||
60 | */ |
||
61 | abstract protected function handleAssociationError(); |
||
62 | |||
63 | /** |
||
64 | * @param ActiveRecord|ElementInterface|int|string $object |
||
65 | * @return ActiveRecord |
||
66 | */ |
||
67 | public function findOrCreate($object): ActiveRecord |
||
75 | |||
76 | /** |
||
77 | * @param ActiveRecord|ElementInterface|int|string $object |
||
78 | * @return ActiveRecord |
||
79 | * @throws Exception |
||
80 | */ |
||
81 | public function findOrFail($object): ActiveRecord |
||
89 | |||
90 | /** |
||
91 | * @param ActiveRecord|ElementInterface|int|string|null $object |
||
92 | * @return ActiveRecord|null |
||
93 | */ |
||
94 | public function findOne($object = null) |
||
102 | |||
103 | /** |
||
104 | * @param ActiveRecord|ElementInterface|int|string $object |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function exists($object): bool |
||
111 | |||
112 | |||
113 | /************************************************************ |
||
114 | * COLLECTIONS |
||
115 | ************************************************************/ |
||
116 | |||
117 | /** |
||
118 | * @inheritDoc |
||
119 | */ |
||
120 | public function getRelationships(): Collection |
||
130 | |||
131 | |||
132 | /************************************************************ |
||
133 | * ADD / REMOVE |
||
134 | ************************************************************/ |
||
135 | |||
136 | /** |
||
137 | * Add one or many object relations (but do not save) |
||
138 | * |
||
139 | * @param $objects |
||
140 | * @param array $attributes |
||
141 | * @return RelationshipInterface |
||
142 | */ |
||
143 | public function add($objects, array $attributes = []): RelationshipInterface |
||
163 | |||
164 | /** |
||
165 | * @param $objects |
||
166 | * @return RelationshipInterface |
||
167 | */ |
||
168 | public function remove($objects): RelationshipInterface |
||
178 | |||
179 | |||
180 | /************************************************************ |
||
181 | * RESET |
||
182 | ************************************************************/ |
||
183 | |||
184 | /** |
||
185 | * Reset associations |
||
186 | * @return RelationshipInterface |
||
187 | */ |
||
188 | public function reset(): RelationshipInterface |
||
194 | |||
195 | /** |
||
196 | * Reset associations |
||
197 | * @return RelationshipInterface |
||
198 | */ |
||
199 | public function clear(): RelationshipInterface |
||
204 | |||
205 | |||
206 | /******************************************* |
||
207 | * SAVE |
||
208 | *******************************************/ |
||
209 | |||
210 | /** |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function save(): bool |
||
246 | |||
247 | |||
248 | /******************************************* |
||
249 | * CACHE |
||
250 | *******************************************/ |
||
251 | |||
252 | /** |
||
253 | * @param array $associations |
||
254 | * @param bool $mutated |
||
255 | * @return static |
||
256 | */ |
||
257 | protected function newRelations(array $associations, bool $mutated = true): self |
||
264 | |||
265 | /** |
||
266 | * @param $association |
||
267 | * @return RelationshipTrait |
||
|
|||
268 | */ |
||
269 | protected function addToRelations($association): self |
||
280 | |||
281 | /** |
||
282 | * @param int $key |
||
283 | * @return RelationshipTrait |
||
284 | */ |
||
285 | protected function removeFromRelations(int $key): self |
||
292 | |||
293 | |||
294 | /******************************************* |
||
295 | * UTILITIES |
||
296 | *******************************************/ |
||
297 | |||
298 | /** |
||
299 | * Ensure we're working with an array of objects, not configs, etc |
||
300 | * |
||
301 | * @param array|QueryInterface|Collection|ElementInterface|UserAssociation $objects |
||
302 | * @return array |
||
303 | */ |
||
304 | protected function objectArray($objects): array |
||
317 | } |
||
318 |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.