| @@ 14-77 (lines=64) @@ | ||
| 11 | use Chubbyphp\Deserialization\DeserializerRuntimeException; | |
| 12 | use Doctrine\Common\Persistence\Proxy; | |
| 13 | ||
| 14 | final class ReferenceOneFieldDenormalizer implements FieldDenormalizerInterface | |
| 15 | { | |
| 16 | /** | |
| 17 | * @var callable | |
| 18 | */ | |
| 19 | private $repository; | |
| 20 | ||
| 21 | /** | |
| 22 | * @var AccessorInterface | |
| 23 | */ | |
| 24 | private $accessor; | |
| 25 | ||
| 26 | /** | |
| 27 | * @param callable $repository | |
| 28 | * @param AccessorInterface $accessor | |
| 29 | */ | |
| 30 | public function __construct(callable $repository, AccessorInterface $accessor) | |
| 31 |     { | |
| 32 | $this->repository = $repository; | |
| 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 DeserializerRuntimeException | |
| 44 | */ | |
| 45 | public function denormalizeField( | |
| 46 | string $path, | |
| 47 | $object, | |
| 48 | $value, | |
| 49 | DenormalizerContextInterface $context, | |
| 50 | DenormalizerInterface $denormalizer = null | |
| 51 |     ) { | |
| 52 |         if (null === $value) { | |
| 53 | $this->accessor->setValue($object, $value); | |
| 54 | ||
| 55 | return; | |
| 56 | } | |
| 57 | ||
| 58 |         if (!is_string($value)) { | |
| 59 | throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'string'); | |
| 60 | } | |
| 61 | ||
| 62 | $repository = $this->repository; | |
| 63 | ||
| 64 | $refObject = $repository($value); | |
| 65 | ||
| 66 | $this->resolveProxy($refObject); | |
| 67 | ||
| 68 | $this->accessor->setValue($object, $refObject); | |
| 69 | } | |
| 70 | ||
| 71 | private function resolveProxy($refObject) | |
| 72 |     { | |
| 73 |         if (null !== $refObject && $refObject instanceof Proxy && !$refObject->__isInitialized()) { | |
| 74 | $refObject->__load(); | |
| 75 | } | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| @@ 17-82 (lines=66) @@ | ||
| 14 | /** | |
| 15 | * @deprecated use Basic or Doctrine ReferenceOneFieldDenormalizer | |
| 16 | */ | |
| 17 | final class ReferenceOneFieldDenormalizer implements FieldDenormalizerInterface | |
| 18 | { | |
| 19 | /** | |
| 20 | * @var callable | |
| 21 | */ | |
| 22 | private $repository; | |
| 23 | ||
| 24 | /** | |
| 25 | * @var AccessorInterface | |
| 26 | */ | |
| 27 | private $accessor; | |
| 28 | ||
| 29 | /** | |
| 30 | * @param callable $repository | |
| 31 | * @param AccessorInterface $accessor | |
| 32 | */ | |
| 33 | public function __construct(callable $repository, AccessorInterface $accessor) | |
| 34 |     { | |
| 35 | $this->repository = $repository; | |
| 36 | $this->accessor = $accessor; | |
| 37 | } | |
| 38 | ||
| 39 | /** | |
| 40 | * @param string $path | |
| 41 | * @param object $object | |
| 42 | * @param mixed $value | |
| 43 | * @param DenormalizerContextInterface $context | |
| 44 | * @param DenormalizerInterface|null $denormalizer | |
| 45 | * | |
| 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_string($value)) { | |
| 62 | throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'string'); | |
| 63 | } | |
| 64 | ||
| 65 | $repository = $this->repository; | |
| 66 | ||
| 67 | $refObject = $repository($value); | |
| 68 | ||
| 69 | $this->resolveProxy($refObject); | |
| 70 | ||
| 71 | $this->accessor->setValue($object, $refObject); | |
| 72 | } | |
| 73 | ||
| 74 | private function resolveProxy($refObject) | |
| 75 |     { | |
| 76 |         if (null !== $refObject && interface_exists('Doctrine\Common\Persistence\Proxy') | |
| 77 | && $refObject instanceof Proxy && !$refObject->__isInitialized() | |
| 78 |         ) { | |
| 79 | $refObject->__load(); | |
| 80 | } | |
| 81 | } | |
| 82 | } | |
| 83 | ||