Passed
Pull Request — master (#938)
by Michael
03:10
created

DefaultAccessorStrategy::setValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 3
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
crap 4.0119
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
namespace JMS\Serializer\Accessor;
22
23
use JMS\Serializer\Exception\ExpressionLanguageRequiredException;
24
use JMS\Serializer\Exception\LogicException;
25
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
26
use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
27
use JMS\Serializer\Metadata\PropertyMetadata;
28
use JMS\Serializer\Metadata\StaticPropertyMetadata;
29
use JMS\Serializer\Metadata\VirtualPropertyMetadata;
30
31
/**
32
 * @author Asmir Mustafic <[email protected]>
33
 */
34
final class DefaultAccessorStrategy implements AccessorStrategyInterface
35
{
36
    private $readAccessors = array();
37
    private $writeAccessors = array();
38
39
    /**
40
     * @var ExpressionEvaluatorInterface
41
     */
42
    private $evaluator;
43
44 320
    public function __construct(ExpressionEvaluatorInterface $evaluator = null)
45
    {
46 320
        $this->evaluator = $evaluator;
47 320
    }
48
49 160
    public function getValue(object $object, PropertyMetadata $metadata)
50
    {
51 160
        if ($metadata instanceof StaticPropertyMetadata) {
52 15
            return $metadata->getValue(null);
53
        }
54
55 156
        if ($metadata instanceof ExpressionPropertyMetadata) {
56 4
            if ($this->evaluator === null) {
57 2
                throw new ExpressionLanguageRequiredException(sprintf('The property %s on %s requires the expression accessor strategy to be enabled.', $metadata->name, $metadata->class));
58
            }
59
60 2
            return $this->evaluator->evaluate($metadata->expression, ['object' => $object]);
61
        }
62
63 154
        if (null === $metadata->getter) {
64 148
            if (!isset($this->readAccessors[$metadata->class])) {
65 148
                $this->readAccessors[$metadata->class] = \Closure::bind(function ($o, $name) {
66 148
                    return $o->$name;
67 148
                }, null, $metadata->class);
68
            }
69
70 148
            return $this->readAccessors[$metadata->class]($object, $metadata->name);
71
        }
72
73 24
        return $object->{$metadata->getter}();
74
    }
75
76 59
    public function setValue(object $object, $value, PropertyMetadata $metadata): void
77
    {
78 59
        if ($metadata->readOnly) {
79
            throw new LogicException(sprintf('%s on %s is read only.'), $metadata->name, $metadata->class);
80
        }
81
82 59
        if (null === $metadata->setter) {
83 58
            if (!isset($this->writeAccessors[$metadata->class])) {
84 58
                $this->writeAccessors[$metadata->class] = \Closure::bind(function ($o, $name, $value) {
85 58
                    $o->$name = $value;
86 58
                }, null, $metadata->class);
87
            }
88
89 58
            $this->writeAccessors[$metadata->class]($object, $metadata->name, $value);
90 58
            return;
91
        }
92
93 3
        $object->{$metadata->setter}($value);
94 3
    }
95
}
96