comparison vendor/symfony/serializer/Normalizer/AbstractNormalizer.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
13 13
14 use Symfony\Component\Serializer\Exception\CircularReferenceException; 14 use Symfony\Component\Serializer\Exception\CircularReferenceException;
15 use Symfony\Component\Serializer\Exception\InvalidArgumentException; 15 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16 use Symfony\Component\Serializer\Exception\LogicException; 16 use Symfony\Component\Serializer\Exception\LogicException;
17 use Symfony\Component\Serializer\Exception\RuntimeException; 17 use Symfony\Component\Serializer\Exception\RuntimeException;
18 use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
18 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; 19 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
19 use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
20 use Symfony\Component\Serializer\NameConverter\NameConverterInterface; 20 use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
21 use Symfony\Component\Serializer\SerializerAwareInterface; 21 use Symfony\Component\Serializer\SerializerAwareInterface;
22 22
23 /** 23 /**
24 * Normalizer implementation. 24 * Normalizer implementation.
56 protected $nameConverter; 56 protected $nameConverter;
57 57
58 /** 58 /**
59 * @var array 59 * @var array
60 */ 60 */
61 protected $callbacks = array(); 61 protected $callbacks = [];
62 62
63 /** 63 /**
64 * @var array 64 * @var array
65 */ 65 */
66 protected $ignoredAttributes = array(); 66 protected $ignoredAttributes = [];
67 67
68 /** 68 /**
69 * @var array 69 * @var array
70 */ 70 */
71 protected $camelizedAttributes = array(); 71 protected $camelizedAttributes = [];
72 72
73 /** 73 /**
74 * Sets the {@link ClassMetadataFactoryInterface} to use. 74 * Sets the {@link ClassMetadataFactoryInterface} to use.
75 */ 75 */
76 public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null) 76 public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
118 */ 118 */
119 public function setCallbacks(array $callbacks) 119 public function setCallbacks(array $callbacks)
120 { 120 {
121 foreach ($callbacks as $attribute => $callback) { 121 foreach ($callbacks as $attribute => $callback) {
122 if (!\is_callable($callback)) { 122 if (!\is_callable($callback)) {
123 throw new InvalidArgumentException(sprintf( 123 throw new InvalidArgumentException(sprintf('The given callback for attribute "%s" is not callable.', $attribute));
124 'The given callback for attribute "%s" is not callable.',
125 $attribute
126 ));
127 } 124 }
128 } 125 }
129 $this->callbacks = $callbacks; 126 $this->callbacks = $callbacks;
130 127
131 return $this; 128 return $this;
219 $groups = $context[static::GROUPS]; 216 $groups = $context[static::GROUPS];
220 } elseif (!isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) || $context[static::ALLOW_EXTRA_ATTRIBUTES]) { 217 } elseif (!isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) || $context[static::ALLOW_EXTRA_ATTRIBUTES]) {
221 return false; 218 return false;
222 } 219 }
223 220
224 $allowedAttributes = array(); 221 $allowedAttributes = [];
225 foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) { 222 foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
226 $name = $attributeMetadata->getName(); 223 $name = $attributeMetadata->getName();
227 224
228 if ( 225 if (
229 (false === $groups || array_intersect($attributeMetadata->getGroups(), $groups)) && 226 (false === $groups || array_intersect($attributeMetadata->getGroups(), $groups)) &&
244 * @param string|null $format 241 * @param string|null $format
245 * @param array $context 242 * @param array $context
246 * 243 *
247 * @return bool 244 * @return bool
248 */ 245 */
249 protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array()) 246 protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = [])
250 { 247 {
251 if (in_array($attribute, $this->ignoredAttributes)) { 248 if (\in_array($attribute, $this->ignoredAttributes)) {
252 return false; 249 return false;
253 } 250 }
254 251
255 if (isset($context[self::ATTRIBUTES][$attribute])) { 252 if (isset($context[self::ATTRIBUTES][$attribute])) {
256 // Nested attributes 253 // Nested attributes
257 return true; 254 return true;
258 } 255 }
259 256
260 if (isset($context[self::ATTRIBUTES]) && is_array($context[self::ATTRIBUTES])) { 257 if (isset($context[self::ATTRIBUTES]) && \is_array($context[self::ATTRIBUTES])) {
261 return in_array($attribute, $context[self::ATTRIBUTES], true); 258 return \in_array($attribute, $context[self::ATTRIBUTES], true);
262 } 259 }
263 260
264 return true; 261 return true;
265 } 262 }
266 263
319 $format = \func_get_arg(5); 316 $format = \func_get_arg(5);
320 } else { 317 } else {
321 if (__CLASS__ !== \get_class($this)) { 318 if (__CLASS__ !== \get_class($this)) {
322 $r = new \ReflectionMethod($this, __FUNCTION__); 319 $r = new \ReflectionMethod($this, __FUNCTION__);
323 if (__CLASS__ !== $r->getDeclaringClass()->getName()) { 320 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
324 @trigger_error(sprintf('Method %s::%s() will have a 6th `string $format = null` argument in version 4.0. Not defining it is deprecated since Symfony 3.2.', get_class($this), __FUNCTION__), E_USER_DEPRECATED); 321 @trigger_error(sprintf('Method %s::%s() will have a 6th `string $format = null` argument in version 4.0. Not defining it is deprecated since Symfony 3.2.', \get_class($this), __FUNCTION__), E_USER_DEPRECATED);
325 } 322 }
326 } 323 }
327 324
328 $format = null; 325 $format = null;
329 } 326 }
336 333
337 $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes); 334 $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
338 if ($constructor) { 335 if ($constructor) {
339 $constructorParameters = $constructor->getParameters(); 336 $constructorParameters = $constructor->getParameters();
340 337
341 $params = array(); 338 $params = [];
342 foreach ($constructorParameters as $constructorParameter) { 339 foreach ($constructorParameters as $constructorParameter) {
343 $paramName = $constructorParameter->name; 340 $paramName = $constructorParameter->name;
344 $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName; 341 $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
345 342
346 $allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes); 343 $allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
359 $params[] = null; 356 $params[] = null;
360 // Don't run set for a parameter passed to the constructor 357 // Don't run set for a parameter passed to the constructor
361 unset($data[$key]); 358 unset($data[$key]);
362 continue; 359 continue;
363 } 360 }
364 try {
365 if (null !== $constructorParameter->getClass()) {
366 if (!$this->serializer instanceof DenormalizerInterface) {
367 throw new LogicException(sprintf('Cannot create an instance of %s from serialized data because the serializer inject in "%s" is not a denormalizer', $constructorParameter->getClass(), static::class));
368 }
369 $parameterClass = $constructorParameter->getClass()->getName();
370 $parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $paramName));
371 }
372 } catch (\ReflectionException $e) {
373 throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
374 }
375 361
376 // Don't run set for a parameter passed to the constructor 362 // Don't run set for a parameter passed to the constructor
377 $params[] = $parameterData; 363 $params[] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
378 unset($data[$key]); 364 unset($data[$key]);
379 } elseif ($constructorParameter->isDefaultValueAvailable()) { 365 } elseif ($constructorParameter->isDefaultValueAvailable()) {
380 $params[] = $constructorParameter->getDefaultValue(); 366 $params[] = $constructorParameter->getDefaultValue();
381 } else { 367 } else {
382 throw new RuntimeException( 368 throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name));
383 sprintf(
384 'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.',
385 $class,
386 $constructorParameter->name
387 )
388 );
389 } 369 }
390 } 370 }
391 371
392 if ($constructor->isConstructor()) { 372 if ($constructor->isConstructor()) {
393 return $reflectionClass->newInstanceArgs($params); 373 return $reflectionClass->newInstanceArgs($params);
398 378
399 return new $class(); 379 return new $class();
400 } 380 }
401 381
402 /** 382 /**
383 * @internal
384 */
385 protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)
386 {
387 try {
388 if (null !== $parameter->getClass()) {
389 if (!$this->serializer instanceof DenormalizerInterface) {
390 throw new LogicException(sprintf('Cannot create an instance of %s from serialized data because the serializer inject in "%s" is not a denormalizer', $parameter->getClass(), static::class));
391 }
392 $parameterClass = $parameter->getClass()->getName();
393
394 return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName));
395 }
396
397 return $parameterData;
398 } catch (\ReflectionException $e) {
399 throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e);
400 }
401 }
402
403 /**
403 * @param array $parentContext 404 * @param array $parentContext
404 * @param string $attribute 405 * @param string $attribute
405 * 406 *
406 * @return array 407 * @return array
407 * 408 *