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