Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit ff26b46

Browse files
committed
chore: improved rule not found handling
1 parent 39cab83 commit ff26b46

4 files changed

Lines changed: 19 additions & 19 deletions

File tree

src/Exception/InvalidRuleException.php

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
4+
5+
class RuleNotFoundException extends \Exception
6+
{
7+
protected $message = 'Rule does not exist.';
8+
}

src/Factory.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator;
44

5-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\InvalidRuleException;
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\RuleNotFoundException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
77

88
class Factory
99
{
1010
private string $namespace = 'ProgrammatorDev\\YetAnotherPhpValidator\\Rule';
1111

1212
/**
13-
* @throws InvalidRuleException
13+
* @throws RuleNotFoundException
1414
*/
1515
public function createRule(string $ruleName, array $arguments = []): RuleInterface
1616
{
17-
try {
18-
$className = \sprintf('%s\\%s', $this->namespace, \ucfirst($ruleName));
19-
return new $className(...$arguments);
20-
}
21-
catch (\Error) {
22-
throw new InvalidRuleException(
17+
$className = \sprintf('%s\\%s', $this->namespace, \ucfirst($ruleName));
18+
19+
if (!class_exists($className)) {
20+
throw new RuleNotFoundException(
2321
\sprintf('"%s" rule does not exist.', $ruleName)
2422
);
2523
}
24+
25+
return new $className(...$arguments);
2626
}
2727
}

src/Validator.php

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

33
namespace ProgrammatorDev\YetAnotherPhpValidator;
44

5-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\InvalidRuleException;
5+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\RuleNotFoundException;
66
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
77
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
88

@@ -24,15 +24,15 @@ private static function create(): self
2424
}
2525

2626
/**
27-
* @throws InvalidRuleException
27+
* @throws RuleNotFoundException
2828
*/
2929
public static function __callStatic(string $ruleName, array $arguments = []): self
3030
{
3131
return self::create()->__call($ruleName, $arguments);
3232
}
3333

3434
/**
35-
* @throws InvalidRuleException
35+
* @throws RuleNotFoundException
3636
*/
3737
public function __call(string $ruleName, array $arguments = []): self
3838
{

0 commit comments

Comments
 (0)