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 = '/@var ((?:(?:[\w?|\\\\<>])+(?:\[])?)+)/'; |
13
|
|
|
|
14
|
|
|
public function __construct(string $definition, bool $hasDefaultValue = false) |
15
|
|
|
{ |
16
|
|
|
preg_match( |
17
|
|
|
DocblockFieldValidator::DOCBLOCK_REGEX, |
18
|
|
|
$definition, |
19
|
|
|
$matches |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
$definition = trim($matches[1] ?? ''); |
23
|
|
|
|
24
|
|
|
$this->hasTypeDeclaration = $definition !== ''; |
25
|
|
|
$this->hasDefaultValue = $hasDefaultValue; |
26
|
|
|
$this->isNullable = $this->resolveNullable($definition); |
27
|
|
|
$this->isMixed = $this->resolveIsMixed($definition); |
28
|
|
|
$this->isMixedArray = $this->resolveIsMixedArray($definition); |
29
|
|
|
$this->allowedTypes = $this->resolveAllowedTypes($definition); |
30
|
|
|
$this->allowedArrayTypes = $this->resolveAllowedArrayTypes($definition); |
31
|
|
|
$this->allowedArrayKeyTypes = $this->resolveAllowedArrayKeyTypes($definition); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function resolveNullable(string $definition): bool |
35
|
|
|
{ |
36
|
|
|
if (! $definition) { |
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (Str::contains($definition, ['mixed', 'null', '?'])) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function resolveIsMixed(string $definition): bool |
48
|
|
|
{ |
49
|
|
|
return Str::contains($definition, ['mixed']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function resolveIsMixedArray(string $definition): bool |
53
|
|
|
{ |
54
|
|
|
$types = $this->normaliseTypes(...explode('|', $definition)); |
55
|
|
|
|
56
|
|
|
foreach ($types as $type) { |
57
|
|
|
if (in_array($type, ['iterable', 'array'])) { |
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function resolveAllowedTypes(string $definition): array |
66
|
|
|
{ |
67
|
|
|
return $this->normaliseTypes(...explode('|', $definition)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function resolveAllowedArrayTypes(string $definition): array |
71
|
|
|
{ |
72
|
|
|
return $this->normaliseTypes(...(new RecursiveIteratorIterator(new RecursiveArrayIterator(array_map( |
73
|
|
|
function (string $type) { |
74
|
|
|
if (! $type) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (strpos($type, '[]') !== false) { |
79
|
|
|
return str_replace('[]', '', $type); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (strpos($type, '>') !== false) { |
83
|
|
|
preg_match('/([\w\\\\]+)(>)/', $type, $matches); |
84
|
|
|
|
85
|
|
|
return $matches[1]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (is_subclass_of($type, DataTransferObjectCollection::class)) { |
89
|
|
|
return $this->resolveAllowedArrayTypesFromCollection($type); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return null; |
93
|
|
|
}, |
94
|
|
|
explode('|', $definition) |
95
|
|
|
))))); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function resolveAllowedArrayKeyTypes(string $definition): array |
99
|
|
|
{ |
100
|
|
|
return $this->normaliseTypes(...array_map( |
101
|
|
|
function (string $type) { |
102
|
|
|
if (strpos($type, '<') === false) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
preg_match('/(<)([\w\\\\]+)(,)/', $type, $matches); |
107
|
|
|
|
108
|
|
|
return $matches[2] ?? null; |
109
|
|
|
}, |
110
|
|
|
explode('|', $definition) |
111
|
|
|
)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|