*/
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
{
+ use ObjectToPopulateTrait;
+
const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
const OBJECT_TO_POPULATE = 'object_to_populate';
const GROUPS = 'groups';
+ const ATTRIBUTES = 'attributes';
+ const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes';
/**
* @var int
/**
* Sets the {@link ClassMetadataFactoryInterface} to use.
- *
- * @param ClassMetadataFactoryInterface|null $classMetadataFactory
- * @param NameConverterInterface|null $nameConverter
*/
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
{
/**
* Set circular reference limit.
*
- * @param int $circularReferenceLimit limit of iterations for the same object
+ * @param int $circularReferenceLimit Limit of iterations for the same object
*
* @return self
*/
/**
* Set normalization callbacks.
*
- * @param callable[] $callbacks help normalize the result
+ * @param callable[] $callbacks Help normalize the result
*
* @return self
*
public function setCallbacks(array $callbacks)
{
foreach ($callbacks as $attribute => $callback) {
- if (!is_callable($callback)) {
+ if (!\is_callable($callback)) {
throw new InvalidArgumentException(sprintf(
'The given callback for attribute "%s" is not callable.',
$attribute
/**
* Set ignored attributes for normalization and denormalization.
*
- * @param array $ignoredAttributes
- *
* @return self
*/
public function setIgnoredAttributes(array $ignoredAttributes)
protected function handleCircularReference($object)
{
if ($this->circularReferenceHandler) {
- return call_user_func($this->circularReferenceHandler, $object);
+ return \call_user_func($this->circularReferenceHandler, $object);
}
- throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d)', get_class($object), $this->circularReferenceLimit));
+ throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d)', \get_class($object), $this->circularReferenceLimit));
}
/**
*/
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
{
- if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
+ if (!$this->classMetadataFactory) {
+ return false;
+ }
+
+ $groups = false;
+ if (isset($context[static::GROUPS]) && \is_array($context[static::GROUPS])) {
+ $groups = $context[static::GROUPS];
+ } elseif (!isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) || $context[static::ALLOW_EXTRA_ATTRIBUTES]) {
return false;
}
$name = $attributeMetadata->getName();
if (
- count(array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS])) &&
+ (false === $groups || array_intersect($attributeMetadata->getGroups(), $groups)) &&
$this->isAllowedAttribute($classOrObject, $name, null, $context)
) {
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
*/
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
{
- return !in_array($attribute, $this->ignoredAttributes);
+ if (in_array($attribute, $this->ignoredAttributes)) {
+ return false;
+ }
+
+ if (isset($context[self::ATTRIBUTES][$attribute])) {
+ // Nested attributes
+ return true;
+ }
+
+ if (isset($context[self::ATTRIBUTES]) && is_array($context[self::ATTRIBUTES])) {
+ return in_array($attribute, $context[self::ATTRIBUTES], true);
+ }
+
+ return true;
}
/**
*
* @throws RuntimeException
*/
- protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes/*, $format = null*/)
+ protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes/*, string $format = null*/)
{
- if (func_num_args() >= 6) {
- $format = func_get_arg(5);
+ if (\func_num_args() >= 6) {
+ $format = \func_get_arg(5);
} else {
- if (__CLASS__ !== get_class($this)) {
+ if (__CLASS__ !== \get_class($this)) {
$r = new \ReflectionMethod($this, __FUNCTION__);
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
- @trigger_error(sprintf('Method %s::%s() will have a 6th `$format = null` argument in version 4.0. Not defining it is deprecated since 3.2.', get_class($this), __FUNCTION__), E_USER_DEPRECATED);
+ @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);
}
}
$format = null;
}
- if (
- isset($context[static::OBJECT_TO_POPULATE]) &&
- is_object($context[static::OBJECT_TO_POPULATE]) &&
- $context[static::OBJECT_TO_POPULATE] instanceof $class
- ) {
- $object = $context[static::OBJECT_TO_POPULATE];
+ if (null !== $object = $this->extractObjectToPopulate($class, $context, static::OBJECT_TO_POPULATE)) {
unset($context[static::OBJECT_TO_POPULATE]);
return $object;
$paramName = $constructorParameter->name;
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
- $allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
- $ignored = in_array($paramName, $this->ignoredAttributes);
+ $allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
+ $ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
- if (!is_array($data[$paramName])) {
+ if (!\is_array($data[$paramName])) {
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));
}
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));
}
$parameterClass = $constructorParameter->getClass()->getName();
- $parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $context);
+ $parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $paramName));
}
} catch (\ReflectionException $e) {
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
return new $class();
}
+
+ /**
+ * @param array $parentContext
+ * @param string $attribute
+ *
+ * @return array
+ *
+ * @internal
+ */
+ protected function createChildContext(array $parentContext, $attribute)
+ {
+ if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
+ $parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];
+ } else {
+ unset($parentContext[self::ATTRIBUTES]);
+ }
+
+ return $parentContext;
+ }
}