Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Normalizer/UnwrappingDenormalizer.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Serializer\Normalizer;
13
14 use Symfony\Component\PropertyAccess\PropertyAccess;
15 use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
16 use Symfony\Component\Serializer\Exception\LogicException;
17 use Symfony\Component\Serializer\SerializerAwareInterface;
18 use Symfony\Component\Serializer\SerializerAwareTrait;
19
20 /**
21 * @author Eduard Bulava <[email protected]>
22 */
23 final class UnwrappingDenormalizer implements DenormalizerInterface, SerializerAwareInterface
24 {
25 use SerializerAwareTrait;
26
27 public const UNWRAP_PATH = 'unwrap_path';
28
29 private readonly PropertyAccessorInterface $propertyAccessor;
30
31 public function __construct(?PropertyAccessorInterface $propertyAccessor = null)
32 {
33 $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
34 }
35
36 public function getSupportedTypes(?string $format): array
37 {
38 return ['*' => false];
39 }
40
41 public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
42 {
43 $propertyPath = $context[self::UNWRAP_PATH];
44 $context['unwrapped'] = true;
45
46 if ($propertyPath) {
47 if (!$this->propertyAccessor->isReadable($data, $propertyPath)) {
48 return null;
49 }
50
51 $data = $this->propertyAccessor->getValue($data, $propertyPath);
52 }
53
54 if (!$this->serializer instanceof DenormalizerInterface) {
55 throw new LogicException('Cannot unwrap path because the injected serializer is not a denormalizer.');
56 }
57
58 return $this->serializer->denormalize($data, $type, $format, $context);
59 }
60
61 public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
62 {
63 return \array_key_exists(self::UNWRAP_PATH, $context) && !isset($context['unwrapped']);
64 }
65 }