PropertyType   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 95.56%

Importance

Changes 0
Metric Value
wmc 23
eloc 39
dl 0
loc 131
ccs 43
cts 45
cp 0.9556
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setTypes() 0 3 1
A getArrayTypes() 0 3 1
A setInitialized() 0 3 1
A setArrayTypes() 0 3 1
A isNullable() 0 3 1
A setNullable() 0 3 1
A getTypes() 0 3 1
A isValidGenericCollection() 0 15 4
A assertTypeEquals() 0 12 5
A isValid() 0 19 6
A isInitialized() 0 3 1
1
<?php
2
3
namespace Larapie\DataTransferObject\Property;
4
5
class PropertyType
6
{
7
    /** @var array */
8
    protected const TYPE_MAPPING = [
9
        'int' => 'integer',
10
        'bool' => 'boolean',
11
        'float' => 'double',
12
    ];
13
14
    protected $types = [];
15
16
    protected $arrayTypes = [];
17
18
    protected $nullable = false;
19
20
    protected $initialized = false;
21
22
    /**
23
     * @return array
24
     */
25 41
    public function getTypes(): array
26
    {
27 41
        return $this->types;
28
    }
29
30
    /**
31
     * @param array $types
32
     */
33 34
    public function setTypes(array $types): void
34
    {
35 34
        $this->types = $types;
36 34
    }
37
38
    /**
39
     * @return array
40
     */
41 2
    public function getArrayTypes(): array
42
    {
43 2
        return $this->arrayTypes;
44
    }
45
46
    /**
47
     * @param array $arrayTypes
48
     */
49 34
    public function setArrayTypes(array $arrayTypes): void
50
    {
51 34
        $this->arrayTypes = $arrayTypes;
52 34
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function isNullable(): bool
58
    {
59
        return $this->nullable;
60
    }
61
62
    /**
63
     * @param bool $nullable
64
     */
65 35
    public function setNullable(bool $nullable): void
66
    {
67 35
        $this->nullable = $nullable;
68 35
    }
69
70
    /**
71
     * @return bool
72
     */
73 1
    public function isInitialized(): bool
74
    {
75 1
        return $this->initialized;
76
    }
77
78
    /**
79
     * @param bool $initialized
80
     */
81 34
    public function setInitialized(bool $initialized): void
82
    {
83 34
        $this->initialized = $initialized;
84 34
    }
85
86 41
    public function isValid($value): bool
87
    {
88 41
        if (! $this->initialized) {
89 1
            return true;
90
        }
91
92 40
        if ($this->nullable && $value === null) {
93 2
            return true;
94
        }
95
96 38
        foreach ($this->types as $currentType) {
97 38
            $isValidType = $this->assertTypeEquals($currentType, $value);
98
99 38
            if ($isValidType) {
100 38
                return true;
101
            }
102
        }
103
104 8
        return false;
105
    }
106
107 38
    protected function assertTypeEquals(string $type, $value): bool
108
    {
109 38
        if (strpos($type, '[]') !== false) {
110 7
            return $this->isValidGenericCollection($type, $value);
111
        }
112
113 38
        if ($type === 'mixed' && $value !== null) {
114 1
            return true;
115
        }
116
117 37
        return $value instanceof $type
118 37
            || gettype($value) === (self::TYPE_MAPPING[$type] ?? $type);
119
    }
120
121 7
    protected function isValidGenericCollection(string $type, $collection): bool
122
    {
123 7
        if (! is_array($collection)) {
124 1
            return false;
125
        }
126
127 6
        $valueType = str_replace('[]', '', $type);
128
129 6
        foreach ($collection as $value) {
130 6
            if (! $this->assertTypeEquals($valueType, $value)) {
131 6
                return false;
132
            }
133
        }
134
135 5
        return true;
136
    }
137
}
138