| @@ 16-103 (lines=88) @@ | ||
| 13 | use Doctrine\Common\Collections\ArrayCollection; | |
| 14 | use Doctrine\Common\Persistence\Proxy; | |
| 15 | ||
| 16 | final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface | |
| 17 | { | |
| 18 | /** | |
| 19 | * @var string | |
| 20 | */ | |
| 21 | private $class; | |
| 22 | ||
| 23 | /** | |
| 24 | * @var AccessorInterface | |
| 25 | */ | |
| 26 | private $accessor; | |
| 27 | ||
| 28 | /** | |
| 29 | * @param string $class | |
| 30 | * @param AccessorInterface $accessor | |
| 31 | */ | |
| 32 | public function __construct(string $class, AccessorInterface $accessor) | |
| 33 |     { | |
| 34 | $this->class = $class; | |
| 35 | $this->accessor = $accessor; | |
| 36 | } | |
| 37 | ||
| 38 | /** | |
| 39 | * @param string $path | |
| 40 | * @param object $object | |
| 41 | * @param mixed $value | |
| 42 | * @param DenormalizerContextInterface $context | |
| 43 | * @param DenormalizerInterface|null $denormalizer | |
| 44 | * | |
| 45 | * @throws DeserializerLogicException | |
| 46 | * @throws DeserializerRuntimeException | |
| 47 | */ | |
| 48 | public function denormalizeField( | |
| 49 | string $path, | |
| 50 | $object, | |
| 51 | $value, | |
| 52 | DenormalizerContextInterface $context, | |
| 53 | DenormalizerInterface $denormalizer = null | |
| 54 |     ) { | |
| 55 |         if (null === $value) { | |
| 56 | $this->accessor->setValue($object, $value); | |
| 57 | ||
| 58 | return; | |
| 59 | } | |
| 60 | ||
| 61 |         if (null === $denormalizer) { | |
| 62 | throw DeserializerLogicException::createMissingDenormalizer($path); | |
| 63 | } | |
| 64 | ||
| 65 |         if (!is_array($value)) { | |
| 66 | throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array'); | |
| 67 | } | |
| 68 | ||
| 69 | $existEmbObjects = $this->accessor->getValue($object); | |
| 70 | ||
| 71 | $embObjects = new ArrayCollection(); | |
| 72 |         foreach ($value as $i => $subValue) { | |
| 73 | $subPath = $path.'['.$i.']'; | |
| 74 | ||
| 75 |             if (!is_array($subValue)) { | |
| 76 | throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array'); | |
| 77 | } | |
| 78 | ||
| 79 | $embObject = $this->getEmbObjectOrClass($existEmbObjects[$i] ?? null); | |
| 80 | ||
| 81 | $embObjects[$i] = $denormalizer->denormalize($embObject, $subValue, $context, $subPath); | |
| 82 | } | |
| 83 | ||
| 84 | $this->accessor->setValue($object, $embObjects); | |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * @param object|null $existEmbObject | |
| 89 | * @return string | |
| 90 | */ | |
| 91 | private function getEmbObjectOrClass($existEmbObject) | |
| 92 |     { | |
| 93 |         if (null === $existEmbObject) { | |
| 94 | return $this->class; | |
| 95 | } | |
| 96 | ||
| 97 |         if ($existEmbObject instanceof Proxy && !$existEmbObject->__isInitialized()) { | |
| 98 | $existEmbObject->__load(); | |
| 99 | } | |
| 100 | ||
| 101 | return $existEmbObject; | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| @@ 18-107 (lines=90) @@ | ||
| 15 | /** | |
| 16 | * @deprecated use Basic or Doctrine EmbedManyFieldDenormalizer | |
| 17 | */ | |
| 18 | final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface | |
| 19 | { | |
| 20 | /** | |
| 21 | * @var string | |
| 22 | */ | |
| 23 | private $class; | |
| 24 | ||
| 25 | /** | |
| 26 | * @var AccessorInterface | |
| 27 | */ | |
| 28 | private $accessor; | |
| 29 | ||
| 30 | /** | |
| 31 | * @param string $class | |
| 32 | * @param AccessorInterface $accessor | |
| 33 | */ | |
| 34 | public function __construct(string $class, AccessorInterface $accessor) | |
| 35 |     { | |
| 36 | $this->class = $class; | |
| 37 | $this->accessor = $accessor; | |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * @param string $path | |
| 42 | * @param object $object | |
| 43 | * @param mixed $value | |
| 44 | * @param DenormalizerContextInterface $context | |
| 45 | * @param DenormalizerInterface|null $denormalizer | |
| 46 | * | |
| 47 | * @throws DeserializerLogicException | |
| 48 | * @throws DeserializerRuntimeException | |
| 49 | */ | |
| 50 | public function denormalizeField( | |
| 51 | string $path, | |
| 52 | $object, | |
| 53 | $value, | |
| 54 | DenormalizerContextInterface $context, | |
| 55 | DenormalizerInterface $denormalizer = null | |
| 56 |     ) { | |
| 57 |         if (null === $value) { | |
| 58 | $this->accessor->setValue($object, $value); | |
| 59 | ||
| 60 | return; | |
| 61 | } | |
| 62 | ||
| 63 |         if (null === $denormalizer) { | |
| 64 | throw DeserializerLogicException::createMissingDenormalizer($path); | |
| 65 | } | |
| 66 | ||
| 67 |         if (!is_array($value)) { | |
| 68 | throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array'); | |
| 69 | } | |
| 70 | ||
| 71 | $existEmbObjects = $this->accessor->getValue($object); | |
| 72 | ||
| 73 | $embObjects = []; | |
| 74 |         foreach ($value as $i => $subValue) { | |
| 75 | $subPath = $path.'['.$i.']'; | |
| 76 | ||
| 77 |             if (!is_array($subValue)) { | |
| 78 | throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array'); | |
| 79 | } | |
| 80 | ||
| 81 | $embObject = $this->getEmbObjectOrClass($existEmbObjects[$i] ?? null); | |
| 82 | ||
| 83 | $embObjects[$i] = $denormalizer->denormalize($embObject, $subValue, $context, $subPath); | |
| 84 | } | |
| 85 | ||
| 86 | $this->accessor->setValue($object, $embObjects); | |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * @param object|null $existEmbObject | |
| 91 | * @return string | |
| 92 | */ | |
| 93 | private function getEmbObjectOrClass($existEmbObject) | |
| 94 |     { | |
| 95 |         if (null === $existEmbObject) { | |
| 96 | return $this->class; | |
| 97 | } | |
| 98 | ||
| 99 |         if (interface_exists('Doctrine\Common\Persistence\Proxy') | |
| 100 | && $existEmbObject instanceof Proxy && !$existEmbObject->__isInitialized() | |
| 101 |         ) { | |
| 102 | $existEmbObject->__load(); | |
| 103 | } | |
| 104 | ||
| 105 | return $existEmbObject; | |
| 106 | } | |
| 107 | } | |
| 108 | ||