|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vox\Metadata; |
|
4
|
|
|
|
|
5
|
|
|
use Metadata\PropertyMetadata as BaseMetadata; |
|
6
|
|
|
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
|
|
9
|
|
|
class PropertyMetadata extends BaseMetadata |
|
10
|
|
|
{ |
|
11
|
|
|
use AnnotationsTrait; |
|
12
|
|
|
|
|
13
|
|
|
public $type; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param ReflectionClass $class |
|
17
|
|
|
* @param string $name |
|
18
|
|
|
*/ |
|
19
|
18 |
|
public function __construct($class, $name) |
|
20
|
|
|
{ |
|
21
|
18 |
|
parent::__construct($class, $name); |
|
22
|
|
|
|
|
23
|
18 |
|
$this->type = $this->parseType(); |
|
24
|
18 |
|
} |
|
25
|
|
|
|
|
26
|
10 |
|
public function getValue($obj) |
|
27
|
|
|
{ |
|
28
|
10 |
|
if ($obj instanceof AccessInterceptorValueHolderInterface) { |
|
29
|
5 |
|
$obj = $obj->getWrappedValueHolderValue(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
10 |
|
return parent::getValue($obj); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
15 |
|
public function setValue($obj, $value) |
|
36
|
|
|
{ |
|
37
|
15 |
|
if ($obj instanceof AccessInterceptorValueHolderInterface) { |
|
38
|
|
|
$obj = $obj->getWrappedValueHolderValue(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
15 |
|
parent::setValue($obj, $value); |
|
42
|
15 |
|
} |
|
43
|
|
|
|
|
44
|
18 |
|
private function parseType() |
|
45
|
|
|
{ |
|
46
|
18 |
|
$docComment = $this->reflection->getDocComment(); |
|
47
|
|
|
|
|
48
|
18 |
|
preg_match('/@var\s+(([^\[\]\s]+)(\[\])?)/', $docComment, $matches); |
|
49
|
|
|
|
|
50
|
18 |
|
$fullType = $matches[1] ?? null; |
|
51
|
18 |
|
$type = $matches[2] ?? null; |
|
52
|
|
|
|
|
53
|
18 |
|
if (null === $type) { |
|
54
|
12 |
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
17 |
|
$uses = $this->getClassUses(); |
|
58
|
|
|
|
|
59
|
17 |
|
foreach ($uses as $use) { |
|
60
|
13 |
|
if (preg_match("/{$type}$/", $use)) { |
|
61
|
|
|
return $use . ($matches[3] ?? null); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
13 |
|
if (class_exists("$use\\$type")) { |
|
65
|
13 |
|
return "$use\\$type" . ($matches[3] ?? null); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
15 |
|
return $fullType; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
17 |
|
private function getClassUses(): array |
|
73
|
|
|
{ |
|
74
|
17 |
|
$filename = $this->reflection->getDeclaringClass()->getFileName(); |
|
75
|
|
|
|
|
76
|
17 |
|
if (is_file($filename)) { |
|
77
|
13 |
|
$contents = file_get_contents($filename); |
|
78
|
|
|
|
|
79
|
13 |
|
preg_match_all('/use\s+(.*);/', $contents, $matches); |
|
80
|
|
|
|
|
81
|
13 |
|
$uses = $matches[1] ?? []; |
|
82
|
|
|
|
|
83
|
13 |
|
$matches = []; |
|
84
|
|
|
|
|
85
|
13 |
|
preg_match('/namespace\s+(.*);/', $contents, $matches); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
13 |
|
if (!empty($matches[1])) { |
|
88
|
13 |
|
array_push($uses, $matches[1]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
13 |
|
return $uses; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
5 |
|
return []; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|