Completed
Push — master ( 3e05ae...99069b )
by Asmir
12s
created

PropertyMetadata::isCollectionMap()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 4
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 4
rs 9.2
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 273
    public function __construct(string $class, string $name)
57
    {
58 273
        parent::__construct($class, $name);
59 273
    }
60
61 14
    private function getReflection(): \ReflectionProperty
62
    {
63 14
        return new \ReflectionProperty($this->class, $this->name);
64
    }
65
66 255
    public function setAccessor(string $type, ?string $getter = null, ?string $setter = null):void
67
    {
68 255
        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 252
        $this->getter = $getter;
93 252
        $this->setter = $setter;
94 252
    }
95
96 212
    public function setType(array $type)
97
    {
98 212
        $this->type = $type;
99 212
    }
100
101 17
    public static function isCollectionList(array $type = null): bool
102
    {
103 17
        return is_array($type)
104 17
            && $type['name'] === 'array'
105 17
            && isset($type['params'][0])
106 17
            && !isset($type['params'][1]);
107
    }
108
109 17
    public static function isCollectionMap(array $type = null): bool
110
    {
111 17
        return is_array($type)
112 17
            && $type['name'] === 'array'
113 17
            && isset($type['params'][0])
114 17
            && isset($type['params'][1]);
115
    }
116
117 1
    public function serialize()
118
    {
119 1
        return serialize([
120 1
            $this->sinceVersion,
121 1
            $this->untilVersion,
122 1
            $this->groups,
123 1
            $this->serializedName,
124 1
            $this->type,
125 1
            $this->xmlCollection,
126 1
            $this->xmlCollectionInline,
127 1
            $this->xmlEntryName,
128 1
            $this->xmlKeyAttribute,
129 1
            $this->xmlAttribute,
130 1
            $this->xmlValue,
131 1
            $this->xmlNamespace,
132 1
            $this->xmlKeyValuePairs,
133 1
            $this->xmlElementCData,
134 1
            $this->getter,
135 1
            $this->setter,
136 1
            $this->inline,
137 1
            $this->readOnly,
138 1
            $this->xmlAttributeMap,
139 1
            $this->maxDepth,
140 1
            parent::serialize(),
141 1
            'xmlEntryNamespace' => $this->xmlEntryNamespace,
142 1
            'xmlCollectionSkipWhenEmpty' => $this->xmlCollectionSkipWhenEmpty,
143 1
            'excludeIf' => $this->excludeIf,
144 1
            'skipWhenEmpty' => $this->skipWhenEmpty,
145
        ]);
146
    }
147
148 1
    public function unserialize($str)
149
    {
150 1
        $unserialized = unserialize($str);
151
        list(
152 1
            $this->sinceVersion,
153 1
            $this->untilVersion,
154 1
            $this->groups,
155 1
            $this->serializedName,
156 1
            $this->type,
157 1
            $this->xmlCollection,
158 1
            $this->xmlCollectionInline,
159 1
            $this->xmlEntryName,
160 1
            $this->xmlKeyAttribute,
161 1
            $this->xmlAttribute,
162 1
            $this->xmlValue,
163 1
            $this->xmlNamespace,
164 1
            $this->xmlKeyValuePairs,
165 1
            $this->xmlElementCData,
166 1
            $this->getter,
167 1
            $this->setter,
168 1
            $this->inline,
169 1
            $this->readOnly,
170 1
            $this->xmlAttributeMap,
171 1
            $this->maxDepth,
172
            $parentStr
173 1
            ) = $unserialized;
174
175 1
        if (isset($unserialized['xmlEntryNamespace'])) {
176
            $this->xmlEntryNamespace = $unserialized['xmlEntryNamespace'];
177
        }
178 1
        if (isset($unserialized['xmlCollectionSkipWhenEmpty'])) {
179 1
            $this->xmlCollectionSkipWhenEmpty = $unserialized['xmlCollectionSkipWhenEmpty'];
180
        }
181 1
        if (isset($unserialized['excludeIf'])) {
182
            $this->excludeIf = $unserialized['excludeIf'];
183
        }
184 1
        if (isset($unserialized['skipWhenEmpty'])) {
185 1
            $this->skipWhenEmpty = $unserialized['skipWhenEmpty'];
186
        }
187
188 1
        parent::unserialize($parentStr);
189 1
    }
190
}
191