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

VirtualPropertyMetadata::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
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
class VirtualPropertyMetadata extends PropertyMetadata
22
{
23 51
    public function __construct($class, $methodName)
24
    {
25 51
        if (0 === strpos($methodName, 'get')) {
26 49
            $fieldName = lcfirst(substr($methodName, 3));
27 49
        } else {
28 8
            $fieldName = $methodName;
29
        }
30
31 51
        $this->class = $class;
32 51
        $this->name = $fieldName;
33 51
        $this->getter = $methodName;
34 51
        $this->readOnly = true;
35 51
    }
36
37
    public function setValue($obj, $value)
38
    {
39
        throw new \LogicException('VirtualPropertyMetadata is immutable.');
40
    }
41
42 47
    public function setAccessor($type, $getter = null, $setter = null)
43
    {
44 47
    }
45
46
    public function serialize()
47
    {
48
        return serialize(array(
49
            $this->sinceVersion,
50
            $this->untilVersion,
51
            $this->groups,
52
            $this->serializedName,
53
            $this->type,
54
            $this->deserializeType,
55
            $this->xmlCollection,
56
            $this->xmlCollectionInline,
57
            $this->xmlEntryName,
58
            $this->xmlKeyAttribute,
59
            $this->xmlAttribute,
60
            $this->xmlValue,
61
            $this->xmlNamespace,
62
            $this->xmlKeyValuePairs,
63
            $this->xmlElementCData,
64
            $this->xmlAttributeMap,
65
            $this->maxDepth,
66
            $this->getter,
67
            $this->setter,
68
            $this->inline,
69
            $this->readOnly,
70
            $this->class,
71
            $this->name,
72
            'excludeIf' => $this->excludeIf,
73
        ));
74
    }
75
76
    public function unserialize($str)
77
    {
78
        $unserialized = unserialize($str);
79
        list(
80
            $this->sinceVersion,
81
            $this->untilVersion,
82
            $this->groups,
83
            $this->serializedName,
84
            $this->type,
85
            $this->deserializeType,
86
            $this->xmlCollection,
87
            $this->xmlCollectionInline,
88
            $this->xmlEntryName,
89
            $this->xmlKeyAttribute,
90
            $this->xmlAttribute,
91
            $this->xmlValue,
92
            $this->xmlNamespace,
93
            $this->xmlKeyValuePairs,
94
            $this->xmlElementCData,
95
            $this->xmlAttributeMap,
96
            $this->maxDepth,
97
            $this->getter,
98
            $this->setter,
99
            $this->inline,
100
            $this->readOnly,
101
            $this->class,
102
            $this->name
103
            ) = $unserialized;
104
105
        if (isset($unserialized['excludeIf'])) {
106
            $this->excludeIf = $unserialized['excludeIf'];
107
        }
108
    }
109
}
110