1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cascader; |
6
|
|
|
|
7
|
|
|
use Cascader\Exception\OptionNotSetException; |
8
|
|
|
use ReflectionParameter; |
9
|
|
|
use ReflectionType; |
10
|
|
|
|
11
|
|
|
final class ParameterAnalysis |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ReflectionParameter |
15
|
|
|
*/ |
16
|
|
|
private $parameter; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ReflectionType|null |
20
|
|
|
*/ |
21
|
|
|
private $parameterType; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $parameterTypeName; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var mixed |
30
|
|
|
*/ |
31
|
|
|
private $argument; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $hasArgument; |
37
|
|
|
|
38
|
8 |
|
public function __construct(ReflectionParameter $parameter, Options $options) |
39
|
|
|
{ |
40
|
8 |
|
$this->parameter = $parameter; |
41
|
8 |
|
$this->setParameterType(); |
42
|
8 |
|
$this->setArgument($options); |
43
|
8 |
|
} |
44
|
|
|
|
45
|
1 |
|
public function getName() : string |
46
|
|
|
{ |
47
|
1 |
|
return $this->parameter->getName(); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function getType() : string |
51
|
|
|
{ |
52
|
2 |
|
return $this->parameterTypeName; |
53
|
|
|
} |
54
|
|
|
|
55
|
8 |
|
public function hasArgument() : bool |
56
|
|
|
{ |
57
|
8 |
|
return $this->hasArgument; |
58
|
|
|
} |
59
|
|
|
|
60
|
7 |
|
public function getArgument() |
61
|
|
|
{ |
62
|
7 |
|
return $this->argument; |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
public function isOptional() : bool |
66
|
|
|
{ |
67
|
6 |
|
return $this->parameter->isOptional(); |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
public function getDefaultValue() |
71
|
|
|
{ |
72
|
5 |
|
return $this->parameter->getDefaultValue(); |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
public function isNestedObject() : bool |
76
|
|
|
{ |
77
|
7 |
|
return null !== $this->parameterType && !$this->parameterType->isBuiltin() && \is_array($this->argument); |
78
|
|
|
} |
79
|
|
|
|
80
|
7 |
|
public function isArray() : bool |
81
|
|
|
{ |
82
|
7 |
|
return 'array' === $this->parameterTypeName && \is_array($this->argument); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function getDeclaringClass() : string |
86
|
|
|
{ |
87
|
1 |
|
return $this->parameter->getDeclaringClass()->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
8 |
|
private function setParameterType() |
91
|
|
|
{ |
92
|
8 |
|
if (null !== ($parameterType = $this->parameter->getType())) { |
93
|
8 |
|
$this->parameterType = $parameterType; |
94
|
8 |
|
$this->parameterTypeName = method_exists($parameterType, 'getName') |
95
|
|
|
? $parameterType->getName() //PHP 7.1 |
96
|
8 |
|
: (string) $parameterType; |
97
|
|
|
} |
98
|
8 |
|
} |
99
|
|
|
|
100
|
8 |
|
private function setArgument(Options $options) |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
8 |
|
$this->argument = $options->get($this->parameter->name); |
104
|
7 |
|
$this->hasArgument = true; |
105
|
6 |
|
} catch (OptionNotSetException $ex) { |
106
|
6 |
|
$this->hasArgument = false; |
107
|
|
|
} |
108
|
8 |
|
} |
109
|
|
|
} |
110
|
|
|
|