PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/serializer/Normalizer/AbstractNormalizer.php

https://gitlab.com/geeta7/drupal
PHP | 345 lines | 158 code | 39 blank | 148 comment | 26 complexity | 2cd898e6266b675d69e8ca8b64cce26f MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Normalizer;
  11. use Symfony\Component\Serializer\Exception\CircularReferenceException;
  12. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  13. use Symfony\Component\Serializer\Exception\LogicException;
  14. use Symfony\Component\Serializer\Exception\RuntimeException;
  15. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  16. use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
  17. use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
  18. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  19. /**
  20. * Normalizer implementation.
  21. *
  22. * @author Kévin Dunglas <dunglas@gmail.com>
  23. */
  24. abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
  25. {
  26. /**
  27. * @var int
  28. */
  29. protected $circularReferenceLimit = 1;
  30. /**
  31. * @var callable
  32. */
  33. protected $circularReferenceHandler;
  34. /**
  35. * @var ClassMetadataFactoryInterface|null
  36. */
  37. protected $classMetadataFactory;
  38. /**
  39. * @var NameConverterInterface|null
  40. */
  41. protected $nameConverter;
  42. /**
  43. * @var array
  44. */
  45. protected $callbacks = array();
  46. /**
  47. * @var array
  48. */
  49. protected $ignoredAttributes = array();
  50. /**
  51. * @var array
  52. */
  53. protected $camelizedAttributes = array();
  54. /**
  55. * Sets the {@link ClassMetadataFactoryInterface} to use.
  56. *
  57. * @param ClassMetadataFactoryInterface|null $classMetadataFactory
  58. * @param NameConverterInterface|null $nameConverter
  59. */
  60. public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
  61. {
  62. $this->classMetadataFactory = $classMetadataFactory;
  63. $this->nameConverter = $nameConverter;
  64. }
  65. /**
  66. * Set circular reference limit.
  67. *
  68. * @param int $circularReferenceLimit limit of iterations for the same object
  69. *
  70. * @return self
  71. */
  72. public function setCircularReferenceLimit($circularReferenceLimit)
  73. {
  74. $this->circularReferenceLimit = $circularReferenceLimit;
  75. return $this;
  76. }
  77. /**
  78. * Set circular reference handler.
  79. *
  80. * @param callable $circularReferenceHandler
  81. *
  82. * @return self
  83. *
  84. * @throws InvalidArgumentException
  85. */
  86. public function setCircularReferenceHandler($circularReferenceHandler)
  87. {
  88. if (!is_callable($circularReferenceHandler)) {
  89. throw new InvalidArgumentException('The given circular reference handler is not callable.');
  90. }
  91. $this->circularReferenceHandler = $circularReferenceHandler;
  92. return $this;
  93. }
  94. /**
  95. * Set normalization callbacks.
  96. *
  97. * @param callable[] $callbacks help normalize the result
  98. *
  99. * @return self
  100. *
  101. * @throws InvalidArgumentException if a non-callable callback is set
  102. */
  103. public function setCallbacks(array $callbacks)
  104. {
  105. foreach ($callbacks as $attribute => $callback) {
  106. if (!is_callable($callback)) {
  107. throw new InvalidArgumentException(sprintf(
  108. 'The given callback for attribute "%s" is not callable.',
  109. $attribute
  110. ));
  111. }
  112. }
  113. $this->callbacks = $callbacks;
  114. return $this;
  115. }
  116. /**
  117. * Set ignored attributes for normalization and denormalization.
  118. *
  119. * @param array $ignoredAttributes
  120. *
  121. * @return self
  122. */
  123. public function setIgnoredAttributes(array $ignoredAttributes)
  124. {
  125. $this->ignoredAttributes = $ignoredAttributes;
  126. return $this;
  127. }
  128. /**
  129. * Set attributes to be camelized on denormalize.
  130. *
  131. * @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.
  132. *
  133. * @param array $camelizedAttributes
  134. *
  135. * @return self
  136. *
  137. * @throws LogicException
  138. */
  139. public function setCamelizedAttributes(array $camelizedAttributes)
  140. {
  141. @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);
  142. if ($this->nameConverter && !$this->nameConverter instanceof CamelCaseToSnakeCaseNameConverter) {
  143. throw new LogicException(sprintf('%s cannot be called if a custom Name Converter is defined.', __METHOD__));
  144. }
  145. $attributes = array();
  146. foreach ($camelizedAttributes as $camelizedAttribute) {
  147. $attributes[] = lcfirst(preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
  148. return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
  149. }, $camelizedAttribute));
  150. }
  151. $this->nameConverter = new CamelCaseToSnakeCaseNameConverter($attributes);
  152. return $this;
  153. }
  154. /**
  155. * Detects if the configured circular reference limit is reached.
  156. *
  157. * @param object $object
  158. * @param array $context
  159. *
  160. * @return bool
  161. *
  162. * @throws CircularReferenceException
  163. */
  164. protected function isCircularReference($object, &$context)
  165. {
  166. $objectHash = spl_object_hash($object);
  167. if (isset($context['circular_reference_limit'][$objectHash])) {
  168. if ($context['circular_reference_limit'][$objectHash] >= $this->circularReferenceLimit) {
  169. unset($context['circular_reference_limit'][$objectHash]);
  170. return true;
  171. }
  172. ++$context['circular_reference_limit'][$objectHash];
  173. } else {
  174. $context['circular_reference_limit'][$objectHash] = 1;
  175. }
  176. return false;
  177. }
  178. /**
  179. * Handles a circular reference.
  180. *
  181. * If a circular reference handler is set, it will be called. Otherwise, a
  182. * {@class CircularReferenceException} will be thrown.
  183. *
  184. * @param object $object
  185. *
  186. * @return mixed
  187. *
  188. * @throws CircularReferenceException
  189. */
  190. protected function handleCircularReference($object)
  191. {
  192. if ($this->circularReferenceHandler) {
  193. return call_user_func($this->circularReferenceHandler, $object);
  194. }
  195. throw new CircularReferenceException(sprintf('A circular reference has been detected (configured limit: %d).', $this->circularReferenceLimit));
  196. }
  197. /**
  198. * Format an attribute name, for example to convert a snake_case name to camelCase.
  199. *
  200. * @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.
  201. *
  202. * @param string $attributeName
  203. *
  204. * @return string
  205. */
  206. protected function formatAttribute($attributeName)
  207. {
  208. @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);
  209. return $this->nameConverter ? $this->nameConverter->normalize($attributeName) : $attributeName;
  210. }
  211. /**
  212. * Gets attributes to normalize using groups.
  213. *
  214. * @param string|object $classOrObject
  215. * @param array $context
  216. * @param bool $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
  217. *
  218. * @return string[]|AttributeMetadataInterface[]|bool
  219. */
  220. protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
  221. {
  222. if (!$this->classMetadataFactory || !isset($context['groups']) || !is_array($context['groups'])) {
  223. return false;
  224. }
  225. $allowedAttributes = array();
  226. foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
  227. if (count(array_intersect($attributeMetadata->getGroups(), $context['groups']))) {
  228. $allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
  229. }
  230. }
  231. return array_unique($allowedAttributes);
  232. }
  233. /**
  234. * Normalizes the given data to an array. It's particularly useful during
  235. * the denormalization process.
  236. *
  237. * @param object|array $data
  238. *
  239. * @return array
  240. */
  241. protected function prepareForDenormalization($data)
  242. {
  243. return (array) $data;
  244. }
  245. /**
  246. * Instantiates an object using constructor parameters when needed.
  247. *
  248. * This method also allows to denormalize data into an existing object if
  249. * it is present in the context with the object_to_populate key.
  250. *
  251. * @param array $data
  252. * @param string $class
  253. * @param array $context
  254. * @param \ReflectionClass $reflectionClass
  255. * @param array|bool $allowedAttributes
  256. *
  257. * @return object
  258. *
  259. * @throws RuntimeException
  260. */
  261. protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
  262. {
  263. if (
  264. isset($context['object_to_populate']) &&
  265. is_object($context['object_to_populate']) &&
  266. $class === get_class($context['object_to_populate'])
  267. ) {
  268. return $context['object_to_populate'];
  269. }
  270. $constructor = $reflectionClass->getConstructor();
  271. if ($constructor) {
  272. $constructorParameters = $constructor->getParameters();
  273. $params = array();
  274. foreach ($constructorParameters as $constructorParameter) {
  275. $paramName = $constructorParameter->name;
  276. $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
  277. $allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
  278. $ignored = in_array($paramName, $this->ignoredAttributes);
  279. if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
  280. if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
  281. if (!is_array($data[$paramName])) {
  282. 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));
  283. }
  284. $params = array_merge($params, $data[$paramName]);
  285. }
  286. } elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
  287. $params[] = $data[$key];
  288. // don't run set for a parameter passed to the constructor
  289. unset($data[$key]);
  290. } elseif ($constructorParameter->isDefaultValueAvailable()) {
  291. $params[] = $constructorParameter->getDefaultValue();
  292. } else {
  293. throw new RuntimeException(
  294. sprintf(
  295. 'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.',
  296. $class,
  297. $constructorParameter->name
  298. )
  299. );
  300. }
  301. }
  302. return $reflectionClass->newInstanceArgs($params);
  303. }
  304. return new $class();
  305. }
  306. }