| 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\UninitializedPropertyException; |
| 15 |
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; |
| 16 |
use Symfony\Component\Serializer\Exception\LogicException; |
| 17 |
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
| 18 |
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface; |
| 19 |
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
| 20 |
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
> |
| 38 |
|
| 39 |
final class PropertyNormalizer extends AbstractObjectNormalizer |
| 40 |
{ |
| 41 |
public const NORMALIZE_PUBLIC = 1; |
| 42 |
public const NORMALIZE_PROTECTED = 2; |
| 43 |
public const NORMALIZE_PRIVATE = 4; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public const NORMALIZE_VISIBILITY = 'normalize_visibility'; |
| 49 |
|
| 50 |
public function __construct(?ClassMetadataFactoryInterface $classMetadataFactory = null, ?NameConverterInterface $nameConverter = null, ?PropertyTypeExtractorInterface $propertyTypeExtractor = null, ?ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, ?callable $objectClassResolver = null, array $defaultContext = []) |
| 51 |
{ |
| 52 |
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext); |
| 53 |
|
| 54 |
if (!isset($this->defaultContext[self::NORMALIZE_VISIBILITY])) { |
| 55 |
$this->defaultContext[self::NORMALIZE_VISIBILITY] = self::NORMALIZE_PUBLIC | self::NORMALIZE_PROTECTED | self::NORMALIZE_PRIVATE; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function getSupportedTypes(?string $format): array |
| 60 |
{ |
| 61 |
return ['object' => true]; |
| 62 |
} |
| 63 |
|
| 64 |
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 65 |
{ |
| 66 |
return parent::supportsNormalization($data, $format) && $this->supports($data::class); |
| 67 |
} |
| 68 |
|
| 69 |
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool |
| 70 |
{ |
| 71 |
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private function supports(string $class): bool |
| 78 |
{ |
| 79 |
if ($this->classDiscriminatorResolver?->getMappingForClass($class)) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
$class = new \ReflectionClass($class); |
| 84 |
|
| 85 |
|
| 86 |
do { |
| 87 |
foreach ($class->getProperties() as $property) { |
| 88 |
if (!$property->isStatic()) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
} |
| 92 |
} while ($class = $class->getParentClass()); |
| 93 |
|
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
protected function isAllowedAttribute(object|string $classOrObject, string $attribute, ?string $format = null, array $context = []): bool |
| 98 |
{ |
| 99 |
if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
if ($this->classDiscriminatorResolver?->getMappingForMappedObject($classOrObject)?->getTypeProperty() === $attribute) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
try { |
| 108 |
$reflectionProperty = $this->getReflectionProperty($classOrObject, $attribute); |
| 109 |
} catch (\ReflectionException) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
if ($reflectionProperty->isStatic()) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
$normalizeVisibility = $context[self::NORMALIZE_VISIBILITY] ?? $this->defaultContext[self::NORMALIZE_VISIBILITY]; |
| 118 |
|
| 119 |
if ((self::NORMALIZE_PUBLIC & $normalizeVisibility) && $reflectionProperty->isPublic()) { |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
if ((self::NORMALIZE_PROTECTED & $normalizeVisibility) && $reflectionProperty->isProtected()) { |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
if ((self::NORMALIZE_PRIVATE & $normalizeVisibility) && $reflectionProperty->isPrivate()) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
protected function extractAttributes(object $object, ?string $format = null, array $context = []): array |
| 135 |
{ |
| 136 |
$reflectionObject = new \ReflectionObject($object); |
| 137 |
$attributes = []; |
| 138 |
|
| 139 |
do { |
| 140 |
foreach ($reflectionObject->getProperties() as $property) { |
| 141 |
if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
|
| 145 |
$attributes[] = $property->name; |
| 146 |
} |
| 147 |
} while ($reflectionObject = $reflectionObject->getParentClass()); |
| 148 |
|
| 149 |
return array_unique($attributes); |
| 150 |
} |
| 151 |
|
| 152 |
protected function getAttributeValue(object $object, string $attribute, ?string $format = null, array $context = []): mixed |
| 153 |
{ |
| 154 |
try { |
| 155 |
$reflectionProperty = $this->getReflectionProperty($object, $attribute); |
| 156 |
} catch (\ReflectionException) { |
| 157 |
return null; |
| 158 |
} |
| 159 |
|
| 160 |
if ($reflectionProperty->hasType()) { |
| 161 |
if (!$reflectionProperty->isInitialized($object)) { |
| 162 |
throw new UninitializedPropertyException(\sprintf('The property "%s::$%s" is not initialized.', $object::class, $reflectionProperty->name)); |
| 163 |
} |
| 164 |
|
| 165 |
return $reflectionProperty->getValue($object); |
| 166 |
} |
| 167 |
|
| 168 |
if (!method_exists($object, '__get') && !isset($object->$attribute)) { |
| 169 |
$propertyValues = (array) $object; |
| 170 |
|
| 171 |
if (($reflectionProperty->isPublic() && !\array_key_exists($reflectionProperty->name, $propertyValues)) |
| 172 |
|| ($reflectionProperty->isProtected() && !\array_key_exists("\0*\0{$reflectionProperty->name}", $propertyValues)) |
| 173 |
|| ($reflectionProperty->isPrivate() && !\array_key_exists("\0{$reflectionProperty->class}\0{$reflectionProperty->name}", $propertyValues)) |
| 174 |
) { |
| 175 |
throw new UninitializedPropertyException(\sprintf('The property "%s::$%s" is not initialized.', $object::class, $reflectionProperty->name)); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return $reflectionProperty->getValue($object); |
| 180 |
} |
| 181 |
|
| 182 |
protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []): void |
| 183 |
{ |
| 184 |
try { |
| 185 |
$reflectionProperty = $this->getReflectionProperty($object, $attribute); |
| 186 |
} catch (\ReflectionException) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
if ($reflectionProperty->isStatic()) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
try { |
| 195 |
if (!$reflectionProperty->isReadOnly()) { |
| 196 |
$reflectionProperty->setValue($object, $value); |
| 197 |
|
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
if (!$reflectionProperty->isInitialized($object)) { |
| 202 |
$declaringClass = $reflectionProperty->getDeclaringClass(); |
| 203 |
$declaringClass->getProperty($reflectionProperty->getName())->setValue($object, $value); |
| 204 |
|
| 205 |
return; |
| 206 |
} |
| 207 |
} catch (\TypeError $e) { |
| 208 |
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Failed to denormalize attribute "%s" value for class "%s": %s', $attribute, $object::class, $e->getMessage()), $value, ['unknown'], $context['deserialization_path'] ?? null, false, $e->getCode(), $e); |
| 209 |
} |
| 210 |
|
| 211 |
if ($reflectionProperty->getValue($object) !== $value) { |
| 212 |
throw new LogicException(\sprintf('Attempting to change readonly property "%s"::$%s.', $object::class, $reflectionProperty->getName())); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
private function getReflectionProperty(string|object $classOrObject, string $attribute): \ReflectionProperty |
| 220 |
{ |
| 221 |
$reflectionClass = new \ReflectionClass($classOrObject); |
| 222 |
while (true) { |
| 223 |
try { |
| 224 |
return $reflectionClass->getProperty($attribute); |
| 225 |
} catch (\ReflectionException $e) { |
| 226 |
if (!$reflectionClass = $reflectionClass->getParentClass()) { |
| 227 |
throw $e; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |