-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
DefaultAccessorStrategy.php
132 lines (110 loc) · 4.76 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
<?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\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 <[email protected]>
*/
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(null);
}
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) {
if (!isset($this->readAccessors[$metadata->class])) {
if (true === $metadata->forceReflectionAccess) {
$this->readAccessors[$metadata->class] = function ($o, $name) use ($metadata) {
$ref = $this->propertyReflectionCache[$metadata->class][$name] ?? null;
if (null === $ref) {
$ref = new \ReflectionProperty($metadata->class, $name);
$ref->setAccessible(true);
$this->propertyReflectionCache[$metadata->class][$name] = $ref;
}
return $ref->getValue($o);
};
} else {
$this->readAccessors[$metadata->class] = \Closure::bind(static function ($o, $name) {
return $o->$name;
}, null, $metadata->class);
}
}
return $this->readAccessors[$metadata->class]($object, $metadata->name);
}
return $object->{$metadata->getter}();
}
/**
* {@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) {
if (!isset($this->writeAccessors[$metadata->class])) {
if (true === $metadata->forceReflectionAccess) {
$this->writeAccessors[$metadata->class] = function ($o, $name, $value) use ($metadata): void {
$ref = $this->propertyReflectionCache[$metadata->class][$name] ?? null;
if (null === $ref) {
$ref = new \ReflectionProperty($metadata->class, $name);
$ref->setAccessible(true);
$this->propertyReflectionCache[$metadata->class][$name] = $ref;
}
$ref->setValue($o, $value);
};
} else {
$this->writeAccessors[$metadata->class] = \Closure::bind(static function ($o, $name, $value): void {
$o->$name = $value;
}, null, $metadata->class);
}
}
$this->writeAccessors[$metadata->class]($object, $metadata->name, $value);
return;
}
$object->{$metadata->setter}($value);
}
}