vendor/symfony/serializer/Normalizer/UnwrappingDenormalizer.php line 30

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Serializer\Normalizer;
  11. use Symfony\Component\PropertyAccess\PropertyAccess;
  12. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  13. use Symfony\Component\Serializer\SerializerAwareInterface;
  14. use Symfony\Component\Serializer\SerializerAwareTrait;
  15. /**
  16.  * @author Eduard Bulava <bulavaeduard@gmail.com>
  17.  */
  18. final class UnwrappingDenormalizer implements DenormalizerInterfaceSerializerAwareInterfaceCacheableSupportsMethodInterface
  19. {
  20.     use SerializerAwareTrait;
  21.     public const UNWRAP_PATH 'unwrap_path';
  22.     private $propertyAccessor;
  23.     public function __construct(PropertyAccessorInterface $propertyAccessor null)
  24.     {
  25.         $this->propertyAccessor $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
  26.     }
  27.     public function denormalize(mixed $datastring $classstring $format null, array $context = []): mixed
  28.     {
  29.         $propertyPath $context[self::UNWRAP_PATH];
  30.         $context['unwrapped'] = true;
  31.         if ($propertyPath) {
  32.             if (!$this->propertyAccessor->isReadable($data$propertyPath)) {
  33.                 return null;
  34.             }
  35.             $data $this->propertyAccessor->getValue($data$propertyPath);
  36.         }
  37.         return $this->serializer->denormalize($data$class$format$context);
  38.     }
  39.     public function supportsDenormalization(mixed $datastring $typestring $format null, array $context = []): bool
  40.     {
  41.         return \array_key_exists(self::UNWRAP_PATH$context) && !isset($context['unwrapped']);
  42.     }
  43.     public function hasCacheableSupportsMethod(): bool
  44.     {
  45.         return $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod();
  46.     }
  47. }