-
Notifications
You must be signed in to change notification settings - Fork 18
/
Marshaller.php
885 lines (787 loc) · 32.9 KB
/
Marshaller.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
<?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 ArrayObject;
use Cake\Collection\Collection;
use Cake\Database\Expression\TupleComparison;
use Cake\Database\TypeFactory;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\InvalidPropertyInterface;
use Cake\ORM\Association\BelongsToMany;
use InvalidArgumentException;
use RuntimeException;
/**
* Contains logic to convert array data into entities.
*
* Useful when converting request data into entities.
*
* @see \Cake\ORM\Table::newEntity()
* @see \Cake\ORM\Table::newEntities()
* @see \Cake\ORM\Table::patchEntity()
* @see \Cake\ORM\Table::patchEntities()
*/
class Marshaller
{
use AssociationsNormalizerTrait;
/**
* The table instance this marshaller is for.
*
* @var \Cake\ORM\Table
*/
protected $_table;
/**
* Constructor.
*
* @param \Cake\ORM\Table $table The table this marshaller is for.
*/
public function __construct(Table $table)
{
$this->_table = $table;
}
/**
* Build the map of property => marshalling callable.
*
* @param array $data The data being marshalled.
* @param array $options List of options containing the 'associated' key.
* @throws \InvalidArgumentException When associations do not exist.
* @return array
*/
protected function _buildPropertyMap(array $data, array $options): array
{
$map = [];
$schema = $this->_table->getSchema();
// Is a concrete column?
foreach (array_keys($data) as $prop) {
$prop = (string)$prop;
$columnType = $schema->getColumnType($prop);
if ($columnType) {
$map[$prop] = function ($value, $entity) use ($columnType) {
return TypeFactory::build($columnType)->marshal($value);
};
}
}
// Map associations
if (!isset($options['associated'])) {
$options['associated'] = [];
}
$include = $this->_normalizeAssociations($options['associated']);
foreach ($include as $key => $nested) {
if (is_int($key) && is_scalar($nested)) {
$key = $nested;
$nested = [];
}
// If the key is not a special field like _ids or _joinData
// it is a missing association that we should error on.
if (!$this->_table->hasAssociation($key)) {
if (substr($key, 0, 1) !== '_') {
throw new InvalidArgumentException(sprintf(
'Cannot marshal data for "%s" association. It is not associated with "%s".',
(string)$key,
$this->_table->getAlias()
));
}
continue;
}
$assoc = $this->_table->getAssociation($key);
if (isset($options['forceNew'])) {
$nested['forceNew'] = $options['forceNew'];
}
if (isset($options['isMerge'])) {
$callback = function ($value, $entity) use ($assoc, $nested) {
/** @var \Cake\Datasource\EntityInterface $entity */
$options = $nested + ['associated' => [], 'association' => $assoc];
return $this->_mergeAssociation($entity->get($assoc->getProperty()), $assoc, $value, $options);
};
} else {
$callback = function ($value, $entity) use ($assoc, $nested) {
$options = $nested + ['associated' => []];
return $this->_marshalAssociation($assoc, $value, $options);
};
}
$map[$assoc->getProperty()] = $callback;
}
$behaviors = $this->_table->behaviors();
foreach ($behaviors->loaded() as $name) {
$behavior = $behaviors->get($name);
if ($behavior instanceof PropertyMarshalInterface) {
$map += $behavior->buildMarshalMap($this, $map, $options);
}
}
return $map;
}
/**
* Hydrate one entity and its associated data.
*
* ### Options:
*
* - validate: Set to false to disable validation. Can also be a string of the validator ruleset to be applied.
* Defaults to true/default.
* - associated: Associations listed here will be marshalled as well. Defaults to null.
* - fields: An allowed list of fields to be assigned to the entity. If not present,
* the accessible fields list in the entity will be used. Defaults to null.
* - accessibleFields: A list of fields to allow or deny in entity accessible fields. Defaults to null
* - forceNew: When enabled, belongsToMany associations will have 'new' entities created
* when primary key values are set, and a record does not already exist. Normally primary key
* on missing entities would be ignored. Defaults to false.
*
* The above options can be used in each nested `associated` array. In addition to the above
* options you can also use the `onlyIds` option for HasMany and BelongsToMany associations.
* When true this option restricts the request data to only be read from `_ids`.
*
* ```
* $result = $marshaller->one($data, [
* 'associated' => ['Tags' => ['onlyIds' => true]]
* ]);
* ```
*
* ```
* $result = $marshaller->one($data, [
* 'associated' => [
* 'Tags' => ['accessibleFields' => ['*' => true]]
* ]
* ]);
* ```
*
* @param array $data The data to hydrate.
* @param array $options List of options
* @return \Cake\Datasource\EntityInterface
* @see \Cake\ORM\Table::newEntity()
* @see \Cake\ORM\Entity::$_accessible
*/
public function one(array $data, array $options = []): EntityInterface
{
[$data, $options] = $this->_prepareDataAndOptions($data, $options);
$primaryKey = (array)$this->_table->getPrimaryKey();
$entityClass = $this->_table->getEntityClass();
$entity = new $entityClass();
$entity->setSource($this->_table->getRegistryAlias());
if (isset($options['accessibleFields'])) {
foreach ((array)$options['accessibleFields'] as $key => $value) {
$entity->setAccess($key, $value);
}
}
$errors = $this->_validate($data, $options, true);
$options['isMerge'] = false;
$propertyMap = $this->_buildPropertyMap($data, $options);
$properties = [];
foreach ($data as $key => $value) {
if (!empty($errors[$key])) {
if ($entity instanceof InvalidPropertyInterface) {
$entity->setInvalidField($key, $value);
}
continue;
}
if ($value === '' && in_array($key, $primaryKey, true)) {
// Skip marshalling '' for pk fields.
continue;
}
if (isset($propertyMap[$key])) {
$properties[$key] = $propertyMap[$key]($value, $entity);
} else {
$properties[$key] = $value;
}
}
if (isset($options['fields'])) {
foreach ((array)$options['fields'] as $field) {
if (array_key_exists($field, $properties)) {
$entity->set($field, $properties[$field]);
}
}
} else {
$entity->set($properties);
}
// Don't flag clean association entities as
// dirty so we don't persist empty records.
foreach ($properties as $field => $value) {
if ($value instanceof EntityInterface) {
$entity->setDirty($field, $value->isDirty());
}
}
$entity->setErrors($errors);
$this->dispatchAfterMarshal($entity, $data, $options);
return $entity;
}
/**
* Returns the validation errors for a data set based on the passed options
*
* @param array $data The data to validate.
* @param array $options The options passed to this marshaller.
* @param bool $isNew Whether it is a new entity or one to be updated.
* @return array The list of validation errors.
* @throws \RuntimeException If no validator can be created.
*/
protected function _validate(array $data, array $options, bool $isNew): array
{
if (!$options['validate']) {
return [];
}
$validator = null;
if ($options['validate'] === true) {
$validator = $this->_table->getValidator();
} elseif (is_string($options['validate'])) {
$validator = $this->_table->getValidator($options['validate']);
} elseif (is_object($options['validate'])) {
/** @var \Cake\Validation\Validator $validator */
$validator = $options['validate'];
}
if ($validator === null) {
throw new RuntimeException(
sprintf('validate must be a boolean, a string or an object. Got %s.', getTypeName($options['validate']))
);
}
return $validator->validate($data, $isNew);
}
/**
* Returns data and options prepared to validate and marshall.
*
* @param array $data The data to prepare.
* @param array $options The options passed to this marshaller.
* @return array An array containing prepared data and options.
*/
protected function _prepareDataAndOptions(array $data, array $options): array
{
$options += ['validate' => true];
$tableName = $this->_table->getAlias();
if (isset($data[$tableName])) {
$data += $data[$tableName];
unset($data[$tableName]);
}
$data = new ArrayObject($data);
$options = new ArrayObject($options);
$this->_table->dispatchEvent('Model.beforeMarshal', compact('data', 'options'));
return [(array)$data, (array)$options];
}
/**
* Create a new sub-marshaller and marshal the associated data.
*
* @param \Cake\ORM\Association $assoc The association to marshall
* @param mixed $value The data to hydrate. If not an array, this method will return null.
* @param array $options List of options.
* @return \Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[]|null
*/
protected function _marshalAssociation(Association $assoc, $value, array $options)
{
if (!is_array($value)) {
return null;
}
$targetTable = $assoc->getTarget();
$marshaller = $targetTable->marshaller();
$types = [Association::ONE_TO_ONE, Association::MANY_TO_ONE];
$type = $assoc->type();
if (in_array($type, $types, true)) {
return $marshaller->one($value, $options);
}
if ($type === Association::ONE_TO_MANY || $type === Association::MANY_TO_MANY) {
$hasIds = array_key_exists('_ids', $value);
$onlyIds = array_key_exists('onlyIds', $options) && $options['onlyIds'];
if ($hasIds && is_array($value['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $value['_ids']);
}
if ($hasIds || $onlyIds) {
return [];
}
}
if ($type === Association::MANY_TO_MANY) {
/** @psalm-suppress ArgumentTypeCoercion */
return $marshaller->_belongsToMany($assoc, $value, $options);
}
return $marshaller->many($value, $options);
}
/**
* Hydrate many entities and their associated data.
*
* ### Options:
*
* - validate: Set to false to disable validation. Can also be a string of the validator ruleset to be applied.
* Defaults to true/default.
* - associated: Associations listed here will be marshalled as well. Defaults to null.
* - fields: An allowed list of fields to be assigned to the entity. If not present,
* the accessible fields list in the entity will be used. Defaults to null.
* - accessibleFields: A list of fields to allow or deny in entity accessible fields. Defaults to null
* - forceNew: When enabled, belongsToMany associations will have 'new' entities created
* when primary key values are set, and a record does not already exist. Normally primary key
* on missing entities would be ignored. Defaults to false.
*
* @param array $data The data to hydrate.
* @param array $options List of options
* @return \Cake\Datasource\EntityInterface[] An array of hydrated records.
* @see \Cake\ORM\Table::newEntities()
* @see \Cake\ORM\Entity::$_accessible
*/
public function many(array $data, array $options = []): array
{
$output = [];
foreach ($data as $record) {
if (!is_array($record)) {
continue;
}
$output[] = $this->one($record, $options);
}
return $output;
}
/**
* Marshals data for belongsToMany associations.
*
* Builds the related entities and handles the special casing
* for junction table entities.
*
* @param \Cake\ORM\Association\BelongsToMany $assoc The association to marshal.
* @param array $data The data to convert into entities.
* @param array $options List of options.
* @return \Cake\Datasource\EntityInterface[] An array of built entities.
* @throws \BadMethodCallException
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
protected function _belongsToMany(BelongsToMany $assoc, array $data, array $options = []): array
{
$associated = $options['associated'] ?? [];
$forceNew = $options['forceNew'] ?? false;
$data = array_values($data);
$target = $assoc->getTarget();
$primaryKey = array_flip((array)$target->getPrimaryKey());
$records = $conditions = [];
$primaryCount = count($primaryKey);
foreach ($data as $i => $row) {
if (!is_array($row)) {
continue;
}
if (array_intersect_key($primaryKey, $row) === $primaryKey) {
$keys = array_intersect_key($row, $primaryKey);
if (count($keys) === $primaryCount) {
$rowConditions = [];
foreach ($keys as $key => $value) {
$rowConditions[][$target->aliasField($key)] = $value;
}
if ($forceNew && !$target->exists($rowConditions)) {
$records[$i] = $this->one($row, $options);
}
$conditions = array_merge($conditions, $rowConditions);
}
} else {
$records[$i] = $this->one($row, $options);
}
}
if (!empty($conditions)) {
$query = $target->find();
$query->andWhere(function ($exp) use ($conditions) {
/** @var \Cake\Database\Expression\QueryExpression $exp */
return $exp->or($conditions);
});
$keyFields = array_keys($primaryKey);
$existing = [];
foreach ($query as $row) {
$k = implode(';', $row->extract($keyFields));
$existing[$k] = $row;
}
foreach ($data as $i => $row) {
$key = [];
foreach ($keyFields as $k) {
if (isset($row[$k])) {
$key[] = $row[$k];
}
}
$key = implode(';', $key);
// Update existing record and child associations
if (isset($existing[$key])) {
$records[$i] = $this->merge($existing[$key], $data[$i], $options);
}
}
}
$jointMarshaller = $assoc->junction()->marshaller();
$nested = [];
if (isset($associated['_joinData'])) {
$nested = (array)$associated['_joinData'];
}
foreach ($records as $i => $record) {
// Update junction table data in _joinData.
if (isset($data[$i]['_joinData'])) {
$joinData = $jointMarshaller->one($data[$i]['_joinData'], $nested);
$record->set('_joinData', $joinData);
}
}
return $records;
}
/**
* Loads a list of belongs to many from ids.
*
* @param \Cake\ORM\Association $assoc The association class for the belongsToMany association.
* @param array $ids The list of ids to load.
* @return \Cake\Datasource\EntityInterface[] An array of entities.
*/
protected function _loadAssociatedByIds(Association $assoc, array $ids): array
{
if (empty($ids)) {
return [];
}
$target = $assoc->getTarget();
$primaryKey = (array)$target->getPrimaryKey();
$multi = count($primaryKey) > 1;
$primaryKey = array_map([$target, 'aliasField'], $primaryKey);
if ($multi) {
$first = current($ids);
if (!is_array($first) || count($first) !== count($primaryKey)) {
return [];
}
$type = [];
$schema = $target->getSchema();
foreach ((array)$target->getPrimaryKey() as $column) {
$type[] = $schema->getColumnType($column);
}
$filter = new TupleComparison($primaryKey, $ids, $type, 'IN');
} else {
$filter = [$primaryKey[0] . ' IN' => $ids];
}
return $target->find()->where($filter)->toArray();
}
/**
* Merges `$data` into `$entity` and recursively does the same for each one of
* the association names passed in `$options`. When merging associations, if an
* entity is not present in the parent entity for a given association, a new one
* will be created.
*
* When merging HasMany or BelongsToMany associations, all the entities in the
* `$data` array will appear, those that can be matched by primary key will get
* the data merged, but those that cannot, will be discarded. `ids` option can be used
* to determine whether the association must use the `_ids` format.
*
* ### Options:
*
* - associated: Associations listed here will be marshalled as well.
* - validate: Whether or not to validate data before hydrating the entities. Can
* also be set to a string to use a specific validator. Defaults to true/default.
* - fields: An allowed list of fields to be assigned to the entity. If not present
* the accessible fields list in the entity will be used.
* - accessibleFields: A list of fields to allow or deny in entity accessible fields.
*
* The above options can be used in each nested `associated` array. In addition to the above
* options you can also use the `onlyIds` option for HasMany and BelongsToMany associations.
* When true this option restricts the request data to only be read from `_ids`.
*
* ```
* $result = $marshaller->merge($entity, $data, [
* 'associated' => ['Tags' => ['onlyIds' => true]]
* ]);
* ```
*
* @param \Cake\Datasource\EntityInterface $entity the entity that will get the
* data merged in
* @param array $data key value list of fields to be merged into the entity
* @param array $options List of options.
* @return \Cake\Datasource\EntityInterface
* @see \Cake\ORM\Entity::$_accessible
*/
public function merge(EntityInterface $entity, array $data, array $options = []): EntityInterface
{
[$data, $options] = $this->_prepareDataAndOptions($data, $options);
$isNew = $entity->isNew();
$keys = [];
if (!$isNew) {
$keys = $entity->extract((array)$this->_table->getPrimaryKey());
}
if (isset($options['accessibleFields'])) {
foreach ((array)$options['accessibleFields'] as $key => $value) {
$entity->setAccess($key, $value);
}
}
$errors = $this->_validate($data + $keys, $options, $isNew);
$options['isMerge'] = true;
$propertyMap = $this->_buildPropertyMap($data, $options);
$properties = [];
foreach ($data as $key => $value) {
if (!empty($errors[$key])) {
if ($entity instanceof InvalidPropertyInterface) {
$entity->setInvalidField($key, $value);
}
continue;
}
$original = $entity->get($key);
if (isset($propertyMap[$key])) {
$value = $propertyMap[$key]($value, $entity);
// Don't dirty scalar values and objects that didn't
// change. Arrays will always be marked as dirty because
// the original/updated list could contain references to the
// same objects, even though those objects may have changed internally.
if (
(
is_scalar($value)
&& $original === $value
)
|| (
$value === null
&& $original === $value
)
|| (
is_object($value)
&& !($value instanceof EntityInterface)
&& $original == $value
)
) {
continue;
}
}
$properties[$key] = $value;
}
$entity->setErrors($errors);
if (!isset($options['fields'])) {
$entity->set($properties);
foreach ($properties as $field => $value) {
if ($value instanceof EntityInterface) {
$entity->setDirty($field, $value->isDirty());
}
}
$this->dispatchAfterMarshal($entity, $data, $options);
return $entity;
}
foreach ((array)$options['fields'] as $field) {
if (!array_key_exists($field, $properties)) {
continue;
}
$entity->set($field, $properties[$field]);
if ($properties[$field] instanceof EntityInterface) {
$entity->setDirty($field, $properties[$field]->isDirty());
}
}
$this->dispatchAfterMarshal($entity, $data, $options);
return $entity;
}
/**
* Merges each of the elements from `$data` into each of the entities in `$entities`
* and recursively does the same for each of the association names passed in
* `$options`. When merging associations, if an entity is not present in the parent
* entity for a given association, a new one will be created.
*
* Records in `$data` are matched against the entities using the primary key
* column. Entries in `$entities` that cannot be matched to any record in
* `$data` will be discarded. Records in `$data` that could not be matched will
* be marshalled as a new entity.
*
* When merging HasMany or BelongsToMany associations, all the entities in the
* `$data` array will appear, those that can be matched by primary key will get
* the data merged, but those that cannot, will be discarded.
*
* ### Options:
*
* - validate: Whether or not to validate data before hydrating the entities. Can
* also be set to a string to use a specific validator. Defaults to true/default.
* - associated: Associations listed here will be marshalled as well.
* - fields: An allowed list of fields to be assigned to the entity. If not present,
* the accessible fields list in the entity will be used.
* - accessibleFields: A list of fields to allow or deny in entity accessible fields.
*
* @param iterable<\Cake\Datasource\EntityInterface> $entities the entities that will get the
* data merged in
* @param array $data list of arrays to be merged into the entities
* @param array $options List of options.
* @return \Cake\Datasource\EntityInterface[]
* @see \Cake\ORM\Entity::$_accessible
* @psalm-suppress NullArrayOffset
*/
public function mergeMany(iterable $entities, array $data, array $options = []): array
{
$primary = (array)$this->_table->getPrimaryKey();
$indexed = (new Collection($data))
->groupBy(function ($el) use ($primary) {
$keys = [];
foreach ($primary as $key) {
$keys[] = $el[$key] ?? '';
}
return implode(';', $keys);
})
->map(function ($element, $key) {
return $key === '' ? $element : $element[0];
})
->toArray();
/** @psalm-suppress InvalidArrayOffset */
$new = $indexed[null] ?? [];
/** @psalm-suppress InvalidArrayOffset */
unset($indexed[null]);
$output = [];
foreach ($entities as $entity) {
if (!($entity instanceof EntityInterface)) {
continue;
}
$key = implode(';', $entity->extract($primary));
if (!isset($indexed[$key])) {
continue;
}
$output[] = $this->merge($entity, $indexed[$key], $options);
unset($indexed[$key]);
}
$conditions = (new Collection($indexed))
->map(function ($data, $key) {
return explode(';', (string)$key);
})
->filter(function ($keys) use ($primary) {
return count(array_filter($keys, 'strlen')) === count($primary);
})
->reduce(function ($conditions, $keys) use ($primary) {
$fields = array_map([$this->_table, 'aliasField'], $primary);
$conditions['OR'][] = array_combine($fields, $keys);
return $conditions;
}, ['OR' => []]);
$maybeExistentQuery = $this->_table->find()->where($conditions);
if (!empty($indexed) && count($maybeExistentQuery->clause('where'))) {
foreach ($maybeExistentQuery as $entity) {
$key = implode(';', $entity->extract($primary));
if (isset($indexed[$key])) {
$output[] = $this->merge($entity, $indexed[$key], $options);
unset($indexed[$key]);
}
}
}
foreach ((new Collection($indexed))->append($new) as $value) {
if (!is_array($value)) {
continue;
}
$output[] = $this->one($value, $options);
}
return $output;
}
/**
* Creates a new sub-marshaller and merges the associated data.
*
* @param \Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[] $original The original entity
* @param \Cake\ORM\Association $assoc The association to merge
* @param mixed $value The array of data to hydrate. If not an array, this method will return null.
* @param array $options List of options.
* @return \Cake\Datasource\EntityInterface|\Cake\Datasource\EntityInterface[]|null
*/
protected function _mergeAssociation($original, Association $assoc, $value, array $options)
{
if (!$original) {
return $this->_marshalAssociation($assoc, $value, $options);
}
if (!is_array($value)) {
return null;
}
$targetTable = $assoc->getTarget();
$marshaller = $targetTable->marshaller();
$types = [Association::ONE_TO_ONE, Association::MANY_TO_ONE];
$type = $assoc->type();
if (in_array($type, $types, true)) {
/** @psalm-suppress PossiblyInvalidArgument, ArgumentTypeCoercion */
return $marshaller->merge($original, $value, $options);
}
if ($type === Association::MANY_TO_MANY) {
/** @psalm-suppress PossiblyInvalidArgument, ArgumentTypeCoercion */
return $marshaller->_mergeBelongsToMany($original, $assoc, $value, $options);
}
if ($type === Association::ONE_TO_MANY) {
$hasIds = array_key_exists('_ids', $value);
$onlyIds = array_key_exists('onlyIds', $options) && $options['onlyIds'];
if ($hasIds && is_array($value['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $value['_ids']);
}
if ($hasIds || $onlyIds) {
return [];
}
}
/** @psalm-suppress PossiblyInvalidArgument */
return $marshaller->mergeMany($original, $value, $options);
}
/**
* Creates a new sub-marshaller and merges the associated data for a BelongstoMany
* association.
*
* @param \Cake\Datasource\EntityInterface[] $original The original entities list.
* @param \Cake\ORM\Association\BelongsToMany $assoc The association to marshall
* @param array $value The data to hydrate
* @param array $options List of options.
* @return \Cake\Datasource\EntityInterface[]
*/
protected function _mergeBelongsToMany(array $original, BelongsToMany $assoc, array $value, array $options): array
{
$associated = $options['associated'] ?? [];
$hasIds = array_key_exists('_ids', $value);
$onlyIds = array_key_exists('onlyIds', $options) && $options['onlyIds'];
if ($hasIds && is_array($value['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $value['_ids']);
}
if ($hasIds || $onlyIds) {
return [];
}
if (!empty($associated) && !in_array('_joinData', $associated, true) && !isset($associated['_joinData'])) {
return $this->mergeMany($original, $value, $options);
}
return $this->_mergeJoinData($original, $assoc, $value, $options);
}
/**
* Merge the special _joinData property into the entity set.
*
* @param \Cake\Datasource\EntityInterface[] $original The original entities list.
* @param \Cake\ORM\Association\BelongsToMany $assoc The association to marshall
* @param array $value The data to hydrate
* @param array $options List of options.
* @return \Cake\Datasource\EntityInterface[] An array of entities
*/
protected function _mergeJoinData(array $original, BelongsToMany $assoc, array $value, array $options): array
{
$associated = $options['associated'] ?? [];
$extra = [];
foreach ($original as $entity) {
// Mark joinData as accessible so we can marshal it properly.
$entity->setAccess('_joinData', true);
$joinData = $entity->get('_joinData');
if ($joinData && $joinData instanceof EntityInterface) {
$extra[spl_object_hash($entity)] = $joinData;
}
}
$joint = $assoc->junction();
$marshaller = $joint->marshaller();
$nested = [];
if (isset($associated['_joinData'])) {
$nested = (array)$associated['_joinData'];
}
$options['accessibleFields'] = ['_joinData' => true];
$records = $this->mergeMany($original, $value, $options);
foreach ($records as $record) {
$hash = spl_object_hash($record);
$value = $record->get('_joinData');
// Already an entity, no further marshalling required.
if ($value instanceof EntityInterface) {
continue;
}
// Scalar data can't be handled
if (!is_array($value)) {
$record->unset('_joinData');
continue;
}
// Marshal data into the old object, or make a new joinData object.
if (isset($extra[$hash])) {
$record->set('_joinData', $marshaller->merge($extra[$hash], $value, $nested));
} else {
$joinData = $marshaller->one($value, $nested);
$record->set('_joinData', $joinData);
}
}
return $records;
}
/**
* dispatch Model.afterMarshal event.
*
* @param \Cake\Datasource\EntityInterface $entity The entity that was marshaled.
* @param array $data readOnly $data to use.
* @param array $options List of options that are readOnly.
* @return void
*/
protected function dispatchAfterMarshal(EntityInterface $entity, array $data, array $options = []): void
{
$data = new ArrayObject($data);
$options = new ArrayObject($options);
$this->_table->dispatchEvent('Model.afterMarshal', compact('entity', 'data', 'options'));
}
}