22
33namespace Microwin7 \PHPUtils \Request ;
44
5+ use Microwin7 \PHPUtils \Rules \Regex ;
56use Microwin7 \PHPUtils \Attributes \AsArguments ;
67use function Microwin7 \PHPUtils \implodeRecursive ;
78use Microwin7 \PHPUtils \Attributes \RegexArguments ;
1314
1415class RequiredArguments
1516{
16- /** @var array<string, string|EnumRequestInterface|EnumInterface|\BackedEnum|non-empty-array<int|string, array<int|string, mixed>|string>|null> $arguments */
17+ /** @var array<string, string|int|bool| EnumRequestInterface|EnumInterface|\BackedEnum|non-empty-array<int|string, array<int|string, mixed>|string>|null> $arguments */
1718 private array $ arguments = [];
1819 /** @var array<string|string[]> $requiredArguments */
1920 private array $ requiredArguments ;
@@ -36,9 +37,9 @@ public function __construct(private \ReflectionFunctionAbstract|null $reflection
3637 $ this ->execute ();
3738 }
3839 /**
39- * @return string|EnumRequestInterface|\BackedEnum|EnumInterface|non-empty-array<int|string, array<int|string, mixed>|string>|null
40+ * @return string|int|bool| EnumRequestInterface|\BackedEnum|EnumInterface|non-empty-array<int|string, array<int|string, mixed>|string>|null
4041 */
41- public function __get (string $ name ): string |array |null |object
42+ public function __get (string $ name ): string |int | bool | array |null |object
4243 {
4344 return $ this ->arguments [$ name ];
4445 }
@@ -136,9 +137,17 @@ private function execute(): void
136137 private function setVariable (string $ argument , bool $ optional = false ): void
137138 {
138139 if (strrpos ($ argument , '\\' ) === false ) {
139- $ whereValue = $ this ->where [$ argument ] ?? ($ optional ? null : throw new RequiredArgumentMissingException ($ argument ));
140- $ this ->with ($ argument , $ whereValue );
141- if (isset ($ this ->regexArguments [$ argument ])) $ this ->validateVariable ($ this ->regexArguments [$ argument ]);
140+ $ VALUE = $ this ->where [$ argument ] ?? ($ optional ? null : throw new RequiredArgumentMissingException ($ argument ));
141+ if (isset ($ this ->regexArguments [$ argument ])) {
142+ $ this ->validateVariable ($ this ->regexArguments [$ argument ]);
143+ /** @psalm-suppress RiskyCast */
144+ $ VALUE = match ($ this ->regexArguments [$ argument ]->regexp ) {
145+ Regex::BOOLEAN_REGXP => (bool ) $ VALUE ,
146+ Regex::NUMERIC_REGXP => (int ) $ VALUE ,
147+ default => $ VALUE
148+ };
149+ }
150+ $ this ->with ($ argument , $ VALUE );
142151 } else if (enum_exists ($ argument )) {
143152 $ argumentClazz = new \ReflectionClass ($ argument );
144153 if (
@@ -149,11 +158,11 @@ private function setVariable(string $argument, bool $optional = false): void
149158 /** @var interface-string<\BackedEnum & EnumInterface & EnumRequestInterface> $enumClass */
150159 $ enumClass = $ argument ;
151160 try {
152- $ whereValue = $ this ->where [$ enumClass ::getNameRequestVariable ()] ?? throw new RequiredArgumentMissingException ('Missing Request variable: ' . $ enumClass ::getNameRequestVariable ());
153- if (is_numeric ($ whereValue ))
154- $ this ->with ($ enumClass ::getNameVariable (), $ enumClass ::from ((int )$ whereValue ));
155- elseif (is_string ($ whereValue ))
156- $ this ->with ($ enumClass ::getNameVariable (), $ enumClass ::fromString ($ whereValue ));
161+ $ VALUE = $ this ->where [$ enumClass ::getNameRequestVariable ()] ?? throw new RequiredArgumentMissingException ('Missing Request variable: ' . $ enumClass ::getNameRequestVariable ());
162+ if (is_numeric ($ VALUE ))
163+ $ this ->with ($ enumClass ::getNameVariable (), $ enumClass ::from ((int )$ VALUE ));
164+ elseif (is_string ($ VALUE ))
165+ $ this ->with ($ enumClass ::getNameVariable (), $ enumClass ::fromString ($ VALUE ));
157166 } catch (\InvalidArgumentException $ exception ) {
158167 if (!$ optional ) throw new \InvalidArgumentException ((string )$ exception );
159168 $ this ->with ($ enumClass ::getNameVariable (), $ enumClass ::getDefault ());
@@ -164,7 +173,7 @@ private function setVariable(string $argument, bool $optional = false): void
164173 protected function validateVariable (RegexArguments $ regexArgument ): void
165174 {
166175 null === $ this ->{$ regexArgument ->argument }
167- ?: filter_var ($ this ->{$ regexArgument ->argument }, FILTER_VALIDATE_REGEXP , ['options ' => ['regexp ' => $ regexArgument ->regexp ]])
176+ ?: filter_var ($ this ->{$ regexArgument ->argument }, FILTER_VALIDATE_REGEXP , ['options ' => ['regexp ' => $ regexArgument ->regexp ]]) !== false
168177 ?: (
169178 !is_null ($ regexArgument ->messageCallback )
170179 ? throw new RegexArgumentsFailedException ($ regexArgument ->messageCallback )
@@ -176,8 +185,8 @@ protected function validateVariable(RegexArguments $regexArgument): void
176185 ))
177186 );
178187 }
179- /** @param string|EnumRequestInterface|EnumInterface|\BackedEnum|non-empty-array<int|string, array<int|string, mixed>|string>|null $value */
180- private function with (string $ property , string |object |array |null $ value ): void
188+ /** @param string|int|bool| EnumRequestInterface|EnumInterface|\BackedEnum|non-empty-array<int|string, array<int|string, mixed>|string>|null $value */
189+ private function with (string $ property , string |int | bool | object |array |null $ value ): void
181190 {
182191 if (strrpos ($ property , '\\' ) === false ) {
183192 $ this ->arguments [$ property ] = $ value ;
0 commit comments