|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Accessor; |
|
6
|
|
|
|
|
7
|
|
|
use JMS\Serializer\Exception\ExpressionLanguageRequiredException; |
|
8
|
|
|
use JMS\Serializer\Exception\LogicException; |
|
9
|
|
|
use JMS\Serializer\Expression\ExpressionEvaluatorInterface; |
|
10
|
|
|
use JMS\Serializer\Metadata\ExpressionPropertyMetadata; |
|
11
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
|
12
|
|
|
use JMS\Serializer\Metadata\StaticPropertyMetadata; |
|
13
|
|
|
use JMS\Serializer\Metadata\VirtualPropertyMetadata; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Asmir Mustafic <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
final class DefaultAccessorStrategy implements AccessorStrategyInterface |
|
19
|
|
|
{ |
|
20
|
|
|
private $readAccessors = array(); |
|
21
|
|
|
private $writeAccessors = array(); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ExpressionEvaluatorInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $evaluator; |
|
27
|
|
|
|
|
28
|
327 |
|
public function __construct(ExpressionEvaluatorInterface $evaluator = null) |
|
29
|
|
|
{ |
|
30
|
327 |
|
$this->evaluator = $evaluator; |
|
31
|
327 |
|
} |
|
32
|
|
|
|
|
33
|
166 |
|
public function getValue(object $object, PropertyMetadata $metadata) |
|
34
|
|
|
{ |
|
35
|
166 |
|
if ($metadata instanceof StaticPropertyMetadata) { |
|
36
|
15 |
|
return $metadata->getValue(null); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
162 |
|
if ($metadata instanceof ExpressionPropertyMetadata) { |
|
40
|
4 |
|
if ($this->evaluator === null) { |
|
41
|
2 |
|
throw new ExpressionLanguageRequiredException(sprintf('The property %s on %s requires the expression accessor strategy to be enabled.', $metadata->name, $metadata->class)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
return $this->evaluator->evaluate($metadata->expression, ['object' => $object]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
160 |
|
if (null === $metadata->getter) { |
|
48
|
154 |
|
if (!isset($this->readAccessors[$metadata->class])) { |
|
49
|
|
|
$this->readAccessors[$metadata->class] = \Closure::bind(function ($o, $name) { |
|
50
|
154 |
|
return $o->$name; |
|
51
|
154 |
|
}, null, $metadata->class); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
154 |
|
return $this->readAccessors[$metadata->class]($object, $metadata->name); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
24 |
|
return $object->{$metadata->getter}(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
60 |
|
public function setValue(object $object, $value, PropertyMetadata $metadata): void |
|
61
|
|
|
{ |
|
62
|
60 |
|
if ($metadata->readOnly) { |
|
63
|
|
|
throw new LogicException(sprintf('%s on %s is read only.', $metadata->name, $metadata->class)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
60 |
|
if (null === $metadata->setter) { |
|
67
|
59 |
|
if (!isset($this->writeAccessors[$metadata->class])) { |
|
68
|
|
|
$this->writeAccessors[$metadata->class] = \Closure::bind(function ($o, $name, $value) { |
|
69
|
59 |
|
$o->$name = $value; |
|
70
|
59 |
|
}, null, $metadata->class); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
59 |
|
$this->writeAccessors[$metadata->class]($object, $metadata->name, $value); |
|
74
|
59 |
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
$object->{$metadata->setter}($value); |
|
78
|
3 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.