| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Normalizer; |
| 13 |
|
| 14 |
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException as PropertyAccessInvalidArgumentException; |
| 15 |
use Symfony\Component\PropertyAccess\Exception\InvalidTypeException; |
| 16 |
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
| 17 |
use Symfony\Component\PropertyAccess\PropertyAccess; |
| 18 |
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 19 |
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; |
| 20 |
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; |
| 21 |
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; |
| 22 |
use Symfony\Component\PropertyInfo\PropertyWriteInfo; |
| 23 |
use Symfony\Component\Serializer\Attribute\Ignore; |
| 24 |
use Symfony\Component\Serializer\Exception\LogicException; |
| 25 |
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
| 26 |
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface; |
| 27 |
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
| 28 |
use Symfony\Component\Serializer\Mapping\Loader\AccessorCollisionResolverTrait; |
| 29 |
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
> |
| 35 |
|
| 36 |
final class ObjectNormalizer extends AbstractObjectNormalizer |
| 37 |
{ |
| 38 |
use AccessorCollisionResolverTrait; |
| 39 |
|
| 40 |
private static $reflectionCache = []; |
| 41 |
private static $isReadableCache = []; |
| 42 |
private static $isWritableCache = []; |
| 43 |
|
| 44 |
protected PropertyAccessorInterface $propertyAccessor; |
| 45 |
protected $propertyInfoExtractor; |
| 46 |
private $writeInfoExtractor; |
| 47 |
|
| 48 |
private readonly \Closure $objectClassResolver; |
| 49 |
|
| 50 |
public function __construct(?ClassMetadataFactoryInterface $classMetadataFactory = null, ?NameConverterInterface $nameConverter = null, ?PropertyAccessorInterface $propertyAccessor = null, ?PropertyTypeExtractorInterface $propertyTypeExtractor = null, ?ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, ?callable $objectClassResolver = null, array $defaultContext = [], ?PropertyInfoExtractorInterface $propertyInfoExtractor = null) |
| 51 |
{ |
| 52 |
if (!class_exists(PropertyAccess::class)) { |
| 53 |
throw new LogicException('The ObjectNormalizer class requires the "PropertyAccess" component. Try running "composer require symfony/property-access".'); |
| 54 |
} |
| 55 |
|
| 56 |
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext); |
| 57 |
|
| 58 |
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); |
| 59 |
|
| 60 |
$this->objectClassResolver = ($objectClassResolver ?? static fn ($class) => \is_object($class) ? $class::class : $class)(...); |
| 61 |
$this->propertyInfoExtractor = $propertyInfoExtractor ?: new ReflectionExtractor(); |
| 62 |
$this->writeInfoExtractor = new ReflectionExtractor(); |
| 63 |
} |
| 64 |
|
| 65 |
public function getSupportedTypes(?string $format): array |
| 66 |
{ |
| 67 |
return ['object' => true]; |
| 68 |
} |
| 69 |
|
| 70 |
protected function extractAttributes(object $object, ?string $format = null, array $context = []): array |
| 71 |
{ |
| 72 |
if (\stdClass::class === $object::class) { |
| 73 |
return array_keys((array) $object); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
$attributes = []; |
| 78 |
|
| 79 |
|
| 80 |
$class = ($this->objectClassResolver)($object); |
| 81 |
$reflClass = new \ReflectionClass($class); |
| 82 |
|
| 83 |
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) { |
| 84 |
$name = $reflMethod->name; |
| 85 |
$attributeName = $this->getAttributeNameFromAccessor($reflClass, $reflMethod, false); |
| 86 |
|
| 87 |
if ($this->hasPropertyForAccessor($reflMethod->getDeclaringClass(), $name) && (null === $attributeName || $this->hasAttributeNameCollision($reflClass, $attributeName, $name))) { |
| 88 |
$attributeName = $name; |
| 89 |
} |
| 90 |
|
| 91 |
if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName, $format, $context)) { |
| 92 |
$attributes[$attributeName] = true; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
foreach ($reflClass->getProperties() as $reflProperty) { |
| 98 |
if (!$reflProperty->isPublic()) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) { |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
$attributes[$reflProperty->name] = true; |
| 107 |
} |
| 108 |
|
| 109 |
return array_keys($attributes); |
| 110 |
} |
| 111 |
|
| 112 |
protected function getAttributeValue(object $object, string $attribute, ?string $format = null, array $context = []): mixed |
| 113 |
{ |
| 114 |
$mapping = $this->classDiscriminatorResolver?->getMappingForMappedObject($object); |
| 115 |
|
| 116 |
return $attribute === $mapping?->getTypeProperty() |
| 117 |
? $mapping |
| 118 |
: $this->propertyAccessor->getValue($object, $attribute); |
| 119 |
} |
| 120 |
|
| 121 |
protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []): void |
| 122 |
{ |
| 123 |
try { |
| 124 |
$this->propertyAccessor->setValue($object, $attribute, $value); |
| 125 |
} catch (NoSuchPropertyException) { |
| 126 |
|
| 127 |
} catch (PropertyAccessInvalidArgumentException $e) { |
| 128 |
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Failed to denormalize attribute "%s" value for class "%s": %s', $attribute, $object::class, $e->getMessage()), $value, $e instanceof InvalidTypeException ? [$e->expectedType] : ['unknown'], $context['deserialization_path'] ?? null, false, $e->getCode(), $e); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []): bool |
| 133 |
{ |
| 134 |
if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
$class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject; |
| 139 |
|
| 140 |
if ($this->classDiscriminatorResolver?->getMappingForMappedObject($classOrObject)?->getTypeProperty() === $attribute) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
if ($context['_read_attributes'] ?? true) { |
| 145 |
$context = array_intersect_key($context, ['enable_getter_setter_extraction' => true, 'enable_magic_methods_extraction' => true]); |
| 146 |
$cacheKey = $class.$attribute.hash('xxh128', serialize($context)); |
| 147 |
|
| 148 |
return self::$isReadableCache[$cacheKey] ??= $this->propertyInfoExtractor->isReadable($class, $attribute, $context) || $this->hasAttributeAccessorMethod($class, $attribute) || (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute)); |
| 149 |
} |
| 150 |
|
| 151 |
$context = array_intersect_key($context, ['enable_getter_setter_extraction' => true, 'enable_magic_methods_extraction' => true, 'enable_constructor_extraction' => true, 'enable_adder_remover_extraction' => true]); |
| 152 |
$cacheKey = $class.$attribute.hash('xxh128', serialize($context)); |
| 153 |
|
| 154 |
if (isset(self::$isWritableCache[$cacheKey])) { |
| 155 |
return self::$isWritableCache[$cacheKey]; |
| 156 |
} |
| 157 |
|
| 158 |
if (str_contains($attribute, '.') || $this->propertyInfoExtractor->isWritable($class, $attribute, $context)) { |
| 159 |
return self::$isWritableCache[$cacheKey] = true; |
| 160 |
} |
| 161 |
|
| 162 |
$writeType = $this->writeInfoExtractor->getWriteInfo($class, $attribute, $context)?->getType(); |
| 163 |
|
| 164 |
return self::$isWritableCache[$cacheKey] = !\in_array($writeType, [null, PropertyWriteInfo::TYPE_NONE], true) |
| 165 |
&& (PropertyWriteInfo::TYPE_PROPERTY !== $writeType || !property_exists($class, $attribute)); |
| 166 |
} |
| 167 |
|
| 168 |
private function hasAttributeAccessorMethod(string $class, string $attribute): bool |
| 169 |
{ |
| 170 |
$reflection = self::$reflectionCache[$class] ??= new \ReflectionClass($class); |
| 171 |
|
| 172 |
if (!$reflection->hasMethod($attribute)) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
$method = $reflection->getMethod($attribute); |
| 177 |
|
| 178 |
return !$method->isStatic() |
| 179 |
&& !$method->getAttributes(Ignore::class) |
| 180 |
&& !$method->getNumberOfRequiredParameters() |
| 181 |
&& !\in_array((string) $method->getReturnType(), ['void', 'never'], true); |
| 182 |
} |
| 183 |
} |