1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spatie\DataTransferObject; |
6
|
|
|
|
7
|
|
|
use RecursiveArrayIterator; |
8
|
|
|
use RecursiveIteratorIterator; |
9
|
|
|
|
10
|
|
|
class DocblockFieldValidator extends FieldValidator |
11
|
|
|
{ |
12
|
|
|
public const DOCBLOCK_REGEX = <<<REGEXP |
13
|
|
|
/ |
14
|
|
|
@var ( # Starting with `@var `, we'll capture the definition the follows |
15
|
|
|
|
16
|
|
|
(?: # Not explicitly capturing this group, |
17
|
|
|
# which contains repeated sets of type definitions |
18
|
|
|
|
19
|
|
|
(?: # Not explicitly capturing this group |
20
|
|
|
[\w?|\\\\<>,\s] # Matches type definitions like `int|string|\My\Object|array<int, string>` |
21
|
|
|
)+ # These definitions can be repeated |
22
|
|
|
|
23
|
|
|
(?: # Not explicitly capturing this group |
24
|
|
|
\[] # Matches array definitions like `int[]` |
25
|
|
|
)? # Array definitions are optional though |
26
|
|
|
|
27
|
|
|
)+ # Repeated sets of type definitions |
28
|
|
|
|
29
|
|
|
) # The whole definition after `@var ` is captured in one group |
30
|
|
|
/x |
31
|
|
|
REGEXP; |
32
|
|
|
|
33
|
|
|
public function __construct(string $definition, bool $hasDefaultValue = false) |
34
|
|
|
{ |
35
|
|
|
preg_match( |
36
|
|
|
DocblockFieldValidator::DOCBLOCK_REGEX, |
37
|
|
|
$definition, |
38
|
|
|
$matches |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$definition = trim($matches[1] ?? ''); |
42
|
|
|
|
43
|
|
|
$this->hasTypeDeclaration = $definition !== ''; |
44
|
|
|
$this->hasDefaultValue = $hasDefaultValue; |
45
|
|
|
$this->isNullable = $this->resolveNullable($definition); |
46
|
|
|
$this->isMixed = $this->resolveIsMixed($definition); |
47
|
|
|
$this->isMixedArray = $this->resolveIsMixedArray($definition); |
48
|
|
|
$this->allowedTypes = $this->resolveAllowedTypes($definition); |
49
|
|
|
$this->allowedArrayTypes = $this->resolveAllowedArrayTypes($definition); |
50
|
|
|
$this->allowedArrayKeyTypes = $this->resolveAllowedArrayKeyTypes($definition); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function resolveNullable(string $definition): bool |
54
|
|
|
{ |
55
|
|
|
if (! $definition) { |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (Str::contains($definition, ['mixed', 'null', '?'])) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function resolveIsMixed(string $definition): bool |
67
|
|
|
{ |
68
|
|
|
return Str::contains($definition, ['mixed']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function resolveIsMixedArray(string $definition): bool |
72
|
|
|
{ |
73
|
|
|
$types = $this->normaliseTypes(...explode('|', $definition)); |
74
|
|
|
|
75
|
|
|
foreach ($types as $type) { |
76
|
|
|
if (in_array($type, ['iterable', 'array'])) { |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function resolveAllowedTypes(string $definition): array |
85
|
|
|
{ |
86
|
|
|
return $this->normaliseTypes(...explode('|', $definition)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function resolveAllowedArrayTypes(string $definition): array |
90
|
|
|
{ |
91
|
|
|
return $this->normaliseTypes(...(new RecursiveIteratorIterator(new RecursiveArrayIterator(array_map( |
92
|
|
|
function (string $type) { |
93
|
|
|
if (! $type) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (strpos($type, '[]') !== false) { |
98
|
|
|
return str_replace('[]', '', $type); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (strpos($type, '>') !== false) { |
102
|
|
|
preg_match('/([\w\\\\]+)(>)/', $type, $matches); |
103
|
|
|
|
104
|
|
|
return $matches[1]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (is_subclass_of($type, DataTransferObjectCollection::class)) { |
108
|
|
|
return $this->resolveAllowedArrayTypesFromCollection($type); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return null; |
112
|
|
|
}, |
113
|
|
|
explode('|', $definition) |
114
|
|
|
))))); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function resolveAllowedArrayKeyTypes(string $definition): array |
118
|
|
|
{ |
119
|
|
|
return $this->normaliseTypes(...array_map( |
120
|
|
|
function (string $type) { |
121
|
|
|
if (strpos($type, '<') === false) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
preg_match('/(<)([\w\\\\]+)(,)/', $type, $matches); |
126
|
|
|
|
127
|
|
|
return $matches[2] ?? null; |
128
|
|
|
}, |
129
|
|
|
explode('|', $definition) |
130
|
|
|
)); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|