1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spatie\DataTransferObject; |
6
|
|
|
|
7
|
|
|
use ReflectionProperty; |
8
|
|
|
|
9
|
|
|
abstract class FieldValidator |
10
|
|
|
{ |
11
|
|
|
public bool $isNullable; |
|
|
|
|
12
|
|
|
|
13
|
|
|
public bool $isMixed; |
14
|
|
|
|
15
|
|
|
public bool $isMixedArray; |
16
|
|
|
|
17
|
|
|
public bool $hasDefaultValue; |
18
|
|
|
|
19
|
|
|
public array $allowedTypes; |
20
|
|
|
|
21
|
|
|
public array $allowedArrayTypes; |
22
|
|
|
|
23
|
|
|
public array $allowedArrayKeyTypes; |
24
|
|
|
|
25
|
|
|
protected static array $typeMapping = [ |
26
|
|
|
'int' => 'integer', |
27
|
|
|
'bool' => 'boolean', |
28
|
|
|
'float' => 'double', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
protected bool $hasTypeDeclaration; |
32
|
|
|
|
33
|
|
|
public static function fromReflection(ReflectionProperty $property): FieldValidator |
34
|
|
|
{ |
35
|
|
|
$docDefinition = null; |
36
|
|
|
|
37
|
|
|
if ($property->getDocComment()) { |
38
|
|
|
preg_match( |
39
|
|
|
DocblockFieldValidator::DOCBLOCK_REGEX, |
40
|
|
|
$property->getDocComment(), |
41
|
|
|
$matches |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$docDefinition = $matches[0] ?? null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($docDefinition) { |
48
|
|
|
return new DocblockFieldValidator($docDefinition, $property->isDefault()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return new PropertyFieldValidator($property); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function isValidType($value): bool |
55
|
|
|
{ |
56
|
|
|
if (! $this->hasTypeDeclaration) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($this->isMixed) { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (is_iterable($value) && $this->isMixedArray) { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($this->isNullable && $value === null) { |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (is_iterable($value)) { |
73
|
|
|
return $this->isValidIterable($value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->isValidValue($value); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function isValidIterable(iterable $iterable): bool |
80
|
|
|
{ |
81
|
|
|
// If the iterable matches one of the normal types, we immediately return true |
82
|
|
|
// For example: custom collection classes type hinted with `MyCollection` |
83
|
|
|
$isValidValue = $this->isValidValue($iterable); |
84
|
|
|
|
85
|
|
|
if ($isValidValue) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// If not, we'll check all individual iterable items and keys |
90
|
|
|
foreach ($iterable as $key => $value) { |
91
|
|
|
$isValidValue = false; |
92
|
|
|
|
93
|
|
|
// First we check whether the value matches the value type definition |
94
|
|
|
foreach ($this->allowedArrayTypes as $type) { |
95
|
|
|
$isValidValue = $this->assertValidType($type, $value); |
96
|
|
|
|
97
|
|
|
// No need to further check this value when a valid type is found |
98
|
|
|
if ($isValidValue) { |
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// If a value is invalid, we immediately return false |
104
|
|
|
if (! $isValidValue) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// We'll assume keys are valid by default, since they can be omitted |
109
|
|
|
$isValidKey = true; |
110
|
|
|
|
111
|
|
|
// Next we check the key's value |
112
|
|
|
foreach ($this->allowedArrayKeyTypes as $keyType) { |
113
|
|
|
$isValidKey = $this->assertValidType($keyType, $key); |
114
|
|
|
|
115
|
|
|
// No need to further check this jey when a valid type is found |
116
|
|
|
if ($isValidKey) { |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// If a key type is invalid, we'll immediately return |
122
|
|
|
if (! $isValidKey) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Moving on to checking the next $key => $value pair |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// If value and key type checks pass, we can return true |
130
|
|
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function isValidValue($value): bool |
134
|
|
|
{ |
135
|
|
|
foreach ($this->allowedTypes as $type) { |
136
|
|
|
// We'll check the type of this value against all allowed types, if one matches we're good |
137
|
|
|
$isValidType = $this->assertValidType($type, $value); |
138
|
|
|
|
139
|
|
|
if ($isValidType) { |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function assertValidType(string $type, $value): bool |
148
|
|
|
{ |
149
|
|
|
return $value instanceof $type || gettype($value) === $type; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|