-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAssociationCollection.php
383 lines (346 loc) · 11.6 KB
/
AssociationCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM;
use ArrayIterator;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\Locator\LocatorInterface;
use InvalidArgumentException;
use IteratorAggregate;
use Traversable;
/**
* A container/collection for association classes.
*
* Contains methods for managing associations, and
* ordering operations around saving and deleting.
*/
class AssociationCollection implements IteratorAggregate
{
use AssociationsNormalizerTrait;
use LocatorAwareTrait;
/**
* Stored associations
*
* @var \Cake\ORM\Association[]
*/
protected $_items = [];
/**
* Constructor.
*
* Sets the default table locator for associations.
* If no locator is provided, the global one will be used.
*
* @param \Cake\ORM\Locator\LocatorInterface|null $tableLocator Table locator instance.
*/
public function __construct(?LocatorInterface $tableLocator = null)
{
if ($tableLocator !== null) {
$this->_tableLocator = $tableLocator;
}
}
/**
* Add an association to the collection
*
* If the alias added contains a `.` the part preceding the `.` will be dropped.
* This makes using plugins simpler as the Plugin.Class syntax is frequently used.
*
* @param string $alias The association alias
* @param \Cake\ORM\Association $association The association to add.
* @return \Cake\ORM\Association The association object being added.
*/
public function add(string $alias, Association $association): Association
{
[, $alias] = pluginSplit($alias);
return $this->_items[$alias] = $association;
}
/**
* Creates and adds the Association object to this collection.
*
* @param string $className The name of association class.
* @param string $associated The alias for the target table.
* @param array $options List of options to configure the association definition.
* @return \Cake\ORM\Association
* @throws \InvalidArgumentException
*/
public function load(string $className, string $associated, array $options = []): Association
{
$options += [
'tableLocator' => $this->getTableLocator(),
];
$association = new $className($associated, $options);
if (!$association instanceof Association) {
$message = sprintf(
'The association must extend `%s` class, `%s` given.',
Association::class,
get_class($association)
);
throw new InvalidArgumentException($message);
}
return $this->add($association->getName(), $association);
}
/**
* Fetch an attached association by name.
*
* @param string $alias The association alias to get.
* @return \Cake\ORM\Association|null Either the association or null.
*/
public function get(string $alias): ?Association
{
if (isset($this->_items[$alias])) {
return $this->_items[$alias];
}
return null;
}
/**
* Fetch an association by property name.
*
* @param string $prop The property to find an association by.
* @return \Cake\ORM\Association|null Either the association or null.
*/
public function getByProperty(string $prop): ?Association
{
foreach ($this->_items as $assoc) {
if ($assoc->getProperty() === $prop) {
return $assoc;
}
}
return null;
}
/**
* Check for an attached association by name.
*
* @param string $alias The association alias to get.
* @return bool Whether or not the association exists.
*/
public function has(string $alias): bool
{
return isset($this->_items[$alias]);
}
/**
* Get the names of all the associations in the collection.
*
* @return string[]
*/
public function keys(): array
{
return array_keys($this->_items);
}
/**
* Get an array of associations matching a specific type.
*
* @param string|array $class The type of associations you want.
* For example 'BelongsTo' or array like ['BelongsTo', 'HasOne']
* @return \Cake\ORM\Association[] An array of Association objects.
* @since 3.5.3
*/
public function getByType($class): array
{
$class = array_map('strtolower', (array)$class);
$out = array_filter($this->_items, function ($assoc) use ($class) {
[, $name] = namespaceSplit(get_class($assoc));
return in_array(strtolower($name), $class, true);
});
return array_values($out);
}
/**
* Drop/remove an association.
*
* Once removed the association will not longer be reachable
*
* @param string $alias The alias name.
* @return void
*/
public function remove(string $alias): void
{
unset($this->_items[$alias]);
}
/**
* Remove all registered associations.
*
* Once removed associations will not longer be reachable
*
* @return void
*/
public function removeAll(): void
{
foreach ($this->_items as $alias => $object) {
$this->remove($alias);
}
}
/**
* Save all the associations that are parents of the given entity.
*
* Parent associations include any association where the given table
* is the owning side.
*
* @param \Cake\ORM\Table $table The table entity is for.
* @param \Cake\Datasource\EntityInterface $entity The entity to save associated data for.
* @param array $associations The list of associations to save parents from.
* associations not in this list will not be saved.
* @param array $options The options for the save operation.
* @return bool Success
*/
public function saveParents(Table $table, EntityInterface $entity, array $associations, array $options = []): bool
{
if (empty($associations)) {
return true;
}
return $this->_saveAssociations($table, $entity, $associations, $options, false);
}
/**
* Save all the associations that are children of the given entity.
*
* Child associations include any association where the given table
* is not the owning side.
*
* @param \Cake\ORM\Table $table The table entity is for.
* @param \Cake\Datasource\EntityInterface $entity The entity to save associated data for.
* @param array $associations The list of associations to save children from.
* associations not in this list will not be saved.
* @param array $options The options for the save operation.
* @return bool Success
*/
public function saveChildren(Table $table, EntityInterface $entity, array $associations, array $options): bool
{
if (empty($associations)) {
return true;
}
return $this->_saveAssociations($table, $entity, $associations, $options, true);
}
/**
* Helper method for saving an association's data.
*
* @param \Cake\ORM\Table $table The table the save is currently operating on
* @param \Cake\Datasource\EntityInterface $entity The entity to save
* @param array $associations Array of associations to save.
* @param array $options Original options
* @param bool $owningSide Compared with association classes'
* isOwningSide method.
* @return bool Success
* @throws \InvalidArgumentException When an unknown alias is used.
*/
protected function _saveAssociations(
Table $table,
EntityInterface $entity,
array $associations,
array $options,
bool $owningSide
): bool {
unset($options['associated']);
foreach ($associations as $alias => $nested) {
if (is_int($alias)) {
$alias = $nested;
$nested = [];
}
$relation = $this->get($alias);
if (!$relation) {
$msg = sprintf(
'Cannot save %s, it is not associated to %s',
$alias,
$table->getAlias()
);
throw new InvalidArgumentException($msg);
}
if ($relation->isOwningSide($table) !== $owningSide) {
continue;
}
if (!$this->_save($relation, $entity, $nested, $options)) {
return false;
}
}
return true;
}
/**
* Helper method for saving an association's data.
*
* @param \Cake\ORM\Association $association The association object to save with.
* @param \Cake\Datasource\EntityInterface $entity The entity to save
* @param array $nested Options for deeper associations
* @param array $options Original options
* @return bool Success
*/
protected function _save(
Association $association,
EntityInterface $entity,
array $nested,
array $options
): bool {
if (!$entity->isDirty($association->getProperty())) {
return true;
}
if (!empty($nested)) {
$options = $nested + $options;
}
return (bool)$association->saveAssociated($entity, $options);
}
/**
* Cascade a delete across the various associations.
* Cascade first across associations for which cascadeCallbacks is true.
*
* @param \Cake\Datasource\EntityInterface $entity The entity to delete associations for.
* @param array $options The options used in the delete operation.
* @return bool
*/
public function cascadeDelete(EntityInterface $entity, array $options): bool
{
$noCascade = [];
foreach ($this->_items as $assoc) {
if (!$assoc->getCascadeCallbacks()) {
$noCascade[] = $assoc;
continue;
}
$success = $assoc->cascadeDelete($entity, $options);
if (!$success) {
return false;
}
}
foreach ($noCascade as $assoc) {
$success = $assoc->cascadeDelete($entity, $options);
if (!$success) {
return false;
}
}
return true;
}
/**
* Returns an associative array of association names out a mixed
* array. If true is passed, then it returns all association names
* in this collection.
*
* @param bool|array $keys the list of association names to normalize
* @return array
*/
public function normalizeKeys($keys): array
{
if ($keys === true) {
$keys = $this->keys();
}
if (empty($keys)) {
return [];
}
return $this->_normalizeAssociations($keys);
}
/**
* Allow looping through the associations
*
* @return \Cake\ORM\Association[]
* @psalm-return \Traversable<string, \Cake\ORM\Association>
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->_items);
}
}