Skip to content

Commit e2d4410

Browse files
committed
Updating exception from ValueException to RegexArgumentsFailedException
1 parent 7618a3f commit e2d4410

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/Exceptions/RegexArgumentsFailedException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ public function __construct(string $message)
88
{
99
parent::__construct($message);
1010
}
11+
public static function pattern(string $argument, string $regexp, mixed $argument_given): self
12+
{
13+
return new self(sprintf(
14+
'Field "%s" should be valid with pattern: [%s], "%s" given',
15+
$argument,
16+
$regexp,
17+
(string)$argument_given
18+
));
19+
}
1120
}

src/Request/RequestParamsAbstract.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Microwin7\PHPUtils\Contracts\Enum\EnumInterface;
88
use Microwin7\PHPUtils\Contracts\Enum\EnumRequestInterface;
9+
use Microwin7\PHPUtils\Exceptions\RegexArgumentsFailedException;
910
use Microwin7\PHPUtils\Exceptions\RequiredArgumentMissingException;
1011

1112
#[\AllowDynamicProperties]
@@ -58,7 +59,7 @@ protected function validateVariable(string $key, string $regexp): static
5859
{
5960
null === $this->arguments[$key]
6061
?: filter_var($this->$key, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => $regexp]])
61-
?: throw new \ValueError(sprintf('Field "%s" should be valid with pattern: [%s], "%s" given', $key, $regexp, (string)$this->$key));
62+
?: throw RegexArgumentsFailedException::pattern($key, $regexp, (string)$this->$key);
6263
return $this;
6364
}
6465
/**
@@ -100,7 +101,7 @@ protected function addEnum(string $enumClass, bool $maybeDefault = false): stati
100101
}
101102
}
102103
/**
103-
* @throws \ValueError
104+
* @throws RegexArgumentsFailedException
104105
*/
105106
protected function addVariable(string $key, string $regexp, bool $maybeNull = false): static
106107
{

src/Request/RequiredArguments.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private function execute(): void
106106
try {
107107
$this->setVariable($oneFromAllArgumets);
108108
$count++;
109-
} catch (RequiredArgumentMissingException $ignored) {
109+
} catch (RequiredArgumentMissingException) {
110110
$this->with($oneFromAllArgumets, null);
111111
}
112112
}
@@ -177,12 +177,7 @@ protected function validateVariable(RegexArguments $regexArgument): void
177177
?: (
178178
!is_null($regexArgument->messageCallback)
179179
? throw new RegexArgumentsFailedException($regexArgument->messageCallback)
180-
: throw new \ValueError(sprintf(
181-
'Field "%s" should be valid with pattern: [%s], "%s" given',
182-
$regexArgument->argument,
183-
$regexArgument->regexp,
184-
(string)$this->{$regexArgument->argument}
185-
))
180+
: throw RegexArgumentsFailedException::pattern($regexArgument->argument, $regexArgument->regexp, $this->{$regexArgument->argument})
186181
);
187182
}
188183
/** @param string|int|bool|EnumRequestInterface|EnumInterface|\BackedEnum|non-empty-array<int|string, array<int|string, mixed>|string>|null $value */

0 commit comments

Comments
 (0)