| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Normalizer; |
| 13 |
|
| 14 |
use Symfony\Component\Serializer\Exception\CircularReferenceException; |
| 15 |
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
| 16 |
use Symfony\Component\Serializer\Exception\LogicException; |
| 17 |
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException; |
| 18 |
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
| 19 |
use Symfony\Component\Serializer\Exception\RuntimeException; |
| 20 |
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; |
| 21 |
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
| 22 |
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
| 23 |
use Symfony\Component\Serializer\SerializerAwareInterface; |
| 24 |
use Symfony\Component\Serializer\SerializerAwareTrait; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
> |
| 30 |
|
| 31 |
abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface |
| 32 |
{ |
| 33 |
use ObjectToPopulateTrait; |
| 34 |
use SerializerAwareTrait; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit'; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public const OBJECT_TO_POPULATE = 'object_to_populate'; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public const GROUPS = 'groups'; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public const ATTRIBUTES = 'attributes'; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes'; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public const DEFAULT_CONSTRUCTOR_ARGUMENTS = 'default_constructor_arguments'; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public const CALLBACKS = 'callbacks'; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public const CIRCULAR_REFERENCE_HANDLER = 'circular_reference_handler'; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public const IGNORED_ATTRIBUTES = 'ignored_attributes'; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public const REQUIRE_ALL_PROPERTIES = 'require_all_properties'; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public const FILTER_BOOL = 'filter_bool'; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
protected const CIRCULAR_REFERENCE_LIMIT_COUNTERS = 'circular_reference_limit_counters'; |
| 135 |
|
| 136 |
protected array $defaultContext = [ |
| 137 |
self::ALLOW_EXTRA_ATTRIBUTES => true, |
| 138 |
self::CIRCULAR_REFERENCE_HANDLER => null, |
| 139 |
self::CIRCULAR_REFERENCE_LIMIT => 1, |
| 140 |
self::IGNORED_ATTRIBUTES => [], |
| 141 |
]; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function __construct( |
| 147 |
protected ?ClassMetadataFactoryInterface $classMetadataFactory = null, |
| 148 |
protected ?NameConverterInterface $nameConverter = null, |
| 149 |
array $defaultContext = [], |
| 150 |
) { |
| 151 |
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); |
| 152 |
|
| 153 |
$this->validateCallbackContext($this->defaultContext, 'default'); |
| 154 |
|
| 155 |
if (isset($this->defaultContext[self::CIRCULAR_REFERENCE_HANDLER]) && !\is_callable($this->defaultContext[self::CIRCULAR_REFERENCE_HANDLER])) { |
| 156 |
throw new InvalidArgumentException(\sprintf('Invalid callback found in the "%s" default context option.', self::CIRCULAR_REFERENCE_HANDLER)); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
protected function isCircularReference(object $object, array &$context): bool |
| 166 |
{ |
| 167 |
$objectId = spl_object_id($object); |
| 168 |
|
| 169 |
$circularReferenceLimit = $context[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT]; |
| 170 |
if (isset($context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId])) { |
| 171 |
if ($context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId] >= $circularReferenceLimit) { |
| 172 |
unset($context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId]); |
| 173 |
|
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
++$context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId]; |
| 178 |
} else { |
| 179 |
$context[self::CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectId] = 1; |
| 180 |
} |
| 181 |
|
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
protected function handleCircularReference(object $object, ?string $format = null, array $context = []): mixed |
| 196 |
{ |
| 197 |
$circularReferenceHandler = $context[self::CIRCULAR_REFERENCE_HANDLER] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_HANDLER]; |
| 198 |
if ($circularReferenceHandler) { |
| 199 |
return $circularReferenceHandler($object, $format, $context); |
| 200 |
} |
| 201 |
|
| 202 |
throw new CircularReferenceException(\sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d).', get_debug_type($object), $context[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT])); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool |
| 215 |
{ |
| 216 |
$allowExtraAttributes = $context[self::ALLOW_EXTRA_ATTRIBUTES] ?? $this->defaultContext[self::ALLOW_EXTRA_ATTRIBUTES]; |
| 217 |
if (!$this->classMetadataFactory) { |
| 218 |
if (!$allowExtraAttributes) { |
| 219 |
throw new LogicException(\sprintf('A class metadata factory must be provided in the constructor when setting "%s" to false.', self::ALLOW_EXTRA_ATTRIBUTES)); |
| 220 |
} |
| 221 |
|
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
$groups = $this->getGroups($context); |
| 226 |
|
| 227 |
$allowedAttributes = []; |
| 228 |
$ignoreUsed = false; |
| 229 |
|
| 230 |
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) { |
| 231 |
if ($ignore = $attributeMetadata->isIgnored()) { |
| 232 |
$ignoreUsed = true; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
if ( |
| 237 |
!$ignore |
| 238 |
&& ([] === $groups || \in_array('*', $groups, true) || array_intersect($attributeMetadata->getGroups(), $groups)) |
| 239 |
&& $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context) |
| 240 |
) { |
| 241 |
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if (!$ignoreUsed && $allowExtraAttributes) { |
| 246 |
if ([] === $groups) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
if ([] === $allowedAttributes && \in_array('*', $groups, true)) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return $allowedAttributes; |
| 256 |
} |
| 257 |
|
| 258 |
protected function getGroups(array $context): array |
| 259 |
{ |
| 260 |
$groups = $context[self::GROUPS] ?? $this->defaultContext[self::GROUPS] ?? []; |
| 261 |
|
| 262 |
return \is_scalar($groups) ? (array) $groups : $groups; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
protected function isAllowedAttribute(object|string $classOrObject, string $attribute, ?string $format = null, array $context = []): bool |
| 269 |
{ |
| 270 |
$ignoredAttributes = $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES]; |
| 271 |
if (\in_array($attribute, $ignoredAttributes, true)) { |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
$attributes = $context[self::ATTRIBUTES] ?? $this->defaultContext[self::ATTRIBUTES] ?? null; |
| 276 |
if (isset($attributes[$attribute])) { |
| 277 |
|
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
if (\is_array($attributes)) { |
| 282 |
return \in_array($attribute, $attributes, true); |
| 283 |
} |
| 284 |
|
| 285 |
return true; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
protected function prepareForDenormalization(mixed $data): array |
| 293 |
{ |
| 294 |
return (array) $data; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
protected function getConstructor(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes): ?\ReflectionMethod |
| 302 |
{ |
| 303 |
return $reflectionClass->getConstructor(); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, ?string $format = null): object |
| 318 |
{ |
| 319 |
if (null !== $object = $this->extractObjectToPopulate($class, $context, self::OBJECT_TO_POPULATE)) { |
| 320 |
unset($context[self::OBJECT_TO_POPULATE]); |
| 321 |
|
| 322 |
return $object; |
| 323 |
} |
| 324 |
|
| 325 |
unset($context[static::OBJECT_TO_POPULATE]); |
| 326 |
|
| 327 |
$constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes); |
| 328 |
if ($constructor) { |
| 329 |
if (true !== $constructor->isPublic()) { |
| 330 |
return $reflectionClass->newInstanceWithoutConstructor(); |
| 331 |
} |
| 332 |
|
| 333 |
$constructorParameters = $constructor->getParameters(); |
| 334 |
$missingConstructorArguments = []; |
| 335 |
$params = []; |
| 336 |
$unsetKeys = []; |
| 337 |
$collectedErrorCountBeforeConstructor = \count($context['not_normalizable_value_exceptions'] ?? []); |
| 338 |
|
| 339 |
foreach ($constructorParameters as $constructorParameter) { |
| 340 |
$paramName = $constructorParameter->name; |
| 341 |
$attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context); |
| 342 |
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName; |
| 343 |
|
| 344 |
$allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes, true); |
| 345 |
$ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context); |
| 346 |
if ($constructorParameter->isVariadic()) { |
| 347 |
if ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) { |
| 348 |
if (!\is_array($data[$key])) { |
| 349 |
throw new RuntimeException(\sprintf('Cannot create an instance of "%s" from serialized data because the variadic parameter "%s" can only accept an array.', $class, $constructorParameter->name)); |
| 350 |
} |
| 351 |
|
| 352 |
$variadicParameters = []; |
| 353 |
foreach ($data[$key] as $parameterKey => $parameterData) { |
| 354 |
try { |
| 355 |
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format); |
| 356 |
} catch (NotNormalizableValueException $exception) { |
| 357 |
if (!isset($context['not_normalizable_value_exceptions'])) { |
| 358 |
throw $exception; |
| 359 |
} |
| 360 |
|
| 361 |
$context['not_normalizable_value_exceptions'][] = $exception; |
| 362 |
$params[$paramName] = $parameterData; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
$params = array_merge(array_values($params), $variadicParameters); |
| 367 |
$unsetKeys[] = $key; |
| 368 |
} |
| 369 |
} elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) { |
| 370 |
$parameterData = $data[$key]; |
| 371 |
if (null === $parameterData && $constructorParameter->allowsNull()) { |
| 372 |
$params[$paramName] = null; |
| 373 |
$unsetKeys[] = $key; |
| 374 |
|
| 375 |
continue; |
| 376 |
} |
| 377 |
|
| 378 |
try { |
| 379 |
$params[$paramName] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format); |
| 380 |
} catch (NotNormalizableValueException $exception) { |
| 381 |
if (!isset($context['not_normalizable_value_exceptions'])) { |
| 382 |
throw $exception; |
| 383 |
} |
| 384 |
|
| 385 |
$context['not_normalizable_value_exceptions'][] = $exception; |
| 386 |
$params[$paramName] = $parameterData; |
| 387 |
} |
| 388 |
|
| 389 |
$unsetKeys[] = $key; |
| 390 |
} elseif (\array_key_exists($key, $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) { |
| 391 |
$params[$paramName] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key]; |
| 392 |
} elseif (\array_key_exists($key, $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) { |
| 393 |
$params[$paramName] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key]; |
| 394 |
} elseif ($constructorParameter->isDefaultValueAvailable()) { |
| 395 |
$params[$paramName] = $constructorParameter->getDefaultValue(); |
| 396 |
} elseif (!($context[self::REQUIRE_ALL_PROPERTIES] ?? $this->defaultContext[self::REQUIRE_ALL_PROPERTIES] ?? false) && $constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) { |
| 397 |
$params[$paramName] = null; |
| 398 |
} else { |
| 399 |
if (!isset($context['not_normalizable_value_exceptions'])) { |
| 400 |
$missingConstructorArguments[] = $constructorParameter->name; |
| 401 |
continue; |
| 402 |
} |
| 403 |
|
| 404 |
$constructorParameterTypes = []; |
| 405 |
$reflectionType = $constructorParameter->getType(); |
| 406 |
if ($reflectionType instanceof \ReflectionUnionType) { |
| 407 |
foreach ($reflectionType->getTypes() as $reflectionType) { |
| 408 |
$constructorParameterTypes[] = (string) $reflectionType; |
| 409 |
} |
| 410 |
} elseif ($reflectionType instanceof \ReflectionType) { |
| 411 |
$constructorParameterTypes[] = (string) $reflectionType; |
| 412 |
} else { |
| 413 |
$constructorParameterTypes[] = 'unknown'; |
| 414 |
} |
| 415 |
|
| 416 |
$exception = NotNormalizableValueException::createForUnexpectedDataType( |
| 417 |
\sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name), |
| 418 |
null, |
| 419 |
$constructorParameterTypes, |
| 420 |
$attributeContext['deserialization_path'] ?? null, |
| 421 |
true |
| 422 |
); |
| 423 |
$context['not_normalizable_value_exceptions'][] = $exception; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
if ($missingConstructorArguments) { |
| 428 |
throw new MissingConstructorArgumentsException(\sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "$%s".', $class, implode('", "$', $missingConstructorArguments)), 0, null, $missingConstructorArguments, $class); |
| 429 |
} |
| 430 |
|
| 431 |
if (!$constructor->isConstructor()) { |
| 432 |
$instance = $constructor->invokeArgs(null, $params); |
| 433 |
|
| 434 |
|
| 435 |
foreach ($unsetKeys as $key) { |
| 436 |
unset($data[$key]); |
| 437 |
} |
| 438 |
|
| 439 |
return $instance; |
| 440 |
} |
| 441 |
|
| 442 |
try { |
| 443 |
$instance = $reflectionClass->newInstanceArgs($params); |
| 444 |
|
| 445 |
|
| 446 |
foreach ($unsetKeys as $key) { |
| 447 |
unset($data[$key]); |
| 448 |
} |
| 449 |
|
| 450 |
return $instance; |
| 451 |
} catch (\TypeError $e) { |
| 452 |
if (!isset($context['not_normalizable_value_exceptions'])) { |
| 453 |
throw $e; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
if (\count($context['not_normalizable_value_exceptions']) === $collectedErrorCountBeforeConstructor) { |
| 461 |
$context['not_normalizable_value_exceptions'][] = NotNormalizableValueException::createForUnexpectedDataType( |
| 462 |
\sprintf('Failed to create an instance of "%s" from constructor: %s', $class, $e->getMessage()), |
| 463 |
null, |
| 464 |
['unknown'], |
| 465 |
$context['deserialization_path'] ?? null, |
| 466 |
false, |
| 467 |
0, |
| 468 |
$e, |
| 469 |
); |
| 470 |
} |
| 471 |
|
| 472 |
return $reflectionClass->newInstanceWithoutConstructor(); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
if (!$reflectionClass->isInstantiable()) { |
| 477 |
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Failed to create object because the class "%s" is not instantiable.', $class), $data, ['unknown'], $context['deserialization_path'] ?? null); |
| 478 |
} |
| 479 |
|
| 480 |
return new $class(); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, string $parameterName, mixed $parameterData, array $context, ?string $format = null): mixed |
| 487 |
{ |
| 488 |
try { |
| 489 |
if (($parameterType = $parameter->getType()) instanceof \ReflectionNamedType && !$parameterType->isBuiltin()) { |
| 490 |
$parameterClass = $parameterType->getName(); |
| 491 |
new \ReflectionClass($parameterClass); |
| 492 |
|
| 493 |
if (!$this->serializer instanceof DenormalizerInterface) { |
| 494 |
throw new LogicException(\sprintf('Cannot create an instance of "%s" from serialized data because the serializer inject in "%s" is not a denormalizer.', $parameterClass, static::class)); |
| 495 |
} |
| 496 |
|
| 497 |
$parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName, $format)); |
| 498 |
} |
| 499 |
} catch (\ReflectionException $e) { |
| 500 |
throw new RuntimeException(\sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e); |
| 501 |
} catch (MissingConstructorArgumentsException $e) { |
| 502 |
if (!$parameter->getType()->allowsNull()) { |
| 503 |
throw $e; |
| 504 |
} |
| 505 |
|
| 506 |
return null; |
| 507 |
} |
| 508 |
|
| 509 |
$parameterData = $this->applyCallbacks($parameterData, $class->getName(), $parameterName, $format, $context); |
| 510 |
|
| 511 |
return $this->applyFilterBool($parameter, $parameterData, $context); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array |
| 518 |
{ |
| 519 |
if (isset($parentContext[self::ATTRIBUTES][$attribute])) { |
| 520 |
$parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute]; |
| 521 |
} else { |
| 522 |
unset($parentContext[self::ATTRIBUTES]); |
| 523 |
} |
| 524 |
|
| 525 |
return $parentContext; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
final protected function validateCallbackContext(array $context, string $contextType = ''): void |
| 536 |
{ |
| 537 |
if (!isset($context[self::CALLBACKS])) { |
| 538 |
return; |
| 539 |
} |
| 540 |
|
| 541 |
if (!\is_array($context[self::CALLBACKS])) { |
| 542 |
throw new InvalidArgumentException(\sprintf('The "%s"%s context option must be an array of callables.', self::CALLBACKS, '' !== $contextType ? " $contextType" : '')); |
| 543 |
} |
| 544 |
|
| 545 |
foreach ($context[self::CALLBACKS] as $attribute => $callback) { |
| 546 |
if (!\is_callable($callback)) { |
| 547 |
throw new InvalidArgumentException(\sprintf('Invalid callback found for attribute "%s" in the "%s"%s context option.', $attribute, self::CALLBACKS, '' !== $contextType ? " $contextType" : '')); |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
final protected function applyCallbacks(mixed $value, object|string $object, string $attribute, ?string $format, array $context): mixed |
| 553 |
{ |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
$callback = $context[self::CALLBACKS][$attribute] ?? $this->defaultContext[self::CALLBACKS][$attribute] ?? null; |
| 558 |
|
| 559 |
return $callback ? $callback($value, $object, $attribute, $format, $context) : $value; |
| 560 |
} |
| 561 |
|
| 562 |
final protected function applyFilterBool(\ReflectionParameter $parameter, mixed $value, array $context): mixed |
| 563 |
{ |
| 564 |
if (!($context[self::FILTER_BOOL] ?? false)) { |
| 565 |
return $value; |
| 566 |
} |
| 567 |
|
| 568 |
if (!($parameterType = $parameter->getType()) instanceof \ReflectionNamedType || 'bool' !== $parameterType->getName()) { |
| 569 |
return $value; |
| 570 |
} |
| 571 |
|
| 572 |
return filter_var($value, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE) ?? $value; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
protected function getAttributeNormalizationContext(object $object, string $attribute, array $context): array |
| 581 |
{ |
| 582 |
if (null === $metadata = $this->getAttributeMetadata($object, $attribute)) { |
| 583 |
return $context; |
| 584 |
} |
| 585 |
|
| 586 |
return array_merge($context, $metadata->getNormalizationContextForGroups($this->getGroups($context))); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
protected function getAttributeDenormalizationContext(string $class, string $attribute, array $context): array |
| 595 |
{ |
| 596 |
$context['deserialization_path'] = ($context['deserialization_path'] ?? false) ? $context['deserialization_path'].'.'.$attribute : $attribute; |
| 597 |
|
| 598 |
if (null === $metadata = $this->getAttributeMetadata($class, $attribute)) { |
| 599 |
return $context; |
| 600 |
} |
| 601 |
|
| 602 |
return array_merge($context, $metadata->getDenormalizationContextForGroups($this->getGroups($context))); |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
protected function getAttributeMetadata(object|string $objectOrClass, string $attribute): ?AttributeMetadataInterface |
| 609 |
{ |
| 610 |
if (!$this->classMetadataFactory) { |
| 611 |
return null; |
| 612 |
} |
| 613 |
|
| 614 |
return $this->classMetadataFactory->getMetadataFor($objectOrClass)->getAttributesMetadata()[$attribute] ?? null; |
| 615 |
} |
| 616 |
} |