| @@ 16-91 (lines=76) @@ | ||
| 13 | use Doctrine\Common\Collections\ArrayCollection; | |
| 14 | use Doctrine\Common\Persistence\Proxy; | |
| 15 | ||
| 16 | final class ReferenceManyFieldDenormalizer implements FieldDenormalizerInterface | |
| 17 | { | |
| 18 | /** | |
| 19 | * @var callable | |
| 20 | */ | |
| 21 | private $repository; | |
| 22 | ||
| 23 | /** | |
| 24 | * @var AccessorInterface | |
| 25 | */ | |
| 26 | private $accessor; | |
| 27 | ||
| 28 | /** | |
| 29 | * @param callable $repository | |
| 30 | * @param AccessorInterface $accessor | |
| 31 | */ | |
| 32 | public function __construct(callable $repository, AccessorInterface $accessor) | |
| 33 |     { | |
| 34 | $this->repository = $repository; | |
| 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 (!is_array($value)) { | |
| 62 | throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array'); | |
| 63 | } | |
| 64 | ||
| 65 | $repository = $this->repository; | |
| 66 | ||
| 67 | $refObjects = new ArrayCollection(); | |
| 68 |         foreach ($value as $i => $subValue) { | |
| 69 | $subPath = $path.'['.$i.']'; | |
| 70 | ||
| 71 |             if (!is_string($subValue)) { | |
| 72 | throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'string'); | |
| 73 | } | |
| 74 | ||
| 75 | $refObject = $repository($subValue); | |
| 76 | ||
| 77 | $this->resolveProxy($refObject); | |
| 78 | ||
| 79 | $refObjects[$i] = $refObject; | |
| 80 | } | |
| 81 | ||
| 82 | $this->accessor->setValue($object, $refObjects); | |
| 83 | } | |
| 84 | ||
| 85 | private function resolveProxy($refObject) | |
| 86 |     { | |
| 87 |         if (null !== $refObject && $refObject instanceof Proxy && !$refObject->__isInitialized()) { | |
| 88 | $refObject->__load(); | |
| 89 | } | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| @@ 18-95 (lines=78) @@ | ||
| 15 | /** | |
| 16 | * @deprecated use Basic or Doctrine ReferenceManyFieldDenormalizer | |
| 17 | */ | |
| 18 | final class ReferenceManyFieldDenormalizer implements FieldDenormalizerInterface | |
| 19 | { | |
| 20 | /** | |
| 21 | * @var callable | |
| 22 | */ | |
| 23 | private $repository; | |
| 24 | ||
| 25 | /** | |
| 26 | * @var AccessorInterface | |
| 27 | */ | |
| 28 | private $accessor; | |
| 29 | ||
| 30 | /** | |
| 31 | * @param callable $repository | |
| 32 | * @param AccessorInterface $accessor | |
| 33 | */ | |
| 34 | public function __construct(callable $repository, AccessorInterface $accessor) | |
| 35 |     { | |
| 36 | $this->repository = $repository; | |
| 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 (!is_array($value)) { | |
| 64 | throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array'); | |
| 65 | } | |
| 66 | ||
| 67 | $repository = $this->repository; | |
| 68 | ||
| 69 | $refObjects = []; | |
| 70 |         foreach ($value as $i => $subValue) { | |
| 71 | $subPath = $path.'['.$i.']'; | |
| 72 | ||
| 73 |             if (!is_string($subValue)) { | |
| 74 | throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'string'); | |
| 75 | } | |
| 76 | ||
| 77 | $refObject = $repository($subValue); | |
| 78 | ||
| 79 | $this->resolveProxy($refObject); | |
| 80 | ||
| 81 | $refObjects[$i] = $refObject; | |
| 82 | } | |
| 83 | ||
| 84 | $this->accessor->setValue($object, $refObjects); | |
| 85 | } | |
| 86 | ||
| 87 | private function resolveProxy($refObject) | |
| 88 |     { | |
| 89 |         if (null !== $refObject && interface_exists('Doctrine\Common\Persistence\Proxy') | |
| 90 | && $refObject instanceof Proxy && !$refObject->__isInitialized() | |
| 91 |         ) { | |
| 92 | $refObject->__load(); | |
| 93 | } | |
| 94 | } | |
| 95 | } | |
| 96 | ||