mirror of
https://github.com/Respect/Validation.git
synced 2024-06-10 17:52:18 +02:00
Use ValidationException when no exception is found
Before this change every create rule must have an exception. This commit allows to create rules without specific exceptions, so when the exception of the rule is not found Validation uses ValidationException instead. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
ad61c49eca
commit
04b3c78ba7
|
@ -22,7 +22,6 @@ use function array_map;
|
|||
use function array_merge;
|
||||
use function array_unique;
|
||||
use function class_exists;
|
||||
use function lcfirst;
|
||||
use function Respect\Stringifier\stringify;
|
||||
|
||||
/**
|
||||
|
@ -138,19 +137,18 @@ final class Factory
|
|||
{
|
||||
$reflection = new ReflectionObject($validatable);
|
||||
$ruleName = $reflection->getShortName();
|
||||
$name = $validatable->getName() ?: stringify($input);
|
||||
$params = ['input' => $input] + $extraParams + $this->extractPropertiesValues($validatable, $reflection);
|
||||
foreach ($this->exceptionsNamespaces as $namespace) {
|
||||
$exceptionName = sprintf('%s\\%sException', $namespace, $ruleName);
|
||||
if (!class_exists($exceptionName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $validatable->getName() ?: stringify($input);
|
||||
$params = ['input' => $input] + $extraParams + $this->extractPropertiesValues($validatable, $reflection);
|
||||
|
||||
return $this->createValidationException($exceptionName, $name, $params);
|
||||
}
|
||||
|
||||
throw new ComponentException(sprintf('Cannot find exception for "%s" rule', lcfirst($ruleName)));
|
||||
return $this->createValidationException(ValidationException::class, $name, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -167,7 +165,7 @@ final class Factory
|
|||
private function createReflectionClass(string $name, string $parentName): ReflectionClass
|
||||
{
|
||||
$reflection = new ReflectionClass($name);
|
||||
if (!$reflection->isSubclassOf($parentName)) {
|
||||
if (!$reflection->isSubclassOf($parentName) && $parentName !== $name) {
|
||||
throw new InvalidClassException(sprintf('"%s" must be an instance of "%s"', $name, $parentName));
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ namespace Respect\Validation;
|
|||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
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\Invalid;
|
||||
|
@ -132,14 +133,13 @@ final class FactoryTest extends TestCase
|
|||
/**
|
||||
* @test
|
||||
*/
|
||||
public function shouldThrowAnExceptionWhenExceptionIsNotFound(): void
|
||||
public function shouldCreateValidationExceptionWhenExceptionIsNotFound(): void
|
||||
{
|
||||
$factory = new Factory([], []);
|
||||
$input = 'input';
|
||||
$rule = new Stub();
|
||||
|
||||
$this->expectException(ComponentException::class);
|
||||
$this->expectExceptionMessage('Cannot find exception for "stub" rule');
|
||||
|
||||
$factory->exception(new Stub(), 'foo');
|
||||
self::assertInstanceOf(ValidationException::class, $factory->exception($rule, $input));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue