Auto-resolve exception namespaces

After the refactoring on the Factory class [1], to throw exceptions of a
specific rule, it is necessary to add the exception namespace of that
rule. That change makes sense when someone wants to create rules from
the Validator class, but when using rules as classes, it's not as handy.

This commit will auto-resolve exception based on the rule namespace,
just as it used to be.

[1]: 1f217dda66

Co-authored-by: Casey McLaughlin <caseyamcl@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2020-10-04 01:07:05 +02:00
parent ed304f3406
commit 8b2819e9f8
No known key found for this signature in database
GPG key ID: 221E9281655813A6
3 changed files with 59 additions and 1 deletions

View file

@ -23,8 +23,10 @@ use Respect\Validation\Message\Formatter;
use Respect\Validation\Message\ParameterStringifier;
use Respect\Validation\Message\Stringifier\KeepOriginalStringName;
use function array_merge;
use function lcfirst;
use function sprintf;
use function str_replace;
use function trim;
use function ucfirst;
@ -157,7 +159,8 @@ final class Factory
if ($validatable->getName() !== null) {
$id = $params['name'] = $validatable->getName();
}
foreach ($this->exceptionsNamespaces as $namespace) {
$exceptionNamespace = str_replace('\\Rules', '\\Exceptions', $reflection->getNamespaceName());
foreach (array_merge([$exceptionNamespace], $this->exceptionsNamespaces) as $namespace) {
try {
/** @var class-string<ValidationException> $exceptionName */
$exceptionName = $namespace . '\\' . $ruleName . 'Exception';

View file

@ -0,0 +1,32 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Test\Rules;
use Respect\Validation\Rules\AbstractRule;
/**
* Example of a custom rule that does not have an exception.
*
* @author Casey McLaughlin <caseyamcl@gmail.com>
*/
final class CustomRule extends AbstractRule
{
/**
* {@inheritDoc}
*/
public function validate($input): bool
{
return false;
}
}

View file

@ -18,6 +18,7 @@ use Respect\Validation\Exceptions\InvalidClassException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Test\Exceptions\StubException;
use Respect\Validation\Test\Rules\AbstractClass;
use Respect\Validation\Test\Rules\CustomRule;
use Respect\Validation\Test\Rules\Invalid;
use Respect\Validation\Test\Rules\Stub;
use Respect\Validation\Test\Rules\Valid;
@ -227,4 +228,26 @@ final class FactoryTest extends TestCase
Factory::setDefaultInstance($defaultInstance);
}
/**
* @test
*/
public function shouldAutoResolveExceptionIfNamespacePatternMatchesAndExceptionClassFound(): void
{
$this->expectException(StubException::class);
$rule = new Stub();
$rule->assert('test');
}
/**
* @test
*/
public function shouldUseDefaultExceptionIfCustomExceptionNotFound(): void
{
$this->expectException(ValidationException::class);
$rule = new CustomRule();
$rule->assert('test');
}
}