View source
<?php
namespace Symfony\Component\Serializer\Normalizer;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface {
protected $circularReferenceLimit = 1;
protected $circularReferenceHandler;
protected $classMetadataFactory;
protected $nameConverter;
protected $callbacks = array();
protected $ignoredAttributes = array();
protected $camelizedAttributes = array();
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null) {
$this->classMetadataFactory = $classMetadataFactory;
$this->nameConverter = $nameConverter;
}
public function setCircularReferenceLimit($circularReferenceLimit) {
$this->circularReferenceLimit = $circularReferenceLimit;
return $this;
}
public function setCircularReferenceHandler($circularReferenceHandler) {
if (!is_callable($circularReferenceHandler)) {
throw new InvalidArgumentException('The given circular reference handler is not callable.');
}
$this->circularReferenceHandler = $circularReferenceHandler;
return $this;
}
public function setCallbacks(array $callbacks) {
foreach ($callbacks as $attribute => $callback) {
if (!is_callable($callback)) {
throw new InvalidArgumentException(sprintf('The given callback for attribute "%s" is not callable.', $attribute));
}
}
$this->callbacks = $callbacks;
return $this;
}
public function setIgnoredAttributes(array $ignoredAttributes) {
$this->ignoredAttributes = $ignoredAttributes;
return $this;
}
public function setCamelizedAttributes(array $camelizedAttributes) {
@trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\\Component\\Serializer\\NameConverter\\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED);
if ($this->nameConverter && !$this->nameConverter instanceof CamelCaseToSnakeCaseNameConverter) {
throw new LogicException(sprintf('%s cannot be called if a custom Name Converter is defined.', __METHOD__));
}
$attributes = array();
foreach ($camelizedAttributes as $camelizedAttribute) {
$attributes[] = lcfirst(preg_replace_callback('/(^|_|\\.)+(.)/', function ($match) {
return ('.' === $match[1] ? '_' : '') . strtoupper($match[2]);
}, $camelizedAttribute));
}
$this->nameConverter = new CamelCaseToSnakeCaseNameConverter($attributes);
return $this;
}
protected function isCircularReference($object, &$context) {
$objectHash = spl_object_hash($object);
if (isset($context['circular_reference_limit'][$objectHash])) {
if ($context['circular_reference_limit'][$objectHash] >= $this->circularReferenceLimit) {
unset($context['circular_reference_limit'][$objectHash]);
return true;
}
++$context['circular_reference_limit'][$objectHash];
}
else {
$context['circular_reference_limit'][$objectHash] = 1;
}
return false;
}
protected function handleCircularReference($object) {
if ($this->circularReferenceHandler) {
return call_user_func($this->circularReferenceHandler, $object);
}
throw new CircularReferenceException(sprintf('A circular reference has been detected (configured limit: %d).', $this->circularReferenceLimit));
}
protected function formatAttribute($attributeName) {
@trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\\Component\\Serializer\\NameConverter\\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED);
return $this->nameConverter ? $this->nameConverter
->normalize($attributeName) : $attributeName;
}
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false) {
if (!$this->classMetadataFactory || !isset($context['groups']) || !is_array($context['groups'])) {
return false;
}
$allowedAttributes = array();
foreach ($this->classMetadataFactory
->getMetadataFor($classOrObject)
->getAttributesMetadata() as $attributeMetadata) {
if (count(array_intersect($attributeMetadata
->getGroups(), $context['groups']))) {
$allowedAttributes[] = $attributesAsString ? $attributeMetadata
->getName() : $attributeMetadata;
}
}
return array_unique($allowedAttributes);
}
protected function prepareForDenormalization($data) {
return (array) $data;
}
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes) {
if (isset($context['object_to_populate']) && is_object($context['object_to_populate']) && $class === get_class($context['object_to_populate'])) {
return $context['object_to_populate'];
}
$constructor = $reflectionClass
->getConstructor();
if ($constructor) {
$constructorParameters = $constructor
->getParameters();
$params = array();
foreach ($constructorParameters as $constructorParameter) {
$paramName = $constructorParameter->name;
$key = $this->nameConverter ? $this->nameConverter
->normalize($paramName) : $paramName;
$allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
$ignored = in_array($paramName, $this->ignoredAttributes);
if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter
->isVariadic()) {
if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
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));
}
$params = array_merge($params, $data[$paramName]);
}
}
elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
$params[] = $data[$key];
unset($data[$key]);
}
elseif ($constructorParameter
->isDefaultValueAvailable()) {
$params[] = $constructorParameter
->getDefaultValue();
}
else {
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));
}
}
return $reflectionClass
->newInstanceArgs($params);
}
return new $class();
}
}