4
4
namespace TheWebSolver \Codegarage \Generator ;
5
5
6
6
use Nette \Utils \Type ;
7
- use RuntimeException ;
8
7
use InvalidArgumentException ;
9
8
use Nette \PhpGenerator \Helpers ;
10
9
use Nette \PhpGenerator \Literal ;
11
10
use Nette \PhpGenerator \PromotedParameter ;
11
+ use TheWebSolver \Codegarage \Generator \Enum \Argument ;
12
12
use TheWebSolver \Codegarage \Generator \Error \ParamExtractionException as ExtractionError ;
13
13
14
14
/**
24
24
* }
25
25
*/
26
26
final class Parameter {
27
- public const REFERENCE = 'isReference ' ;
28
- public const NULLABLE = 'isNullable ' ;
29
- public const VARIADIC = 'isVariadic ' ;
30
- public const PROMOTED = 'isPromoted ' ;
31
- public const POSITION = 'position ' ;
32
- public const DEFAULT = 'defaultValue ' ;
33
- public const TYPE = 'type ' ;
34
- public const NAME = 'name ' ;
35
-
36
- /**
37
- * List of constructor arguments with their respective type.
38
- *
39
- * @var array<string,string>
40
- */
41
- public const CREATION_ARGS = array (
42
- self ::REFERENCE => 'bool ' ,
43
- self ::NULLABLE => 'bool ' ,
44
- self ::VARIADIC => 'bool ' ,
45
- self ::PROMOTED => 'bool ' ,
46
- self ::POSITION => 'int ' ,
47
- self ::DEFAULT => 'mixed ' ,
48
- self ::TYPE => '?string ' ,
49
- self ::NAME => 'string ' ,
50
- );
51
-
52
27
private bool $ isDefaultValueAvailable = false ;
53
28
private bool $ isDefaultValueConstant = false ;
54
29
private ?string $ invalidValueKey = null ;
@@ -153,7 +128,7 @@ public static function extractFrom( string $string, ?callable $validator = null
153
128
}
154
129
155
130
[ $ name , $ value ] = $ pair ;
156
- $ args [ $ name ] = ! isset ( self :: CREATION_ARGS [ $ name ] )
131
+ $ args [ $ name ] = ! Argument:: tryFrom ( $ name )
157
132
? self ::throwExtractionError ( ExtractionError::INVALID_CREATION_ARG )
158
133
: $ value ;
159
134
}
@@ -162,7 +137,9 @@ public static function extractFrom( string $string, ?callable $validator = null
162
137
self ::throwExtractionError ( ExtractionError::FROM_VALIDATOR );
163
138
}
164
139
165
- return isset ( $ args [ self ::NAME ] ) ? $ args : self ::throwExtractionError ( ExtractionError::NO_NAME_ARG );
140
+ return ! isset ( $ args [ Argument::Name->value ] )
141
+ ? self ::throwExtractionError ( ExtractionError::NO_NAME_ARG )
142
+ : $ args ;
166
143
}
167
144
168
145
/**
@@ -180,34 +157,33 @@ public static function createFrom( array $args ): self {
180
157
);
181
158
}
182
159
183
- public static function validateCreationArg ( string $ name ): void {
184
- if ( ! array_key_exists ( $ name , self ::CREATION_ARGS ) ) {
185
- throw new RuntimeException (
186
- sprintf (
187
- 'The parameter data must be for one of the creation args: %1$s. "%2$s" given ' ,
188
- implode ( ' | ' , array_keys ( self ::CREATION_ARGS ) ),
189
- $ name
190
- )
191
- );
192
- }
193
- }
194
-
195
160
/** @param array{position?:int,defaultValue?:mixed} $newValues */
196
161
public function recreateWith ( array $ newValues ): self {
197
162
return self ::createFrom ( array ( ...$ this ->toArray (), ...$ newValues ) );
198
163
}
199
164
200
- /** @return ArgsAsArray */
165
+ /**
166
+ * @return array{
167
+ * name:string,
168
+ * position:int,
169
+ * type:?string,
170
+ * defaultValue:mixed,
171
+ * isReference:bool,
172
+ * isVariadic:bool,
173
+ * isNullable:bool,
174
+ * isPromoted:bool
175
+ * }
176
+ */
201
177
public function toArray (): array {
202
178
return $ this ->asArray ??= array (
203
- self :: REFERENCE => $ this ->isPassedByReference (),
204
- self :: NULLABLE => $ this ->allowsNull (),
205
- self :: VARIADIC => $ this ->isVariadic (),
206
- self :: PROMOTED => $ this ->isPromoted (),
207
- self :: POSITION => $ this ->getPosition (),
208
- self :: DEFAULT => $ this ->getRawDefaultValue (),
209
- self :: TYPE => $ this ->getRawType (),
210
- self :: NAME => $ this ->getName (),
179
+ Argument::Reference-> value => $ this ->isPassedByReference (),
180
+ Argument::Nullable-> value => $ this ->allowsNull (),
181
+ Argument::Variadic-> value => $ this ->isVariadic (),
182
+ Argument::Promoted-> value => $ this ->isPromoted (),
183
+ Argument::Position-> value => $ this ->getPosition (),
184
+ Argument::Default-> value => $ this ->getRawDefaultValue (),
185
+ Argument::Type-> value => $ this ->getRawType (),
186
+ Argument::Name-> value => $ this ->getName (),
211
187
);
212
188
}
213
189
0 commit comments