Completed
Pull Request — master (#787)
by
unknown
03:32
created

ExpressionPropertyMetadata::setAccessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
crap 1
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\Metadata;
20
21
use JMS\Serializer\Exception\ExpressionLanguageRequiredException;
22
23
/**
24
 * @Annotation
25
 * @Target("METHOD")
26
 *
27
 * @author Asmir Mustafic <[email protected]>
28
 */
29
class ExpressionPropertyMetadata extends PropertyMetadata
30
{
31
    /**
32
     * @var string
33
     */
34
    public $expression;
35
36 14
    public function __construct($class, $fieldName, $expression)
37
    {
38 14
        $this->class = $class;
39 14
        $this->name = $fieldName;
40 14
        $this->expression = $expression;
41 14
        $this->readOnly = true;
42 14
    }
43
44 11
    public function setAccessor($type, $getter = null, $setter = null)
45
    {
46 11
    }
47
48
    /**
49
     * @param object $object
50
     * @return mixed
51
     */
52 3
    public function getValue($object)
53
    {
54 3
        throw new ExpressionLanguageRequiredException(sprintf('The property %s on %s requires the expression accessor strategy to be enabled.', $this->name, $this->class));
55
    }
56
57
    public function setValue($obj, $value)
58
    {
59
        throw new \LogicException('ExpressionPropertyMetadata is immutable.');
60
    }
61
62
    public function serialize()
63
    {
64
        return serialize(array(
65
            $this->sinceVersion,
66
            $this->untilVersion,
67
            $this->groups,
68
            $this->serializedName,
69
            $this->type,
70
            $this->deserializeType,
71
            $this->xmlCollection,
72
            $this->xmlCollectionInline,
73
            $this->xmlEntryName,
74
            $this->xmlKeyAttribute,
75
            $this->xmlAttribute,
76
            $this->xmlValue,
77
            $this->xmlNamespace,
78
            $this->xmlKeyValuePairs,
79
            $this->xmlElementCData,
80
            $this->xmlAttributeMap,
81
            $this->maxDepth,
82
            $this->getter,
83
            $this->setter,
84
            $this->inline,
85
            $this->readOnly,
86
            $this->class,
87
            $this->name,
88
            'excludeIf' => $this->excludeIf,
89
            'expression' => $this->expression,
90
        ));
91
    }
92
93
    public function unserialize($str)
94
    {
95
        $unserialized = unserialize($str);
96
        list(
97
            $this->sinceVersion,
98
            $this->untilVersion,
99
            $this->groups,
100
            $this->serializedName,
101
            $this->type,
102
            $this->deserializeType,
103
            $this->xmlCollection,
104
            $this->xmlCollectionInline,
105
            $this->xmlEntryName,
106
            $this->xmlKeyAttribute,
107
            $this->xmlAttribute,
108
            $this->xmlValue,
109
            $this->xmlNamespace,
110
            $this->xmlKeyValuePairs,
111
            $this->xmlElementCData,
112
            $this->xmlAttributeMap,
113
            $this->maxDepth,
114
            $this->getter,
115
            $this->setter,
116
            $this->inline,
117
            $this->readOnly,
118
            $this->class,
119
            $this->name
120
            ) = $unserialized;
121
122
        if (isset($unserialized['excludeIf'])) {
123
            $this->excludeIf = $unserialized['excludeIf'];
124
        }
125
        if (isset($unserialized['expression'])) {
126
            $this->expression = $unserialized['expression'];
127
        }
128
    }
129
}
130