1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Minime\Annotations\Types; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use Minime\Annotations\Interfaces\TypeInterface; |
8
|
|
|
use Minime\Annotations\ParserException; |
9
|
|
|
|
10
|
|
|
class ConcreteType implements TypeInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Process a value to be a concrete annotation |
15
|
|
|
* |
16
|
|
|
* @param string $value json string |
17
|
|
|
* @param string $class name of concrete annotation type (class) |
18
|
|
|
* @throws \Minime\Annotations\ParserException |
19
|
|
|
* @return object |
20
|
|
|
*/ |
21
|
|
|
public function parse($value, $class = null) |
22
|
|
|
{ |
23
|
|
|
if (! class_exists($class)) { |
24
|
|
|
throw new ParserException("Concrete annotation expects {$class} to exist."); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$prototype = (new JsonType)->parse($value); |
28
|
|
|
|
29
|
|
|
if ($prototype instanceof stdClass) { |
30
|
|
|
if (! $this->isPrototypeSchemaValid($prototype)) { |
31
|
|
|
throw new ParserException("Only arrays should be used to configure concrete annotation method calls."); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->makeInstance($class, $prototype); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (is_array($prototype)) { |
38
|
|
|
return $this->makeConstructSugarInjectionInstance($class, $prototype); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
throw new ParserException("Json value for annotation({$class}) must be of type object or array."); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function makeConstructSugarInjectionInstance($class, array $prototype) { |
45
|
|
|
$reflection = new ReflectionClass($class); |
46
|
|
|
$instance = $reflection->newInstanceArgs($prototype); |
47
|
|
|
|
48
|
|
|
return $instance; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creates and hydrates a concrete annotation class |
53
|
|
|
* |
54
|
|
|
* @param string $class full qualified class name |
55
|
|
|
* @param stdClass $prototype object prototype |
56
|
|
|
* @return object hydrated concrete annotation class |
57
|
|
|
*/ |
58
|
|
|
protected function makeInstance($class, stdClass $prototype) |
59
|
|
|
{ |
60
|
|
|
$reflection = new ReflectionClass($class); |
61
|
|
|
if (isset($prototype->__construct)) { |
62
|
|
|
$instance = $reflection->newInstanceArgs($prototype->__construct); |
63
|
|
|
unset($prototype->__construct); |
64
|
|
|
} else { |
65
|
|
|
$instance = $reflection->newInstance(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->doMethodConfiguration($instance, $prototype); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Do configuration injection through method calls |
73
|
|
|
* |
74
|
|
|
* @param object $instance concrete annotation instance |
75
|
|
|
* @param stdClass $prototype object prototype |
76
|
|
|
* @return object hydrated concrete annotation class |
77
|
|
|
*/ |
78
|
|
|
protected function doMethodConfiguration($instance, stdClass $prototype) |
79
|
|
|
{ |
80
|
|
|
foreach ($prototype as $method => $args) { |
81
|
|
|
call_user_func_array([$instance, $method], $args); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $instance; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Validates a prototype object |
89
|
|
|
* |
90
|
|
|
* @param stdClass $prototype object prototype |
91
|
|
|
* @return boolean true if prototype is valid |
92
|
|
|
*/ |
93
|
|
|
protected function isPrototypeSchemaValid(stdclass $prototype) |
94
|
|
|
{ |
95
|
|
|
foreach ($prototype as $method => $args) { |
96
|
|
|
if (! is_array($args)) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|