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