4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Normalizer;
14 use Symfony\Component\Serializer\Exception\CircularReferenceException;
15 use Symfony\Component\Serializer\Exception\LogicException;
16 use Symfony\Component\Serializer\Exception\RuntimeException;
19 * Converts between objects with getter and setter methods and arrays.
21 * The normalization process looks at all public methods and calls the ones
22 * which have a name starting with get and take no parameters. The result is a
23 * map from property names (method name stripped of the get prefix and converted
24 * to lower case) to property values. Property values are normalized through the
27 * The denormalization first looks at the constructor of the given class to see
28 * if any of the parameters have the same name as one of the properties. The
29 * constructor is then called with all parameters or an exception is thrown if
30 * any required parameters were not present as properties. Then the denormalizer
31 * walks through the given map of property names to property values to see if a
32 * setter method exists for any of the properties. If a setter exists it is
33 * called with the property value. No automatic denormalization of the value
36 * @author Nils Adermann <naderman@naderman.de>
37 * @author Kévin Dunglas <dunglas@gmail.com>
39 class GetSetMethodNormalizer extends AbstractNormalizer
44 * @throws LogicException
45 * @throws CircularReferenceException
47 public function normalize($object, $format = null, array $context = array())
49 if ($this->isCircularReference($object, $context)) {
50 return $this->handleCircularReference($object);
53 $reflectionObject = new \ReflectionObject($object);
54 $reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC);
55 $allowedAttributes = $this->getAllowedAttributes($object, $context, true);
57 $attributes = array();
58 foreach ($reflectionMethods as $method) {
59 if ($this->isGetMethod($method)) {
60 $attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
61 if (in_array($attributeName, $this->ignoredAttributes)) {
65 if (false !== $allowedAttributes && !in_array($attributeName, $allowedAttributes)) {
69 $attributeValue = $method->invoke($object);
70 if (isset($this->callbacks[$attributeName])) {
71 $attributeValue = call_user_func($this->callbacks[$attributeName], $attributeValue);
73 if (null !== $attributeValue && !is_scalar($attributeValue)) {
74 if (!$this->serializer instanceof NormalizerInterface) {
75 throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName));
78 $attributeValue = $this->serializer->normalize($attributeValue, $format, $context);
81 if ($this->nameConverter) {
82 $attributeName = $this->nameConverter->normalize($attributeName);
85 $attributes[$attributeName] = $attributeValue;
95 * @throws RuntimeException
97 public function denormalize($data, $class, $format = null, array $context = array())
99 $allowedAttributes = $this->getAllowedAttributes($class, $context, true);
100 $normalizedData = $this->prepareForDenormalization($data);
102 $reflectionClass = new \ReflectionClass($class);
103 $object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
105 $classMethods = get_class_methods($object);
106 foreach ($normalizedData as $attribute => $value) {
107 if ($this->nameConverter) {
108 $attribute = $this->nameConverter->denormalize($attribute);
111 $allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes);
112 $ignored = in_array($attribute, $this->ignoredAttributes);
114 if ($allowed && !$ignored) {
115 $setter = 'set'.ucfirst($attribute);
117 if (in_array($setter, $classMethods) && !$reflectionClass->getMethod($setter)->isStatic()) {
118 $object->$setter($value);
129 public function supportsNormalization($data, $format = null)
131 return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
137 public function supportsDenormalization($data, $type, $format = null)
139 return class_exists($type) && $this->supports($type);
143 * Checks if the given class has any get{Property} method.
145 * @param string $class
149 private function supports($class)
151 $class = new \ReflectionClass($class);
152 $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
153 foreach ($methods as $method) {
154 if ($this->isGetMethod($method)) {
163 * Checks if a method's name is get.* or is.*, and can be called without parameters.
165 * @param \ReflectionMethod $method the method to check
167 * @return bool whether the method is a getter or boolean getter
169 private function isGetMethod(\ReflectionMethod $method)
171 $methodLength = strlen($method->name);
174 !$method->isStatic() &&
176 ((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
177 (0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
178 0 === $method->getNumberOfRequiredParameters()