Skip to content

Commit c75571b

Browse files
committed
Implementing type conversion based on verified Regex matches. Boolean and Integer
1 parent 0791bdf commit c75571b

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/Request/RequiredArguments.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Microwin7\PHPUtils\Request;
44

5+
use Microwin7\PHPUtils\Rules\Regex;
56
use Microwin7\PHPUtils\Attributes\AsArguments;
67
use function Microwin7\PHPUtils\implodeRecursive;
78
use Microwin7\PHPUtils\Attributes\RegexArguments;
@@ -13,7 +14,7 @@
1314

1415
class 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;

src/Rules/Regex.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Regex
1111
public const string ID_REGXP = '/^[a-zA-Z0-9\-\_\:\.]+$/';
1212
public const string DISPLAYNAME_REGXP = '/^[a-zA-Zа-яА-ЯЁё0-9\-\_\ \(\)\[\]\.\,\"\«\»\/]+$/';
1313
public const string NUMERIC_REGXP = '/^[0-9]+$/';
14+
public const string BOOLEAN_REGXP = '/^(0|1|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee])$/';
1415
public const string CATEGORY_REGXP = '/^[A-Z0-9\_]+$/';
1516
public const string ITEM_LINK_REGXP = '/^[a-zA-Z0-9\_\-\.]+$/';
1617
public const string SERVER_REGXP = '/^[a-zA-Z\_]+$/';

0 commit comments

Comments
 (0)