Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions library/NamespacedValidatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Respect\Validation\Transformers\ValidatorSpec;

use function array_merge;
use function Respect\Stringifier\stringify;
use function sprintf;
use function trim;
use function ucfirst;
Expand Down Expand Up @@ -57,6 +58,8 @@ private function createValidatorSpec(ValidatorSpec $validatorSpec): Validator
/** @param array<int, mixed> $arguments */
private function createRule(string $ruleName, array $arguments = []): Validator
{
$reflection = null;

foreach ($this->rulesNamespaces as $namespace) {
try {
/** @var class-string<Validator> $name */
Expand All @@ -72,12 +75,22 @@ private function createRule(string $ruleName, array $arguments = []): Validator
throw new InvalidClassException(sprintf('"%s" must be instantiable', $name));
}

return $reflection->newInstanceArgs($arguments);
break;
} catch (ReflectionException) {
continue;
}
}

throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName));
if (!$reflection) {
throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName));
}

try {
return $reflection->newInstanceArgs($arguments);
} catch (ReflectionException) {
throw new InvalidClassException(
sprintf('"%s" could not be instantiated with arguments %s', $ruleName, stringify($arguments)),
);
}
}
}
26 changes: 26 additions & 0 deletions tests/library/Validators/NoConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Validation\Test\Validators;

use Respect\Validation\Message\Template;
use Respect\Validation\Validators\Core\Simple;

#[Template(
'{{subject}} must be a no-constructor validator',
'{{subject}} must not be a no-constructor validator',
)]
final class NoConstructor extends Simple
{
public function isValid(mixed $input): bool
{
// Accept everything for test purposes
return true;
}
}
13 changes: 13 additions & 0 deletions tests/unit/NamespacedRuleFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public function shouldDefineConstructorArgumentsWhenCreatingRule(): void
self::assertSame($constructorArguments, $validator->validations);
}

#[Test]
public function shouldThrowsAnExceptionOnConstructorReflectionFailure(): void
{
$constructorArguments = ['a', 'b'];

$factory = new NamespacedValidatorFactory(new StubTransformer(), [self::TEST_RULES_NAMESPACE]);

$this->expectException(InvalidClassException::class);
$this->expectExceptionMessage('"noConstructor" could not be instantiated with arguments `["a", "b"]`');

$factory->create('noConstructor', $constructorArguments);
}

#[Test]
public function shouldThrowsAnExceptionWhenRuleIsInvalid(): void
{
Expand Down