Completed
Pull Request — master (#944)
by Asmir
02:57
created

PropertyMetadata::isCollectionMap()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 4
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 20
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 268
    public function __construct($class, $name)
57
    {
58 268
        parent::__construct($class, $name);
59 268
    }
60
61 250
    public function setAccessor($type, $getter = null, $setter = null)
62
    {
63 250
        if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
64 14
            $class = $this->reflection->getDeclaringClass();
65
66 14
            if (empty($getter)) {
67 10
                if ($class->hasMethod('get' . $this->name) && $class->getMethod('get' . $this->name)->isPublic()) {
68 6
                    $getter = 'get' . $this->name;
69 4
                } elseif ($class->hasMethod('is' . $this->name) && $class->getMethod('is' . $this->name)->isPublic()) {
70 1
                    $getter = 'is' . $this->name;
71 3
                } elseif ($class->hasMethod('has' . $this->name) && $class->getMethod('has' . $this->name)->isPublic()) {
72 1
                    $getter = 'has' . $this->name;
73
                } else {
74 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));
75
                }
76
            }
77
78 12
            if (empty($setter) && !$this->readOnly) {
79 6
                if ($class->hasMethod('set' . $this->name) && $class->getMethod('set' . $this->name)->isPublic()) {
80 5
                    $setter = 'set' . $this->name;
81
                } else {
82 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));
83
                }
84
            }
85
        }
86
87 247
        $this->getter = $getter;
88 247
        $this->setter = $setter;
89 247
    }
90
91
    public function setValue($obj, $value)
92
    {
93
        if (null === $this->setter) {
94
            parent::setValue($obj, $value);
95
            return;
96
        }
97
98
        $obj->{$this->setter}($value);
99
    }
100
101 207
    public function setType(array $type)
102
    {
103 207
        $this->type = $type;
104 207
    }
105
106 12
    public static function isCollectionList(array $type = null): bool
107
    {
108 12
        return is_array($type)
109 12
            && $type['name'] === 'array'
110 12
            && isset($type['params'][0])
111 12
            && !isset($type['params'][1]);
112
    }
113
114
    public static function isCollectionMap(array $type = null): bool
115
    {
116
        return is_array($type)
117
            && $type['name'] === 'array'
118
            && isset($type['params'][0])
119
            && isset($type['params'][1]);
120
    }
121
122 1
    public function serialize()
123
    {
124 1
        return serialize([
125 1
            $this->sinceVersion,
126 1
            $this->untilVersion,
127 1
            $this->groups,
128 1
            $this->serializedName,
129 1
            $this->type,
130 1
            $this->xmlCollection,
131 1
            $this->xmlCollectionInline,
132 1
            $this->xmlEntryName,
133 1
            $this->xmlKeyAttribute,
134 1
            $this->xmlAttribute,
135 1
            $this->xmlValue,
136 1
            $this->xmlNamespace,
137 1
            $this->xmlKeyValuePairs,
138 1
            $this->xmlElementCData,
139 1
            $this->getter,
140 1
            $this->setter,
141 1
            $this->inline,
142 1
            $this->readOnly,
143 1
            $this->xmlAttributeMap,
144 1
            $this->maxDepth,
145 1
            parent::serialize(),
146 1
            'xmlEntryNamespace' => $this->xmlEntryNamespace,
147 1
            'xmlCollectionSkipWhenEmpty' => $this->xmlCollectionSkipWhenEmpty,
148 1
            'excludeIf' => $this->excludeIf,
149 1
            'skipWhenEmpty' => $this->skipWhenEmpty,
150
        ]);
151
    }
152
153 1
    public function unserialize($str)
154
    {
155 1
        $unserialized = unserialize($str);
156
        list(
157 1
            $this->sinceVersion,
158 1
            $this->untilVersion,
159 1
            $this->groups,
160 1
            $this->serializedName,
161 1
            $this->type,
162 1
            $this->xmlCollection,
163 1
            $this->xmlCollectionInline,
164 1
            $this->xmlEntryName,
165 1
            $this->xmlKeyAttribute,
166 1
            $this->xmlAttribute,
167 1
            $this->xmlValue,
168 1
            $this->xmlNamespace,
169 1
            $this->xmlKeyValuePairs,
170 1
            $this->xmlElementCData,
171 1
            $this->getter,
172 1
            $this->setter,
173 1
            $this->inline,
174 1
            $this->readOnly,
175 1
            $this->xmlAttributeMap,
176 1
            $this->maxDepth,
177
            $parentStr
178 1
            ) = $unserialized;
179
180 1
        if (isset($unserialized['xmlEntryNamespace'])) {
181
            $this->xmlEntryNamespace = $unserialized['xmlEntryNamespace'];
182
        }
183 1
        if (isset($unserialized['xmlCollectionSkipWhenEmpty'])) {
184 1
            $this->xmlCollectionSkipWhenEmpty = $unserialized['xmlCollectionSkipWhenEmpty'];
185
        }
186 1
        if (isset($unserialized['excludeIf'])) {
187
            $this->excludeIf = $unserialized['excludeIf'];
188
        }
189 1
        if (isset($unserialized['skipWhenEmpty'])) {
190 1
            $this->skipWhenEmpty = $unserialized['skipWhenEmpty'];
191
        }
192
193 1
        parent::unserialize($parentStr);
194 1
    }
195
}
196