Passed
Branch master (bf85d9)
by Johannes
05:40
created

ExpressionAccessorStrategy::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Serializer\Accessor;
20
21
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
22
use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
23
use JMS\Serializer\Metadata\PropertyMetadata;
24
25
/**
26
 * @author Asmir Mustafic <[email protected]>
27
 */
28
final class ExpressionAccessorStrategy implements AccessorStrategyInterface
29
{
30
    /**
31
     * @var AccessorStrategyInterface
32
     */
33
    private $fallback;
34
    /**
35
     * @var ExpressionEvaluatorInterface
36
     */
37
    private $evaluator;
38
39 5
    public function __construct(ExpressionEvaluatorInterface $evaluator, AccessorStrategyInterface $fallback)
40
    {
41 5
        $this->fallback = $fallback;
42 5
        $this->evaluator = $evaluator;
43 5
    }
44
45 5
    public function getValue($object, PropertyMetadata $metadata)
46
    {
47 5
        if ($metadata instanceof ExpressionPropertyMetadata) {
48 2
            return $this->evaluator->evaluate($metadata->expression, array('object' => $object));
49
        }
50 5
        return $this->fallback->getValue($object, $metadata);
51
    }
52
53 1
    public function setValue($object, $value, PropertyMetadata $metadata)
54
    {
55 1
        $this->fallback->setValue($object, $value, $metadata);
56 1
    }
57
}
58