Passed
Pull Request — master (#1549)
by
unknown
02:36
created

PropertyMetadata::setUnionDiscriminator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Metadata;
6
7
use JMS\Serializer\Exception\InvalidMetadataException;
8
use JMS\Serializer\Expression\Expression;
9
use Metadata\PropertyMetadata as BasePropertyMetadata;
10
11
class PropertyMetadata extends BasePropertyMetadata
12
{
13
    public const ACCESS_TYPE_PROPERTY = 'property';
14
    public const ACCESS_TYPE_PUBLIC_METHOD = 'public_method';
15
16
    /**
17
     * @var string|null
18
     */
19
    public $sinceVersion;
20
21
    /**
22
     * @var string|null
23
     */
24
    public $untilVersion;
25
26
    /**
27
     * @var string[]|null
28
     */
29
    public $groups;
30
31
    /**
32
     * @var string|null
33
     */
34
    public $serializedName;
35
36
37
    /**
38
     * @var string|null
39
     */
40
    public $unionDiscriminatorField;
41
42
    /**
43
     * @var array<string, string>|null
44
     */
45
    public $unionDiscriminatorMap;
46 280
47
    /**
48 280
     * @var array|null
49
     */
50
    public $type;
51 280
52 280
    /**
53
     * @var bool
54
     */
55 280
    public $xmlCollection = false;
56
57 280
    /**
58
     * @var bool
59 280
     */
60
    public $xmlCollectionInline = false;
61
62 260
    /**
63
     * @var bool
64 260
     */
65 14
    public $xmlCollectionSkipWhenEmpty = true;
66
67 14
    /**
68 10
     * @var string|null
69 6
     */
70 4
    public $xmlEntryName;
71 1
72 3
    /**
73 1
     * @var string|null
74
     */
75 2
    public $xmlEntryNamespace;
76
77
    /**
78
     * @var string|null
79 12
     */
80 6
    public $xmlKeyAttribute;
81 5
82
    /**
83 1
     * @var bool
84
     */
85
    public $xmlAttribute = false;
86
87
    /**
88 257
     * @var bool
89 257
     */
90 257
    public $xmlValue = false;
91
92 217
    /**
93
     * @var string|null
94 217
     */
95 217
    public $xmlNamespace;
96
97 17
    /**
98
     * @var bool
99 17
     */
100 17
    public $xmlKeyValuePairs = false;
101 17
102 17
    /**
103
     * @var bool
104
     */
105 17
    public $xmlElementCData = true;
106
107 17
    /**
108 17
     * @var string|null
109 17
     */
110 17
    public $getter;
111
112
    /**
113 1
     * @var string|null
114
     */
115 1
    public $setter;
116 1
117 1
    /**
118 1
     * @var bool
119 1
     */
120 1
    public $inline = false;
121 1
122 1
    /**
123 1
     * @var bool
124 1
     */
125 1
    public $skipWhenEmpty = false;
126 1
127 1
    /**
128 1
     * @var bool
129 1
     */
130 1
    public $readOnly = false;
131 1
132 1
    /**
133 1
     * @var bool
134 1
     */
135 1
    public $xmlAttributeMap = false;
136 1
137 1
    /**
138 1
     * @var int|null
139 1
     */
140 1
    public $maxDepth = null;
141 1
142
    /**
143
     * @var string|Expression|null
144
     */
145 1
    public $excludeIf = null;
146
147 1
    /**
148
     * @var bool|null
149 1
     */
150 1
    public $hasDefault;
151 1
152 1
    /**
153 1
     * @var mixed|null
154 1
     */
155 1
    public $defaultValue;
156 1
157 1
    /**
158 1
     * @internal
159 1
     *
160 1
     * @var bool
161 1
     */
162 1
    public $forceReflectionAccess = false;
163 1
164 1
    public function __construct(string $class, string $name)
165 1
    {
166 1
        parent::__construct($class, $name);
167 1
168 1
        try {
169
            $class = $this->getReflection()->getDeclaringClass();
170 1
            $this->forceReflectionAccess = $class->isInternal() || $class->getProperty($name)->isStatic();
171
        } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
172 1
        }
173
    }
174
175 1
    private function getReflection(): \ReflectionProperty
176 1
    {
177
        return new \ReflectionProperty($this->class, $this->name);
178 1
    }
179
180
    public function setAccessor(string $type, ?string $getter = null, ?string $setter = null): void
181 1
    {
182 1
        if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
183
            $class = $this->getReflection()->getDeclaringClass();
184 1
185 1
            if (empty($getter)) {
186
                if ($class->hasMethod('get' . $this->name) && $class->getMethod('get' . $this->name)->isPublic()) {
187
                    $getter = 'get' . $this->name;
188 1
                } elseif ($class->hasMethod('is' . $this->name) && $class->getMethod('is' . $this->name)->isPublic()) {
189 1
                    $getter = 'is' . $this->name;
190
                } elseif ($class->hasMethod('has' . $this->name) && $class->getMethod('has' . $this->name)->isPublic()) {
191
                    $getter = 'has' . $this->name;
192
                } else {
193
                    throw new InvalidMetadataException(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));
194
                }
195
            }
196
197
            if (empty($setter) && !$this->readOnly) {
198
                if ($class->hasMethod('set' . $this->name) && $class->getMethod('set' . $this->name)->isPublic()) {
199
                    $setter = 'set' . $this->name;
200
                } else {
201
                    throw new InvalidMetadataException(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));
202
                }
203
            }
204
        }
205
206
        $this->getter = $getter;
207
        $this->setter = $setter;
208
    }
209
210
    public function setUnionDiscriminator(string $field, ?array $map): void
211
    {
212
        $this->unionDiscriminatorField = $field;
213
        $this->unionDiscriminatorMap = $map;
214
    }
215
216
    public function setType(array $type): void
217
    {
218
        $this->type = $type;
219
    }
220
221
    public static function isCollectionList(?array $type = null): bool
222
    {
223
        return is_array($type)
224
            && 'array' === $type['name']
225
            && isset($type['params'][0])
226
            && !isset($type['params'][1]);
227
    }
228
229
    public static function isCollectionMap(?array $type = null): bool
230
    {
231
        return is_array($type)
232
            && 'array' === $type['name']
233
            && isset($type['params'][0])
234
            && isset($type['params'][1]);
235
    }
236
237
    protected function serializeToArray(): array
238
    {
239
        return [
240
            $this->sinceVersion,
241
            $this->untilVersion,
242
            $this->groups,
243
            $this->serializedName,
244
            $this->unionDiscriminatorField,
245
            $this->unionDiscriminatorMap,
246
            $this->type,
247
            $this->xmlCollection,
248
            $this->xmlCollectionInline,
249
            $this->xmlEntryName,
250
            $this->xmlKeyAttribute,
251
            $this->xmlAttribute,
252
            $this->xmlValue,
253
            $this->xmlNamespace,
254
            $this->xmlKeyValuePairs,
255
            $this->xmlElementCData,
256
            $this->getter,
257
            $this->setter,
258
            $this->inline,
259
            $this->readOnly,
260
            $this->xmlAttributeMap,
261
            $this->maxDepth,
262
            $this->xmlEntryNamespace,
263
            $this->xmlCollectionSkipWhenEmpty,
264
            $this->excludeIf,
265
            $this->skipWhenEmpty,
266
            $this->forceReflectionAccess,
267
            $this->hasDefault,
268
            $this->defaultValue,
269
            parent::serializeToArray(),
270
        ];
271
    }
272
273
    protected function unserializeFromArray(array $data): void
274
    {
275
        [
276
            $this->sinceVersion,
277
            $this->untilVersion,
278
            $this->groups,
279
            $this->serializedName,
280
            $this->unionDiscriminatorField,
281
            $this->unionDiscriminatorMap,
282
            $this->type,
283
            $this->xmlCollection,
284
            $this->xmlCollectionInline,
285
            $this->xmlEntryName,
286
            $this->xmlKeyAttribute,
287
            $this->xmlAttribute,
288
            $this->xmlValue,
289
            $this->xmlNamespace,
290
            $this->xmlKeyValuePairs,
291
            $this->xmlElementCData,
292
            $this->getter,
293
            $this->setter,
294
            $this->inline,
295
            $this->readOnly,
296
            $this->xmlAttributeMap,
297
            $this->maxDepth,
298
            $this->xmlEntryNamespace,
299
            $this->xmlCollectionSkipWhenEmpty,
300
            $this->excludeIf,
301
            $this->skipWhenEmpty,
302
            $this->forceReflectionAccess,
303
            $this->hasDefault,
304
            $this->defaultValue,
305
            $parentData,
306
        ] = $data;
307
308
        parent::unserializeFromArray($parentData);
309
    }
310
}
311