|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spaark\CompositeUtils\Factory\Reflection; |
|
4
|
|
|
|
|
5
|
|
|
use Spaark\CompositeUtils\Factory\BaseFactory; |
|
6
|
|
|
use Spaark\CompositeUtils\Service\RawPropertyAccessor; |
|
7
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Reflector as SpaarkReflector; |
|
8
|
|
|
use \Reflector as PHPNativeReflector; |
|
9
|
|
|
|
|
10
|
|
|
abstract class ReflectorFactory extends BaseFactory |
|
11
|
|
|
{ |
|
12
|
|
|
const REFLECTION_OBJECT = null; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var PHPNativeReflector |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $reflector; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var RawPropertyAccessor |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $accessor; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var SpaarkReflector |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $object; |
|
28
|
|
|
|
|
29
|
4 |
|
public function __construct(PHPNativeReflector $reflector) |
|
30
|
|
|
{ |
|
31
|
4 |
|
$class = static::REFLECTION_OBJECT; |
|
32
|
|
|
|
|
33
|
4 |
|
$this->object = new $class(); |
|
34
|
4 |
|
$this->accessor = new RawPropertyAccessor($this->object); |
|
35
|
4 |
|
$this->reflector = $reflector; |
|
36
|
4 |
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
protected function parseDocComment() |
|
39
|
|
|
{ |
|
40
|
4 |
|
preg_match_all |
|
41
|
|
|
( |
|
42
|
|
|
'/^' |
|
43
|
|
|
. '[ \t]*\*[ \t]*' |
|
44
|
|
|
. '@([a-zA-Z]+)' |
|
45
|
|
|
. '(.*)' |
|
46
|
4 |
|
. '$/m', |
|
47
|
4 |
|
$this->reflector->getDocComment(), |
|
|
|
|
|
|
48
|
|
|
$matches |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
4 |
|
foreach ($matches[0] as $key => $value) |
|
52
|
|
|
{ |
|
53
|
4 |
|
$name = strtolower($matches[1][$key]); |
|
54
|
4 |
|
$value = trim($matches[2][$key]); |
|
55
|
|
|
|
|
56
|
4 |
|
if (isset($this->acceptedParams[$name])) |
|
57
|
|
|
{ |
|
58
|
4 |
|
call_user_func |
|
59
|
|
|
( |
|
60
|
4 |
|
array($this, $this->acceptedParams[$name]), |
|
|
|
|
|
|
61
|
|
|
$name, $value |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
4 |
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
protected function setBool($name, $value) |
|
68
|
|
|
{ |
|
69
|
4 |
|
switch(strtolower($value)) |
|
70
|
|
|
{ |
|
71
|
4 |
|
case '': |
|
72
|
|
|
case 'true': |
|
73
|
4 |
|
$value = true; |
|
74
|
4 |
|
break; |
|
75
|
|
|
|
|
76
|
|
|
case 'false': |
|
77
|
|
|
$value = false; |
|
78
|
|
|
break; |
|
79
|
|
|
|
|
80
|
|
|
default: |
|
81
|
|
|
$value = (boolean)$value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
4 |
|
$this->accessor->setRawValue($name, $value); |
|
85
|
4 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function setInt($name, $value) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->accessor->setRawValue($name, (int)$value); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function setMixed($name, $value) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->accessor->setRawValue($name, $value); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: