1 | <?php |
||
10 | class ArgumentSpecification |
||
11 | { |
||
12 | use TypeValidator; |
||
13 | |||
14 | /** |
||
15 | * A name of the argument. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $name = ''; |
||
20 | /** |
||
21 | * A type of the argument. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $type = ''; |
||
26 | /** |
||
27 | * Indicates whether an argument must be optional. |
||
28 | * |
||
29 | * @var bool |
||
30 | */ |
||
31 | protected $optional = false; |
||
32 | /** |
||
33 | * Indicates whether an argument must be passed by reference. |
||
34 | * |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $passedByReference = false; |
||
38 | /** |
||
39 | * A parameter to compare specification with. |
||
40 | * |
||
41 | * @var \ReflectionParameter |
||
42 | */ |
||
43 | private $parameter; |
||
44 | /** |
||
45 | * A bag for errors. |
||
46 | * |
||
47 | * @var BadFunctionDefinitionException |
||
48 | */ |
||
49 | private $exception; |
||
50 | |||
51 | /** |
||
52 | * ArgumentSpecification constructor. |
||
53 | * |
||
54 | * @param string $name |
||
55 | * A name of the argument. |
||
56 | */ |
||
57 | 15 | public function __construct(string $name) |
|
61 | |||
62 | /** |
||
63 | * Defines a type of the argument. |
||
64 | * |
||
65 | * @param string $type |
||
66 | * A type of the argument. |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | 14 | public function setType(string $type): self |
|
76 | |||
77 | /** |
||
78 | * Defines whether an argument is optional. |
||
79 | * |
||
80 | * @param bool $state |
||
81 | * An indicator of a state. |
||
82 | * |
||
83 | * @return $this |
||
84 | */ |
||
85 | 15 | public function setOptional(bool $state): self |
|
91 | |||
92 | /** |
||
93 | * Defines whether an argument is passed by reference. |
||
94 | * |
||
95 | * @param bool $state |
||
96 | * An indicator of a state. |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | 15 | public function setPassedByReference(bool $state): self |
|
106 | |||
107 | /** |
||
108 | * Validates whether argument specification according to parameter definition. |
||
109 | * |
||
110 | * @param \ReflectionParameter $parameter |
||
111 | * A parameter to compare specification with. |
||
112 | * @param BadFunctionDefinitionException|null $exception |
||
113 | * An exception to collect violations. |
||
114 | */ |
||
115 | 13 | public function validate(\ReflectionParameter $parameter, BadFunctionDefinitionException $exception = null) |
|
140 | |||
141 | /** |
||
142 | * Checks that parameter is named correctly. |
||
143 | */ |
||
144 | 13 | private function checkName() |
|
156 | |||
157 | /** |
||
158 | * Checks optionality of the parameter. |
||
159 | */ |
||
160 | 13 | private function checkOptional() |
|
176 | |||
177 | /** |
||
178 | * Checks referenceability of the parameter. |
||
179 | */ |
||
180 | 13 | private function checkReference() |
|
196 | |||
197 | /** |
||
198 | * Checks a type of the parameter. |
||
199 | */ |
||
200 | 13 | private function checkType() |
|
225 | } |
||
226 |