1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cascader; |
6
|
|
|
|
7
|
|
|
use Cascader\Exception\InvalidClassException; |
8
|
|
|
use Cascader\Exception\InvalidOptionsException; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionParameter; |
11
|
|
|
|
12
|
|
|
class Cascader |
13
|
|
|
{ |
14
|
|
|
const ARGUMENT_CLASS = '__class__'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $className |
18
|
|
|
* @param array $options |
19
|
|
|
* |
20
|
|
|
* @throws InvalidClassException |
21
|
|
|
* @throws InvalidOptionsException |
22
|
|
|
* |
23
|
|
|
* @return object |
24
|
|
|
*/ |
25
|
12 |
|
public function create(string $className, array $options) |
26
|
|
|
{ |
27
|
12 |
|
$reflectionClass = $this->getReflectionClass($className); |
28
|
10 |
|
$options = Options::fromArray($options); |
29
|
|
|
|
30
|
9 |
|
$arguments = $this->resolveArguments($reflectionClass, $options); |
31
|
|
|
|
32
|
8 |
|
return new $className(...$arguments); |
33
|
|
|
} |
34
|
|
|
|
35
|
12 |
|
protected function getReflectionClass(string $className) : ReflectionClass |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
12 |
|
$reflectionClass = new ReflectionClass($className); |
39
|
1 |
|
} catch (\ReflectionException $ex) { |
40
|
1 |
|
throw InvalidClassException::nonExistingClass($className); |
41
|
|
|
} |
42
|
|
|
|
43
|
11 |
|
if (! $reflectionClass->isInstantiable()) { |
44
|
1 |
|
throw InvalidClassException::nonInstantiableClass($className); |
45
|
|
|
} |
46
|
|
|
|
47
|
10 |
|
return $reflectionClass; |
48
|
|
|
} |
49
|
|
|
|
50
|
9 |
|
protected function resolveArguments(ReflectionClass $reflectionClass, Options $options) : array |
51
|
|
|
{ |
52
|
9 |
|
$constructor = $reflectionClass->getConstructor(); |
53
|
|
|
|
54
|
9 |
|
if (null === $constructor) { |
55
|
1 |
|
return []; |
56
|
|
|
} |
57
|
|
|
|
58
|
8 |
|
$arguments = []; |
59
|
|
|
|
60
|
8 |
|
$constructorParameters = $constructor->getParameters(); |
61
|
|
|
|
62
|
8 |
|
foreach ($constructorParameters as $parameter) { |
63
|
8 |
|
$arguments[] = $this->resolveArgument($parameter, $options); |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
return $arguments; |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
protected function resolveArgument(ReflectionParameter $reflectionParameter, Options $options) |
70
|
|
|
{ |
71
|
8 |
|
$parameterAnalysis = new ParameterAnalysis($reflectionParameter, $options); |
72
|
|
|
|
73
|
8 |
|
if (! $parameterAnalysis->hasArgument()) { |
74
|
6 |
|
if (! $parameterAnalysis->isOptional()) { |
75
|
1 |
|
throw InvalidOptionsException::missingMandatoryParameter($parameterAnalysis); |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
return $parameterAnalysis->getDefaultValue(); |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
$argument = $parameterAnalysis->getArgument(); |
82
|
|
|
|
83
|
7 |
|
if ($parameterAnalysis->isArray()) { |
84
|
3 |
|
return $this->resolveArrayArgument($argument); |
85
|
|
|
} |
86
|
|
|
|
87
|
7 |
|
if ($parameterAnalysis->isNestedObject()) { |
88
|
2 |
|
return $this->createNestedObject($parameterAnalysis->getType(), $argument); |
89
|
|
|
} |
90
|
|
|
|
91
|
7 |
|
return $argument; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
protected function resolveArrayArgument(array $argument) : array |
95
|
|
|
{ |
96
|
3 |
|
foreach ($argument as $k => $value) { |
97
|
3 |
|
if (\is_array($value) && isset($value[self::ARGUMENT_CLASS])) { |
98
|
1 |
|
$argument[$k] = $this->createNestedObject('', $value); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
return $argument; |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
protected function createNestedObject(string $className, array $arguments) |
106
|
|
|
{ |
107
|
3 |
|
if (isset($arguments[self::ARGUMENT_CLASS])) { |
108
|
2 |
|
$className = $arguments[self::ARGUMENT_CLASS]; |
109
|
2 |
|
unset($arguments[self::ARGUMENT_CLASS]); |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
return $this->create($className, $arguments); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|