| @@ 14-82 (lines=69) @@ | ||
| 11 | /** | |
| 12 | * @deprecated 1.0-beta use EmbedManyFieldDenormalizer or ReferenceManyFieldDenormalizer | |
| 13 | */ | |
| 14 | final class CollectionFieldDenormalizer implements FieldDenormalizerInterface | |
| 15 | { | |
| 16 | /** | |
| 17 | * @var string | |
| 18 | */ | |
| 19 | private $class; | |
| 20 | ||
| 21 | /** | |
| 22 | * @var AccessorInterface | |
| 23 | */ | |
| 24 | private $accessor; | |
| 25 | ||
| 26 | /** | |
| 27 | * @param string $class | |
| 28 | * @param AccessorInterface $accessor | |
| 29 | */ | |
| 30 | public function __construct(string $class, AccessorInterface $accessor) | |
| 31 |     { | |
| 32 | $this->class = $class; | |
| 33 | $this->accessor = $accessor; | |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * @param string $path | |
| 38 | * @param object $object | |
| 39 | * @param mixed $value | |
| 40 | * @param DenormalizerContextInterface $context | |
| 41 | * @param DenormalizerInterface|null $denormalizer | |
| 42 | * | |
| 43 | * @throws DeserializerLogicException | |
| 44 | * @throws DeserializerRuntimeException | |
| 45 | */ | |
| 46 | public function denormalizeField( | |
| 47 | string $path, | |
| 48 | $object, | |
| 49 | $value, | |
| 50 | DenormalizerContextInterface $context, | |
| 51 | DenormalizerInterface $denormalizer = null | |
| 52 |     ) { | |
| 53 |         if (null === $denormalizer) { | |
| 54 | throw DeserializerLogicException::createMissingDenormalizer($path); | |
| 55 | } | |
| 56 | ||
| 57 |         if (!is_array($value)) { | |
| 58 | throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array'); | |
| 59 | } | |
| 60 | ||
| 61 | $existingChildObjects = $this->accessor->getValue($object) ?? []; | |
| 62 | ||
| 63 | $newChildObjects = []; | |
| 64 |         foreach ($value as $i => $subValue) { | |
| 65 | $subPath = $path.'['.$i.']'; | |
| 66 | ||
| 67 |             if (!is_array($subValue)) { | |
| 68 | throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array'); | |
| 69 | } | |
| 70 | ||
| 71 |             if (isset($existingChildObjects[$i])) { | |
| 72 | $newChildObject = $existingChildObjects[$i]; | |
| 73 |             } else { | |
| 74 | $newChildObject = $this->class; | |
| 75 | } | |
| 76 | ||
| 77 | $newChildObjects[$i] = $denormalizer->denormalize($newChildObject, $subValue, $context, $subPath); | |
| 78 | } | |
| 79 | ||
| 80 | $this->accessor->setValue($object, $newChildObjects); | |
| 81 | } | |
| 82 | } | |
| 83 | ||
| @@ 14-82 (lines=69) @@ | ||
| 11 | use Chubbyphp\Deserialization\DeserializerLogicException; | |
| 12 | use Chubbyphp\Deserialization\DeserializerRuntimeException; | |
| 13 | ||
| 14 | final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface | |
| 15 | { | |
| 16 | /** | |
| 17 | * @var string | |
| 18 | */ | |
| 19 | private $class; | |
| 20 | ||
| 21 | /** | |
| 22 | * @var AccessorInterface | |
| 23 | */ | |
| 24 | private $accessor; | |
| 25 | ||
| 26 | /** | |
| 27 | * @param string $class | |
| 28 | * @param AccessorInterface $accessor | |
| 29 | */ | |
| 30 | public function __construct(string $class, AccessorInterface $accessor) | |
| 31 |     { | |
| 32 | $this->class = $class; | |
| 33 | $this->accessor = $accessor; | |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * @param string $path | |
| 38 | * @param object $object | |
| 39 | * @param mixed $value | |
| 40 | * @param DenormalizerContextInterface $context | |
| 41 | * @param DenormalizerInterface|null $denormalizer | |
| 42 | * | |
| 43 | * @throws DeserializerLogicException | |
| 44 | * @throws DeserializerRuntimeException | |
| 45 | */ | |
| 46 | public function denormalizeField( | |
| 47 | string $path, | |
| 48 | $object, | |
| 49 | $value, | |
| 50 | DenormalizerContextInterface $context, | |
| 51 | DenormalizerInterface $denormalizer = null | |
| 52 |     ) { | |
| 53 |         if (null === $denormalizer) { | |
| 54 | throw DeserializerLogicException::createMissingDenormalizer($path); | |
| 55 | } | |
| 56 | ||
| 57 |         if (!is_array($value)) { | |
| 58 | throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array'); | |
| 59 | } | |
| 60 | ||
| 61 | $existingChildObjects = $this->accessor->getValue($object) ?? []; | |
| 62 | ||
| 63 | $newChildObjects = []; | |
| 64 |         foreach ($value as $i => $subValue) { | |
| 65 | $subPath = $path.'['.$i.']'; | |
| 66 | ||
| 67 |             if (!is_array($subValue)) { | |
| 68 | throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array'); | |
| 69 | } | |
| 70 | ||
| 71 |             if (isset($existingChildObjects[$i])) { | |
| 72 | $newChildObject = $existingChildObjects[$i]; | |
| 73 |             } else { | |
| 74 | $newChildObject = $this->class; | |
| 75 | } | |
| 76 | ||
| 77 | $newChildObjects[$i] = $denormalizer->denormalize($newChildObject, $subValue, $context, $subPath); | |
| 78 | } | |
| 79 | ||
| 80 | $this->accessor->setValue($object, $newChildObjects); | |
| 81 | } | |
| 82 | } | |
| 83 | ||