|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Serializer\Normalizer;
|
13 | 13 |
|
14 |
| -use Symfony\Component\Serializer\Exception\RuntimeException; |
15 |
| - |
16 | 14 | /**
|
17 | 15 | * Converts between objects with getter and setter methods and arrays.
|
18 | 16 | *
|
|
36 | 34 | */
|
37 | 35 | class GetSetMethodNormalizer extends Ab
8000
stractObjectNormalizer
|
38 | 36 | {
|
39 |
| - /** |
40 |
| - * {@inheritdoc} |
41 |
| - * |
42 |
| - * @throws RuntimeException |
43 |
| - */ |
44 |
| - public function denormalize($data, $class, $format = null, array $context = array()) |
45 |
| - { |
46 |
| - $allowedAttributes = $this->getAllowedAttributes($class, $context, true); |
47 |
| - $normalizedData = $this->prepareForDenormalization($data); |
48 |
| - |
49 |
| - $reflectionClass = new \ReflectionClass($class); |
50 |
| - $object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes); |
51 |
| - |
52 |
| - $classMethods = get_class_methods($object); |
53 |
| - foreach ($normalizedData as $attribute => $value) { |
54 |
| - if ($this->nameConverter) { |
55 |
| - $attribute = $this->nameConverter->denormalize($attribute); |
56 |
| - } |
57 |
| - |
58 |
| - $allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes); |
59 |
| - $ignored = in_array($attribute, $this->ignoredAttributes); |
60 |
| - |
61 |
| - if ($allowed && !$ignored) { |
62 |
| - $setter = 'set'.ucfirst($attribute); |
63 |
| - |
64 |
| - if (in_array($setter, $classMethods) && !$reflectionClass->getMethod($setter)->isStatic()) { |
65 |
| - $object->$setter($value); |
66 |
| - } |
67 |
| - } |
68 |
| - } |
69 |
| - |
70 |
| - return $object; |
71 |
| - } |
| 37 | + private static $setterAccessibleCache = array(); |
72 | 38 |
|
73 | 39 | /**
|
74 | 40 | * {@inheritdoc}
|
@@ -175,8 +141,13 @@ protected function getAttributeValue($object, $attribute, $format = null, array
|
175 | 141 | protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
|
176 | 142 | {
|
177 | 143 | $setter = 'set'.ucfirst($attribute);
|
| 144 | + $key = get_class($object).':'.$setter; |
| 145 | + |
| 146 | + if (!isset(self::$setterAccessibleCache[$key])) { |
| 147 | + self::$setterAccessibleCache[$key] = is_callable(array($object, $setter)) && !(new \ReflectionMethod($object, $setter))->isStatic(); |
| 148 | + } |
178 | 149 |
|
179 |
| - if (is_callable(array($object, $setter))) { |
| 150 | + if (self::$setterAccessibleCache[$key]) { |
180 | 151 | $object->$setter($value);
|
181 | 152 | }
|
182 | 153 | }
|
|
0 commit comments