-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
DefaultAccessorStrategy.php
145 lines (118 loc) · 4.86 KB
/
DefaultAccessorStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace JMS\Serializer\Accessor;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Exception\ExpressionLanguageRequiredException;
use JMS\Serializer\Exception\LogicException;
use JMS\Serializer\Exception\UninitializedPropertyException;
use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
use JMS\Serializer\Expression\Expression;
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Metadata\StaticPropertyMetadata;
use JMS\Serializer\SerializationContext;
/**
* @author Asmir Mustafic <goetas@gmail.com>
*/
final class DefaultAccessorStrategy implements AccessorStrategyInterface
{
/**
* @var callable[]
*/
private $readAccessors = [];
/**
* @var callable[]
*/
private $writeAccessors = [];
/**
* @var \ReflectionProperty[]
*/
private $propertyReflectionCache = [];
/**
* @var ExpressionEvaluatorInterface
*/
private $evaluator;
public function __construct(?ExpressionEvaluatorInterface $evaluator = null)
{
$this->evaluator = $evaluator;
}
/**
* {@inheritdoc}
*/
public function getValue(object $object, PropertyMetadata $metadata, SerializationContext $context)
{
if ($metadata instanceof StaticPropertyMetadata) {
return $metadata->getValue();
}
if ($metadata instanceof ExpressionPropertyMetadata) {
if (null === $this->evaluator) {
throw new ExpressionLanguageRequiredException(sprintf('The property %s on %s requires the expression accessor strategy to be enabled.', $metadata->name, $metadata->class));
}
$variables = ['object' => $object, 'context' => $context, 'property_metadata' => $metadata];
if (($metadata->expression instanceof Expression) && ($this->evaluator instanceof CompilableExpressionEvaluatorInterface)) {
return $this->evaluator->evaluateParsed($metadata->expression, $variables);
}
return $this->evaluator->evaluate($metadata->expression, $variables);
}
if (null !== $metadata->getter) {
return $object->{$metadata->getter}();
}
if ($metadata->forceReflectionAccess) {
$ref = $this->propertyReflectionCache[$metadata->class][$metadata->name] ?? null;
if (null === $ref) {
$ref = new \ReflectionProperty($metadata->class, $metadata->name);
$ref->setAccessible(true);
$this->propertyReflectionCache[$metadata->class][$metadata->name] = $ref;
}
return $ref->getValue($object);
}
$accessor = $this->readAccessors[$metadata->class] ?? null;
if (null === $accessor) {
$accessor = \Closure::bind(static function ($o, $name) {
return $o->$name;
}, null, $metadata->class);
$this->readAccessors[$metadata->class] = $accessor;
}
try {
return $accessor($object, $metadata->name);
} catch (\Error $e) {
// handle uninitialized properties in PHP >= 7.4
if (preg_match('/^Typed property ([\w\\\\@]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
throw new UninitializedPropertyException(sprintf('Uninitialized property "%s::$%s".', $metadata->class, $metadata->name), 0, $e);
}
throw $e;
}
}
/**
* {@inheritdoc}
*/
public function setValue(object $object, $value, PropertyMetadata $metadata, DeserializationContext $context): void
{
if (true === $metadata->readOnly) {
throw new LogicException(sprintf('%s on %s is read only.', $metadata->name, $metadata->class));
}
if (null !== $metadata->setter) {
$object->{$metadata->setter}($value);
return;
}
if ($metadata->forceReflectionAccess) {
$ref = $this->propertyReflectionCache[$metadata->class][$metadata->name] ?? null;
if (null === $ref) {
$ref = new \ReflectionProperty($metadata->class, $metadata->name);
$ref->setAccessible(true);
$this->propertyReflectionCache[$metadata->class][$metadata->name] = $ref;
}
$ref->setValue($object, $value);
return;
}
$accessor = $this->writeAccessors[$metadata->class] ?? null;
if (null === $accessor) {
$accessor = \Closure::bind(static function ($o, $name, $value): void {
$o->$name = $value;
}, null, $metadata->class);
$this->writeAccessors[$metadata->class] = $accessor;
}
$accessor($object, $metadata->name, $value);
}
}