comparison vendor/symfony/serializer/Normalizer/AbstractNormalizer.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
328 if (null !== $object = $this->extractObjectToPopulate($class, $context, static::OBJECT_TO_POPULATE)) { 328 if (null !== $object = $this->extractObjectToPopulate($class, $context, static::OBJECT_TO_POPULATE)) {
329 unset($context[static::OBJECT_TO_POPULATE]); 329 unset($context[static::OBJECT_TO_POPULATE]);
330 330
331 return $object; 331 return $object;
332 } 332 }
333 // clean up even if no match
334 unset($context[static::OBJECT_TO_POPULATE]);
333 335
334 $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes); 336 $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
335 if ($constructor) { 337 if ($constructor) {
338 if (true !== $constructor->isPublic()) {
339 return $reflectionClass->newInstanceWithoutConstructor();
340 }
341
336 $constructorParameters = $constructor->getParameters(); 342 $constructorParameters = $constructor->getParameters();
337 343
338 $params = []; 344 $params = [];
339 foreach ($constructorParameters as $constructorParameter) { 345 foreach ($constructorParameters as $constructorParameter) {
340 $paramName = $constructorParameter->name; 346 $paramName = $constructorParameter->name;
341 $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName; 347 $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
342 348
343 $allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes); 349 $allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
344 $ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context); 350 $ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
345 if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) { 351 if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
346 if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) { 352 if ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
347 if (!\is_array($data[$paramName])) { 353 if (!\is_array($data[$paramName])) {
348 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)); 354 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));
349 } 355 }
350 356
351 $params = array_merge($params, $data[$paramName]); 357 $params = array_merge($params, $data[$paramName]);
352 } 358 }
353 } elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) { 359 } elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
354 $parameterData = $data[$key]; 360 $parameterData = $data[$key];
355 if (null === $parameterData && $constructorParameter->allowsNull()) { 361 if (null === $parameterData && $constructorParameter->allowsNull()) {
356 $params[] = null; 362 $params[] = null;
357 // Don't run set for a parameter passed to the constructor 363 // Don't run set for a parameter passed to the constructor
358 unset($data[$key]); 364 unset($data[$key]);
389 if (!$this->serializer instanceof DenormalizerInterface) { 395 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)); 396 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 } 397 }
392 $parameterClass = $parameter->getClass()->getName(); 398 $parameterClass = $parameter->getClass()->getName();
393 399
394 return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName)); 400 return $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $parameterName, $format));
395 } 401 }
396 402
397 return $parameterData; 403 return $parameterData;
398 } catch (\ReflectionException $e) { 404 } catch (\ReflectionException $e) {
399 throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e); 405 throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e);
400 } 406 }
401 } 407 }
402 408
403 /** 409 /**
404 * @param array $parentContext 410 * @param array $parentContext
405 * @param string $attribute 411 * @param string $attribute Attribute name
412 * @param string|null $format
406 * 413 *
407 * @return array 414 * @return array
408 * 415 *
409 * @internal 416 * @internal
410 */ 417 */
411 protected function createChildContext(array $parentContext, $attribute) 418 protected function createChildContext(array $parentContext, $attribute/*, string $format = null */)
412 { 419 {
413 if (isset($parentContext[self::ATTRIBUTES][$attribute])) { 420 if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
414 $parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute]; 421 $parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];
415 } else { 422 } else {
416 unset($parentContext[self::ATTRIBUTES]); 423 unset($parentContext[self::ATTRIBUTES]);