Passed
Push — master ( 0756fe...706dc7 )
by Arthur
14:47
created

PropertyType::assertTypeEquals()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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