Completed
Pull Request — master (#708)
by Asmir
11:37
created

ExpressionPropertyMetadata::serialize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 27
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 0
crap 2
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 7
    public function __construct($class, $fieldName, $expression)
37
    {
38 7
        $this->class = $class;
39 7
        $this->name = $fieldName;
40 7
        $this->expression = $expression;
41 7
        $this->readOnly = true;
42 7
    }
43
44 5
    public function setAccessor($type, $getter = null, $setter = null)
45
    {
46 5
    }
47
48
    /**
49
     * @param object $object
50
     * @return mixed
51
     */
52
    public function getValue($object)
53
    {
54
        throw new ExpressionLanguageRequiredException(sprintf('The property %s on %s requires the expression evaluator 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->xmlCollection,
71
            $this->xmlCollectionInline,
72
            $this->xmlEntryName,
73
            $this->xmlKeyAttribute,
74
            $this->xmlAttribute,
75
            $this->xmlValue,
76
            $this->xmlNamespace,
77
            $this->xmlKeyValuePairs,
78
            $this->xmlElementCData,
79
            $this->xmlAttributeMap,
80
            $this->maxDepth,
81
            $this->getter,
82
            $this->setter,
83
            $this->inline,
84
            $this->readOnly,
85
            $this->class,
86
            $this->name,
87
            'excludeIf' => $this->excludeIf,
88
            'expression' => $this->expression,
89
        ));
90
    }
91
92
    public function unserialize($str)
93
    {
94
        $unserialized = unserialize($str);
95
        list(
96
            $this->sinceVersion,
97
            $this->untilVersion,
98
            $this->groups,
99
            $this->serializedName,
100
            $this->type,
101
            $this->xmlCollection,
102
            $this->xmlCollectionInline,
103
            $this->xmlEntryName,
104
            $this->xmlKeyAttribute,
105
            $this->xmlAttribute,
106
            $this->xmlValue,
107
            $this->xmlNamespace,
108
            $this->xmlKeyValuePairs,
109
            $this->xmlElementCData,
110
            $this->xmlAttributeMap,
111
            $this->maxDepth,
112
            $this->getter,
113
            $this->setter,
114
            $this->inline,
115
            $this->readOnly,
116
            $this->class,
117
            $this->name
118
        ) = $unserialized;
119
120
        if (isset($unserialized['excludeIf'])){
121
            $this->excludeIf = $unserialized['excludeIf'];
122
        }
123
        if (isset($unserialized['expression'])){
124
            $this->expression = $unserialized['expression'];
125
        }
126
    }
127
}
128