namespace Symfony\Component\Serializer\Normalizer;
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
-use Symfony\Component\Serializer\Encoder\JsonEncoder;
-use Symfony\Component\Serializer\Exception\LogicException;
-use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type;
+use Symfony\Component\Serializer\Encoder\JsonEncoder;
+use Symfony\Component\Serializer\Exception\ExtraAttributesException;
+use Symfony\Component\Serializer\Exception\LogicException;
+use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
{
const ENABLE_MAX_DEPTH = 'enable_max_depth';
const DEPTH_KEY_PATTERN = 'depth_%s::%s';
+ const DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement';
private $propertyTypeExtractor;
private $attributesCache = array();
+ private $cache = array();
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)
{
*/
public function supportsNormalization($data, $format = null)
{
- return is_object($data) && !$data instanceof \Traversable;
+ return \is_object($data) && !$data instanceof \Traversable;
}
/**
$data = array();
$stack = array();
$attributes = $this->getAttributes($object, $format, $context);
- $class = get_class($object);
+ $class = \get_class($object);
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
foreach ($attributes as $attribute) {
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
if (isset($this->callbacks[$attribute])) {
- $attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
+ $attributeValue = \call_user_func($this->callbacks[$attribute], $attributeValue);
}
if (null !== $attributeValue && !is_scalar($attributeValue)) {
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute));
}
- $data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $context));
+ $data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $this->createChildContext($context, $attribute)));
}
return $data;
*/
protected function getAttributes($object, $format = null, array $context)
{
- $class = get_class($object);
+ $class = \get_class($object);
$key = $class.'-'.$context['cache_key'];
if (isset($this->attributesCache[$key])) {
return $allowedAttributes;
}
+ if (isset($context['attributes'])) {
+ return $this->extractAttributes($object, $format, $context);
+ }
+
if (isset($this->attributesCache[$class])) {
return $this->attributesCache[$class];
}
*/
public function supportsDenormalization($data, $type, $format = null)
{
- return class_exists($type);
+ return isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = class_exists($type);
}
/**
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);
$attribute = $this->nameConverter->denormalize($attribute);
}
- if (($allowedAttributes !== false && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
+ if ((false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
+ if (isset($context[self::ALLOW_EXTRA_ATTRIBUTES]) && !$context[self::ALLOW_EXTRA_ATTRIBUTES]) {
+ $extraAttributes[] = $attribute;
+ }
+
continue;
}
try {
$this->setAttributeValue($object, $attribute, $value, $format, $context);
} catch (InvalidArgumentException $e) {
- throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
+ throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
}
}
+ if (!empty($extraAttributes)) {
+ throw new ExtraAttributesException($extraAttributes);
+ }
+
return $object;
}
*
* @return mixed
*
- * @throws UnexpectedValueException
+ * @throws NotNormalizableValueException
* @throws LogicException
*/
private function validateAndDenormalize($currentClass, $attribute, $data, $format, array $context)
$builtinType = Type::BUILTIN_TYPE_OBJECT;
$class = $collectionValueType->getClassName().'[]';
+ // Fix a collection that contains the only one element
+ // This is special to xml format only
+ if ('xml' === $format && !\is_int(key($data))) {
+ $data = array($data);
+ }
+
if (null !== $collectionKeyType = $type->getCollectionKeyType()) {
$context['key_type'] = $collectionKeyType;
}
throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class));
}
- if ($this->serializer->supportsDenormalization($data, $class, $format)) {
- return $this->serializer->denormalize($data, $class, $format, $context);
+ $childContext = $this->createChildContext($context, $attribute);
+ if ($this->serializer->supportsDenormalization($data, $class, $format, $childContext)) {
+ return $this->serializer->denormalize($data, $class, $format, $childContext);
}
}
// PHP's json_decode automatically converts Numbers without a decimal part to integers.
// To circumvent this behavior, integers are converted to floats when denormalizing JSON based formats and when
// a float is expected.
- if (Type::BUILTIN_TYPE_FLOAT === $builtinType && is_int($data) && false !== strpos($format, JsonEncoder::FORMAT)) {
+ if (Type::BUILTIN_TYPE_FLOAT === $builtinType && \is_int($data) && false !== strpos($format, JsonEncoder::FORMAT)) {
return (float) $data;
}
- if (call_user_func('is_'.$builtinType, $data)) {
+ if (\call_user_func('is_'.$builtinType, $data)) {
return $data;
}
}
- throw new UnexpectedValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), gettype($data)));
+ if (!empty($context[self::DISABLE_TYPE_ENFORCEMENT])) {
+ return $data;
+ }
+
+ throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), \gettype($data)));
}
/**
* Sets an attribute and apply the name converter if necessary.
*
- * @param array $data
* @param string $attribute
* @param mixed $attributeValue
*
{
if (
!isset($context[static::ENABLE_MAX_DEPTH]) ||
+ !$context[static::ENABLE_MAX_DEPTH] ||
!isset($attributesMetadata[$attribute]) ||
null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
) {