Passed
Push — master ( 0c064e...87b116 )
by Arthur
08:29 queued 10s
created

PropertyType::isNullable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 $hasType = false;
21
22
    /**
23
     * @return array
24
     */
25
    public function getTypes(): array
26
    {
27
        return $this->types;
28
    }
29
30
    /**
31
     * @param array $types
32
     */
33
    public function setTypes(array $types): void
34
    {
35
        $this->types = $types;
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function getArrayTypes(): array
42
    {
43
        return $this->arrayTypes;
44
    }
45
46
    /**
47
     * @param array $arrayTypes
48
     */
49
    public function setArrayTypes(array $arrayTypes): void
50
    {
51
        $this->arrayTypes = $arrayTypes;
52
    }
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
    public function setNullable(bool $nullable): void
66
    {
67
        $this->nullable = $nullable;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function isHasType(): bool
74
    {
75
        return $this->hasType;
76
    }
77
78
    /**
79
     * @param bool $hasType
80
     */
81
    public function setHasType(bool $hasType): void
82
    {
83
        $this->hasType = $hasType;
84
    }
85
86
    public function isValid($value): bool
87
    {
88
        if (! $this->hasType) {
89
            return true;
90
        }
91
92
        if ($this->nullable && $value === null) {
93
            return true;
94
        }
95
96
        foreach ($this->types as $currentType) {
97
            $isValidType = $this->assertTypeEquals($currentType, $value);
98
99
            if ($isValidType) {
100
                return true;
101
            }
102
        }
103
104
        return false;
105
    }
106
107
    protected function assertTypeEquals(string $type, $value): bool
108
    {
109
        if (strpos($type, '[]') !== false) {
110
            return $this->isValidGenericCollection($type, $value);
111
        }
112
113
        if ($type === 'mixed' && $value !== null) {
114
            return true;
115
        }
116
117
        return $value instanceof $type
118
            || gettype($value) === (self::TYPE_MAPPING[$type] ?? $type);
119
    }
120
121
    protected function isValidGenericCollection(string $type, $collection): bool
122
    {
123
        if (! is_array($collection)) {
124
            return false;
125
        }
126
127
        $valueType = str_replace('[]', '', $type);
128
129
        foreach ($collection as $value) {
130
            if (! $this->assertTypeEquals($valueType, $value)) {
131
                return false;
132
            }
133
        }
134
135
        return true;
136
    }
137
}
138