Skip to content

Commit

Permalink
Updating exception from ValueException to RegexArgumentsFailedException
Browse files Browse the repository at this point in the history
  • Loading branch information
microwin7 committed Nov 30, 2024
1 parent 7618a3f commit e2d4410
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/Exceptions/RegexArgumentsFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ public function __construct(string $message)
{
parent::__construct($message);
}
public static function pattern(string $argument, string $regexp, mixed $argument_given): self
{
return new self(sprintf(
'Field "%s" should be valid with pattern: [%s], "%s" given',
$argument,
$regexp,
(string)$argument_given
));
}
}
5 changes: 3 additions & 2 deletions src/Request/RequestParamsAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Microwin7\PHPUtils\Contracts\Enum\EnumInterface;
use Microwin7\PHPUtils\Contracts\Enum\EnumRequestInterface;
use Microwin7\PHPUtils\Exceptions\RegexArgumentsFailedException;
use Microwin7\PHPUtils\Exceptions\RequiredArgumentMissingException;

#[\AllowDynamicProperties]
Expand Down Expand Up @@ -58,7 +59,7 @@ protected function validateVariable(string $key, string $regexp): static
{
null === $this->arguments[$key]
?: filter_var($this->$key, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => $regexp]])
?: throw new \ValueError(sprintf('Field "%s" should be valid with pattern: [%s], "%s" given', $key, $regexp, (string)$this->$key));
?: throw RegexArgumentsFailedException::pattern($key, $regexp, (string)$this->$key);
return $this;
}
/**
Expand Down Expand Up @@ -100,7 +101,7 @@ protected function addEnum(string $enumClass, bool $maybeDefault = false): stati
}
}
/**
* @throws \ValueError
* @throws RegexArgumentsFailedException
*/
protected function addVariable(string $key, string $regexp, bool $maybeNull = false): static
{
Expand Down
9 changes: 2 additions & 7 deletions src/Request/RequiredArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private function execute(): void
try {
$this->setVariable($oneFromAllArgumets);
$count++;
} catch (RequiredArgumentMissingException $ignored) {
} catch (RequiredArgumentMissingException) {
$this->with($oneFromAllArgumets, null);
}
}
Expand Down Expand Up @@ -177,12 +177,7 @@ protected function validateVariable(RegexArguments $regexArgument): void
?: (
!is_null($regexArgument->messageCallback)
? throw new RegexArgumentsFailedException($regexArgument->messageCallback)
: throw new \ValueError(sprintf(
'Field "%s" should be valid with pattern: [%s], "%s" given',
$regexArgument->argument,
$regexArgument->regexp,
(string)$this->{$regexArgument->argument}
))
: throw RegexArgumentsFailedException::pattern($regexArgument->argument, $regexArgument->regexp, $this->{$regexArgument->argument})
);
}
/** @param string|int|bool|EnumRequestInterface|EnumInterface|\BackedEnum|non-empty-array<int|string, array<int|string, mixed>|string>|null $value */
Expand Down

0 comments on commit e2d4410

Please sign in to comment.