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