1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larapie\DataTransferObject\Resolvers; |
4
|
|
|
|
5
|
|
|
use Throwable; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
use Larapie\DataTransferObject\Annotations\Inherit; |
8
|
|
|
use Larapie\DataTransferObject\Property\PropertyType; |
9
|
|
|
use Larapie\DataTransferObject\Exceptions\ConstraintInheritanceException; |
10
|
|
|
|
11
|
|
|
class PropertyTypeResolver |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ReflectionProperty |
15
|
|
|
*/ |
16
|
|
|
protected $reflection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $annotations; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* TypeResolver constructor. |
25
|
|
|
* @param ReflectionProperty $reflection |
26
|
|
|
* @param array $annotations |
27
|
|
|
*/ |
28
|
36 |
|
final public function __construct(ReflectionProperty $reflection, array $annotations) |
29
|
|
|
{ |
30
|
36 |
|
$this->reflection = $reflection; |
31
|
36 |
|
$this->annotations = $annotations; |
32
|
36 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return PropertyType |
36
|
|
|
*/ |
37
|
36 |
|
public function resolve(): PropertyType |
38
|
|
|
{ |
39
|
36 |
|
$type = new PropertyType(); |
40
|
|
|
|
41
|
36 |
|
$docComment = $this->reflection->getDocComment(); |
42
|
|
|
|
43
|
36 |
|
if (! $docComment) { |
44
|
1 |
|
$type->setNullable(true); |
45
|
|
|
|
46
|
1 |
|
if (($parentType = $this->resolvePossibleParentType()) !== null) { |
47
|
|
|
return $parentType; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return $type; |
51
|
|
|
} |
52
|
|
|
|
53
|
36 |
|
preg_match('/\@var ((?:(?:[\w|\\\\])+(?:\[\])?)+)/', $docComment, $matches); |
|
|
|
|
54
|
|
|
|
55
|
36 |
|
if (! count($matches)) { |
56
|
2 |
|
$type->setNullable(true); |
57
|
|
|
|
58
|
2 |
|
if (($parentType = $this->resolvePossibleParentType()) !== null) { |
59
|
1 |
|
return $parentType; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return $type; |
63
|
|
|
} |
64
|
|
|
|
65
|
35 |
|
$varDocComment = end($matches); |
66
|
|
|
|
67
|
35 |
|
$resolver = new VarTypeResolver($this->reflection); |
68
|
35 |
|
$types = $resolver->resolve($varDocComment); |
69
|
34 |
|
$type->setTypes($types); |
70
|
34 |
|
$type->setArrayTypes(str_replace('[]', '', $types)); |
71
|
34 |
|
$type->setInitialized(true); |
72
|
34 |
|
$type->setNullable(strpos($varDocComment, 'null') !== false); |
73
|
|
|
|
74
|
34 |
|
return $type; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
protected function resolvePossibleParentType() |
78
|
|
|
{ |
79
|
2 |
|
foreach ($this->annotations as $annotation) { |
80
|
1 |
|
if ($annotation instanceof Inherit) { |
81
|
1 |
|
$type = $this->getParentType(); |
82
|
1 |
|
if ($type->isInitialized()) { |
83
|
1 |
|
return $type; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
1 |
|
} |
88
|
|
|
|
89
|
1 |
|
protected function getParentType(): PropertyType |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
1 |
|
if ($parentClass = $this->reflection->getDeclaringClass()->getParentClass()) { |
93
|
1 |
|
$parentProperty = $parentClass->getProperty($this->reflection->getName()); |
94
|
|
|
} |
95
|
|
|
} catch (Throwable $exception) { |
96
|
|
|
throw new ConstraintInheritanceException('There is no parent property to inherit from'); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return (new static($parentProperty, (new AnnotationResolver($parentProperty))->resolve()))->resolve(); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|