Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::denormalize PHP Метод

denormalize() публичный Метод

public denormalize ( $data, $class, $format = null, array $context = [] )
$context array
    public function denormalize($data, $class, $format = null, array $context = array())
    {
        if (!isset($context['cache_key'])) {
            $context['cache_key'] = $this->getCacheKey($format, $context);
        }
        $allowedAttributes = $this->getAllowedAttributes($class, $context, true);
        $normalizedData = $this->prepareForDenormalization($data);
        $extraAttributes = array();
        $reflectionClass = new \ReflectionClass($class);
        $object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
        foreach ($normalizedData as $attribute => $value) {
            if ($this->nameConverter) {
                $attribute = $this->nameConverter->denormalize($attribute);
            }
            if ($allowedAttributes !== false && !in_array($attribute, $allowedAttributes) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
                if (isset($context['allow_extra_attributes']) && !$context['allow_extra_attributes']) {
                    $extraAttributes[] = $attribute;
                }
                continue;
            }
            $value = $this->validateAndDenormalize($class, $attribute, $value, $format, $context);
            try {
                $this->setAttributeValue($object, $attribute, $value, $format, $context);
            } catch (InvalidArgumentException $e) {
                throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
            }
        }
        if (!empty($extraAttributes)) {
            throw new ExtraAttributesException($extraAttributes);
        }
        return $object;
    }

Usage Example

Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     $context['api_denormalize'] = true;
     if (!isset($context['resource_class'])) {
         $context['resource_class'] = $class;
     }
     return parent::denormalize($data, $class, $format, $context);
 }