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 = $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
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function resolveNullable(string $definition): bool |
34
|
|
|
{ |
35
|
|
|
if (! $definition) { |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (Str::contains($definition, ['mixed', 'null', '?'])) { |
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function resolveIsMixed(string $definition): bool |
47
|
|
|
{ |
48
|
|
|
return Str::contains($definition, ['mixed']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function resolveIsMixedArray(string $definition): bool |
52
|
|
|
{ |
53
|
|
|
$types = $this->normaliseTypes(...explode('|', $definition)); |
54
|
|
|
|
55
|
|
|
foreach ($types as $type) { |
56
|
|
|
if (in_array($type, ['iterable', 'array'])) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function resolveAllowedTypes(string $definition): array |
65
|
|
|
{ |
66
|
|
|
return $this->normaliseTypes(...explode('|', $definition)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function resolveAllowedArrayTypes(string $definition): array |
70
|
|
|
{ |
71
|
|
|
return $this->normaliseTypes(...(new RecursiveIteratorIterator(new RecursiveArrayIterator(array_map( |
72
|
|
|
function (string $type) { |
73
|
|
|
if (! $type) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (strpos($type, '[]') !== false) { |
78
|
|
|
return str_replace('[]', '', $type); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (strpos($type, 'iterable<') !== false) { |
82
|
|
|
return str_replace(['iterable<', '>'], ['', ''], $type); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (is_subclass_of($type, DataTransferObjectCollection::class)) { |
86
|
|
|
return $this->resolveAllowedArrayTypesFromCollection($type); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return null; |
90
|
|
|
}, |
91
|
|
|
explode('|', $definition) |
92
|
|
|
))))); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|