Completed
Pull Request — master (#940)
by Asmir
03:00
created

PropertyMetadata::getReflection()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Metadata;
22
23
use JMS\Serializer\Exception\RuntimeException;
24
use Metadata\PropertyMetadata as BasePropertyMetadata;
25
26
class PropertyMetadata extends BasePropertyMetadata
27
{
28
    const ACCESS_TYPE_PROPERTY = 'property';
29
    const ACCESS_TYPE_PUBLIC_METHOD = 'public_method';
30
31
    public $sinceVersion;
32
    public $untilVersion;
33
    public $groups;
34
    public $serializedName;
35
    public $type;
36
    public $xmlCollection = false;
37
    public $xmlCollectionInline = false;
38
    public $xmlCollectionSkipWhenEmpty = true;
39
    public $xmlEntryName;
40
    public $xmlEntryNamespace;
41
    public $xmlKeyAttribute;
42
    public $xmlAttribute = false;
43
    public $xmlValue = false;
44
    public $xmlNamespace;
45
    public $xmlKeyValuePairs = false;
46
    public $xmlElementCData = true;
47
    public $getter;
48
    public $setter;
49
    public $inline = false;
50
    public $skipWhenEmpty = false;
51
    public $readOnly = false;
52
    public $xmlAttributeMap = false;
53
    public $maxDepth = null;
54
    public $excludeIf = null;
55
56 261
    public function __construct(string $class, string $name)
57
    {
58 261
        parent::__construct($class, $name);
59 261
    }
60
61 14
    private function getReflection(): \ReflectionProperty
62
    {
63 14
        return new \ReflectionProperty($this->class, $this->name);
64
    }
65
66 243
    public function setAccessor(string $type, ?string $getter = null, ?string $setter = null):void
67
    {
68 243
        if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
69 14
            $class = $this->getReflection()->getDeclaringClass();
70
71 14
            if (empty($getter)) {
72 10
                if ($class->hasMethod('get' . $this->name) && $class->getMethod('get' . $this->name)->isPublic()) {
73 6
                    $getter = 'get' . $this->name;
74 4
                } elseif ($class->hasMethod('is' . $this->name) && $class->getMethod('is' . $this->name)->isPublic()) {
75 1
                    $getter = 'is' . $this->name;
76 3
                } elseif ($class->hasMethod('has' . $this->name) && $class->getMethod('has' . $this->name)->isPublic()) {
77 1
                    $getter = 'has' . $this->name;
78
                } else {
79 2
                    throw new RuntimeException(sprintf('There is neither a public %s method, nor a public %s method, nor a public %s method in class %s. Please specify which public method should be used for retrieving the value of the property %s.', 'get' . ucfirst($this->name), 'is' . ucfirst($this->name), 'has' . ucfirst($this->name), $this->class, $this->name));
80
                }
81
            }
82
83 12
            if (empty($setter) && !$this->readOnly) {
84 6
                if ($class->hasMethod('set' . $this->name) && $class->getMethod('set' . $this->name)->isPublic()) {
85 5
                    $setter = 'set' . $this->name;
86
                } else {
87 1
                    throw new RuntimeException(sprintf('There is no public %s method in class %s. Please specify which public method should be used for setting the value of the property %s.', 'set' . ucfirst($this->name), $this->class, $this->name));
88
                }
89
            }
90
        }
91
92 240
        $this->getter = $getter;
93 240
        $this->setter = $setter;
94 240
    }
95
96 200
    public function setType(array $type)
97
    {
98 200
        $this->type = $type;
99 200
    }
100
101 1
    public function serialize()
102
    {
103 1
        return serialize([
104 1
            $this->sinceVersion,
105 1
            $this->untilVersion,
106 1
            $this->groups,
107 1
            $this->serializedName,
108 1
            $this->type,
109 1
            $this->xmlCollection,
110 1
            $this->xmlCollectionInline,
111 1
            $this->xmlEntryName,
112 1
            $this->xmlKeyAttribute,
113 1
            $this->xmlAttribute,
114 1
            $this->xmlValue,
115 1
            $this->xmlNamespace,
116 1
            $this->xmlKeyValuePairs,
117 1
            $this->xmlElementCData,
118 1
            $this->getter,
119 1
            $this->setter,
120 1
            $this->inline,
121 1
            $this->readOnly,
122 1
            $this->xmlAttributeMap,
123 1
            $this->maxDepth,
124 1
            parent::serialize(),
125 1
            'xmlEntryNamespace' => $this->xmlEntryNamespace,
126 1
            'xmlCollectionSkipWhenEmpty' => $this->xmlCollectionSkipWhenEmpty,
127 1
            'excludeIf' => $this->excludeIf,
128 1
            'skipWhenEmpty' => $this->skipWhenEmpty,
129
        ]);
130
    }
131
132 1
    public function unserialize($str)
133
    {
134 1
        $unserialized = unserialize($str);
135
        list(
136 1
            $this->sinceVersion,
137 1
            $this->untilVersion,
138 1
            $this->groups,
139 1
            $this->serializedName,
140 1
            $this->type,
141 1
            $this->xmlCollection,
142 1
            $this->xmlCollectionInline,
143 1
            $this->xmlEntryName,
144 1
            $this->xmlKeyAttribute,
145 1
            $this->xmlAttribute,
146 1
            $this->xmlValue,
147 1
            $this->xmlNamespace,
148 1
            $this->xmlKeyValuePairs,
149 1
            $this->xmlElementCData,
150 1
            $this->getter,
151 1
            $this->setter,
152 1
            $this->inline,
153 1
            $this->readOnly,
154 1
            $this->xmlAttributeMap,
155 1
            $this->maxDepth,
156
            $parentStr
157 1
            ) = $unserialized;
158
159 1
        if (isset($unserialized['xmlEntryNamespace'])) {
160
            $this->xmlEntryNamespace = $unserialized['xmlEntryNamespace'];
161
        }
162 1
        if (isset($unserialized['xmlCollectionSkipWhenEmpty'])) {
163 1
            $this->xmlCollectionSkipWhenEmpty = $unserialized['xmlCollectionSkipWhenEmpty'];
164
        }
165 1
        if (isset($unserialized['excludeIf'])) {
166
            $this->excludeIf = $unserialized['excludeIf'];
167
        }
168 1
        if (isset($unserialized['skipWhenEmpty'])) {
169 1
            $this->skipWhenEmpty = $unserialized['skipWhenEmpty'];
170
        }
171
172 1
        parent::unserialize($parentStr);
173 1
    }
174
}
175