PropertyMetadata   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 412
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 412
ccs 66
cts 66
cp 1
rs 9.52
wmc 36

28 Methods

Rating   Name   Duplication   Size   Complexity  
A inline() 0 3 1
A normalizationOptions() 0 3 1
A setGroups() 0 4 2
A setReadOnly() 0 3 1
A setDefaultValue() 0 4 1
A type() 0 3 1
A setSince() 0 3 1
A since() 0 3 1
A defaultValue() 0 3 1
A name() 0 3 1
A className() 0 3 1
A setNormalizationOptions() 0 3 1
A hasGroups() 0 9 3
A setUntil() 0 3 1
A accessor() 0 3 1
A alias() 0 3 1
A denormalizationOptions() 0 3 1
A matchVersion() 0 11 5
A setInline() 0 3 1
A until() 0 3 1
A setAlias() 0 3 1
A isDefaultValue() 0 3 2
A __construct() 0 4 1
A setDenormalizationOptions() 0 3 1
A readOnly() 0 3 1
A groups() 0 3 1
A setAccessor() 0 3 1
A setType() 0 3 1
1
<?php
2
3
namespace Bdf\Serializer\Metadata;
4
5
use Bdf\Serializer\PropertyAccessor\PropertyAccessorInterface;
6
use Bdf\Serializer\Type\Type;
7
8
/**
9
 * PropertyMetadata
10
 *
11
 * @author  Seb
12
 *
13
 * @psalm-suppress PropertyNotSetInConstructor
14
 */
15
class PropertyMetadata
16
{
17
    /**
18
     * The owner class name
19
     *
20
     * @var string
21
     */
22
    public $class;
23
24
    /**
25
     * The property name
26
     *
27
     * @var string
28
     */
29
    public $name;
30
31
    /**
32
     * The property alias
33
     *
34
     * @var string
35
     */
36
    public $alias;
37
38
    /**
39
     * The property type infos
40
     *
41
     * @var Type
42
     */
43
    public $type;
44
45
    /**
46
     * The property groups
47
     *
48
     * @var array
49
     */
50
    public $groups = [];
51
52
    /**
53
     * The property accessor
54
     *
55
     * @var PropertyAccessorInterface
56
     */
57
    public $accessor;
58
59
    /**
60
     * The version when the property has been added.
61
     *
62
     * @var string|null
63
     */
64
    public $since;
65
66
    /**
67
     * The version when the property has been removed.
68
     *
69
     * @var string|null
70
     */
71
    public $until;
72
73
    /**
74
     * The read only state of the property.
75
     *
76
     * @var bool
77
     */
78
    public $readOnly = false;
79
80
    /**
81
     * The inline state of the property.
82
     *
83
     * @var bool
84
     */
85
    public $inline = false;
86
87
    /**
88
     * The default value of the property.
89
     *
90
     * @var mixed
91
     */
92
    public $defaultValue;
93
94
    /**
95
     * A flag to know if the property has a default value.
96
     *
97
     * @var bool
98
     */
99
    private $hasDefaultValue = false;
100
101
    /**
102
     * Flag to know if the property used php type.
103
     *
104
     * @var bool
105
     */
106
    public $isPhpTyped = false;
107
108
    /**
109
     * The context options for normalization.
110
     *
111
     * @var null|array
112
     */
113
    public $normalizationOptions;
114
115
    /**
116
     * The context options for denormalization.
117
     *
118
     * @var null|array
119
     */
120
    public $denormalizationOptions;
121
122
    /**
123
     * PropertyMetadata constructor.
124
     *
125
     * @param string $class
126
     * @param string $name
127
     */
128 294
    public function __construct(string $class, string $name)
129
    {
130 294
        $this->class = $class;
131 294
        $this->name = $name;
132
    }
133
134
    /**
135
     * Check whether the property should be skipped
136
     *
137
     * @param array $groups
138
     *
139
     * @return boolean  True if the property has one of the groups
140
     */
141 12
    public function hasGroups(array $groups)
142
    {
143 12
        foreach ($groups as $group) {
144 12
            if (isset($this->groups[$group])) {
145 10
                return true;
146
            }
147
        }
148
149 10
        return false;
150
    }
151
152
    /**
153
     * Get the property class name
154
     *
155
     * @return string
156
     */
157 4
    public function className()
158
    {
159 4
        return $this->class;
160
    }
161
162
    /**
163
     * Get the property name
164
     *
165
     * @return string
166
     */
167 196
    public function name()
168
    {
169 196
        return $this->name;
170
    }
171
172
    /**
173
     * Set the property type infos
174
     *
175
     * @param Type $type
176
     */
177 270
    public function setType(Type $type): void
178
    {
179 270
        $this->type = $type;
180
    }
181
182
    /**
183
     * Get the property type
184
     *
185
     * @return Type
186
     */
187 42
    public function type()
188
    {
189 42
        return $this->type;
190
    }
191
192
    /**
193
     * Set the property alias
194
     *
195
     * @param string $alias
196
     */
197 242
    public function setAlias($alias): void
198
    {
199 242
        $this->alias = $alias;
200
    }
201
202
    /**
203
     * Get the property alias
204
     *
205
     * @return string
206
     */
207 8
    public function alias()
208
    {
209 8
        return $this->alias;
210
    }
211
212
    /**
213
     * Set the property groups
214
     *
215
     * @param array $groups
216
     */
217 268
    public function setGroups(array $groups): void
218
    {
219 268
        foreach ($groups as $group) {
220 94
            $this->groups[$group] = $group;
221
        }
222
    }
223
224
    /**
225
     * Get the property groups
226
     *
227
     * @return array
228
     */
229 4
    public function groups()
230
    {
231 4
        return $this->groups;
232
    }
233
234
    /**
235
     * Set the property accessor
236
     *
237
     * @param PropertyAccessorInterface $accessor
238
     */
239 242
    public function setAccessor(PropertyAccessorInterface $accessor): void
240
    {
241 242
        $this->accessor = $accessor;
242
    }
243
244
    /**
245
     * Get the property accessor
246
     *
247
     * @return PropertyAccessorInterface
248
     */
249 20
    public function accessor()
250
    {
251 20
        return $this->accessor;
252
    }
253
254
    /**
255
     * Set the version when the property has been added
256
     *
257
     * @param string|null $version
258
     */
259 270
    public function setSince($version): void
260
    {
261 270
        $this->since = $version;
262
    }
263
264
    /**
265
     * Get the version when the property has been added
266
     *
267
     * @return string|null
268
     */
269 10
    public function since()
270
    {
271 10
        return $this->since;
272
    }
273
274
    /**
275
     * Set the version when the property has been removed
276
     *
277
     * @param string|null $version
278
     */
279 270
    public function setUntil($version): void
280
    {
281 270
        $this->until = $version;
282
    }
283
284
    /**
285
     * Get the version when the property has been removed
286
     *
287
     * @return string|null
288
     */
289 10
    public function until()
290
    {
291 10
        return $this->until;
292
    }
293
294
    /**
295
     * Test if the property match to the version
296
     *
297
     * @param string $version
298
     *
299
     * @return boolean
300
     */
301 20
    public function matchVersion($version)
302
    {
303 20
        if (null !== $this->since && version_compare($version, $this->since, '<')) {
304 8
            return false;
305
        }
306
307 18
        if (null !== $this->until && version_compare($version, $this->until, '>')) {
308 6
            return false;
309
        }
310
311 16
        return true;
312
    }
313
314
    /**
315
     * Set the value of the read only flag.
316
     *
317
     * @param bool $flag
318
     */
319 242
    public function setReadOnly($flag): void
320
    {
321 242
        $this->readOnly = $flag;
322
    }
323
324
    /**
325
     * Get the value of the read only flag.
326
     *
327
     * @return bool
328
     */
329 6
    public function readOnly()
330
    {
331 6
        return $this->readOnly;
332
    }
333
334
    /**
335
     * Set the property inline.
336
     *
337
     * The result of the normalization of this property
338
     * will be add on the same layer of the property.
339
     * Works only if the normalization of this property is an array.
340
     *
341
     * @param bool $flag
342
     */
343 242
    public function setInline(bool $flag): void
344
    {
345 242
        $this->inline = $flag;
346
    }
347
348
    /**
349
     * Get the inline state of the property.
350
     *
351
     * @return bool
352
     */
353 6
    public function inline(): bool
354
    {
355 6
        return $this->inline;
356
    }
357
358
    /**
359
     * Set the value to skip.
360
     *
361
     * @param mixed $defaultValue
362
     */
363 218
    public function setDefaultValue($defaultValue): void
364
    {
365 218
        $this->defaultValue = $defaultValue;
366 218
        $this->hasDefaultValue = true;
367
    }
368
369
    /**
370
     * Get the value to skip.
371
     *
372
     * @return mixed
373
     */
374 4
    public function defaultValue()
375
    {
376 4
        return $this->defaultValue;
377
    }
378
379
    /**
380
     * Check whether the value is the default value of the property.
381
     *
382
     * @param mixed $value
383
     */
384 6
    public function isDefaultValue($value): bool
385
    {
386 6
        return $this->hasDefaultValue && $this->defaultValue === $value;
387
    }
388
389
    /**
390
     * Set the options for normalization context
391
     *
392
     * @param null|array $options
393
     */
394 242
    public function setNormalizationOptions($options): void
395
    {
396 242
        $this->normalizationOptions = $options;
397
    }
398
399
    /**
400
     * Get property options for normalization context.
401
     *
402
     * @return null|array
403
     */
404 2
    public function normalizationOptions()
405
    {
406 2
        return $this->normalizationOptions;
407
    }
408
409
    /**
410
     * Set the options for denormalization context
411
     *
412
     * @param null|array $options
413
     */
414 242
    public function setDenormalizationOptions($options): void
415
    {
416 242
        $this->denormalizationOptions = $options;
417
    }
418
419
    /**
420
     * Get property options for denormalization context.
421
     *
422
     * @return null|array
423
     */
424 2
    public function denormalizationOptions()
425
    {
426 2
        return $this->denormalizationOptions;
427
    }
428
}
429