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