1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spaark\CompositeUtils\Factory\Reflection; |
4
|
|
|
|
5
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite; |
6
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionProperty; |
7
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionParameter; |
8
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType; |
9
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType; |
10
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType; |
11
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType; |
12
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType; |
13
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\StringType; |
14
|
|
|
use \ReflectionProperty as PHPNativeReflectionProperty; |
15
|
|
|
|
16
|
|
|
class ReflectionPropertyFactory extends ReflectorFactory |
17
|
|
|
{ |
18
|
|
|
const REFLECTION_OBJECT = ReflectionProperty::class; |
19
|
|
|
|
20
|
|
|
protected $acceptedParams = |
21
|
|
|
[ |
22
|
|
|
'readable' => 'setBool', |
23
|
|
|
'writable' => 'setBool', |
24
|
|
|
'var' => 'setType' |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
public static function fromName($class, $property) |
28
|
|
|
{ |
29
|
|
|
return new static(new PHPNativeReflectionProperty |
30
|
|
|
( |
31
|
|
|
$class, $property |
32
|
|
|
)); |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
public function build(ReflectionComposite $parent, $default) |
36
|
|
|
{ |
37
|
4 |
|
$this->accessor->setRawValue('owner', $parent); |
38
|
4 |
|
$this->accessor->setRawValue('defaultValue', $default); |
39
|
4 |
|
$this->accessor->setRawValue |
40
|
|
|
( |
41
|
4 |
|
'name', |
42
|
4 |
|
$this->reflector->getName() |
43
|
|
|
); |
44
|
|
|
|
45
|
4 |
|
$this->parseDocComment(); |
46
|
|
|
|
47
|
4 |
|
return $this->object; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
protected function setType($name, $value) |
|
|
|
|
51
|
|
|
{ |
52
|
4 |
View Code Duplication |
if ($value{0} !== '?') |
|
|
|
|
53
|
|
|
{ |
54
|
4 |
|
$nullable = false; |
55
|
|
|
} |
56
|
|
|
else |
57
|
|
|
{ |
58
|
|
|
$nullable = true; |
59
|
|
|
$value = substr($value, 1); |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
View Code Duplication |
if (substr($value, -2) !== '[]') |
|
|
|
|
63
|
|
|
{ |
64
|
4 |
|
$collection = false; |
65
|
|
|
} |
66
|
|
|
else |
67
|
|
|
{ |
68
|
|
|
$collection = true; |
69
|
|
|
$value = substr($value, 0, -2); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
switch ($value) |
73
|
|
|
{ |
74
|
4 |
|
case 'string': |
75
|
4 |
|
$class = new StringType(); |
76
|
4 |
|
break; |
77
|
4 |
|
case 'int': |
78
|
4 |
|
case 'integer': |
79
|
4 |
|
$class = new IntegerType(); |
80
|
4 |
|
break; |
81
|
4 |
|
case 'bool': |
82
|
4 |
|
case 'boolean': |
83
|
|
|
$class = new BooleanType(); |
84
|
|
|
break; |
85
|
4 |
|
case 'mixed': |
86
|
4 |
|
case '': |
87
|
|
|
$class = new MixedType(); |
88
|
|
|
break; |
89
|
4 |
|
case 'null': |
90
|
|
|
$class = new NullType(); |
91
|
|
|
break; |
92
|
|
|
default: |
93
|
|
|
$useStatements = |
94
|
4 |
|
$this->object->owner->namespace->useStatements; |
|
|
|
|
95
|
|
|
|
96
|
4 |
|
if ($useStatements->contains($value)) |
97
|
|
|
{ |
98
|
4 |
|
$value = $useStatements[$value]->classname; |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
$class = new ObjectType($value); |
102
|
|
|
} |
103
|
|
|
|
104
|
4 |
|
if ($nullable) |
105
|
|
|
{ |
106
|
|
|
(new RawPropertyAccessor($class)) |
107
|
|
|
->setRawValue('nullable', $value); |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
if ($collection) |
111
|
|
|
{ |
112
|
|
|
$class = new CollectionType($class); |
113
|
|
|
} |
114
|
|
|
|
115
|
4 |
|
$this->accessor->setRawValue('type', $class); |
116
|
4 |
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.