Skip to content

Commit

Permalink
In RuleInterface replace getHandlerClassName(): string to `getHan…
Browse files Browse the repository at this point in the history
…dler(): string|RuleHandlerInterface` (#468)
  • Loading branch information
vjik committed Dec 17, 2022
1 parent 9562b0c commit 4734f7a
Show file tree
Hide file tree
Showing 30 changed files with 84 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Rule/AtLeast.php
Expand Up @@ -98,7 +98,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return AtLeastHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Boolean.php
Expand Up @@ -110,7 +110,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return BooleanHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Callback.php
Expand Up @@ -116,7 +116,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return CallbackHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Compare.php
Expand Up @@ -179,7 +179,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return CompareHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Composite.php
Expand Up @@ -99,7 +99,7 @@ public function getRules(): iterable
return $this->rules;
}

final public function getHandlerClassName(): string
final public function getHandler(): string
{
return CompositeHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Count.php
Expand Up @@ -130,7 +130,7 @@ public function getOptions(): array
]);
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return CountHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Each.php
Expand Up @@ -137,7 +137,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return EachHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Email.php
Expand Up @@ -159,7 +159,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return EmailHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/HasLength.php
Expand Up @@ -126,7 +126,7 @@ public function getOptions(): array
]);
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return HasLengthHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/In.php
Expand Up @@ -96,7 +96,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return InHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Ip.php
Expand Up @@ -414,7 +414,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return IpHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/IsTrue.php
Expand Up @@ -88,7 +88,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return IsTrueHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Json.php
Expand Up @@ -73,7 +73,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return JsonHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Nested.php
Expand Up @@ -360,7 +360,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return NestedHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Number.php
Expand Up @@ -157,7 +157,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return NumberHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Regex.php
Expand Up @@ -98,7 +98,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return RegexHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Required.php
Expand Up @@ -85,7 +85,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return RequiredHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/StopOnError.php
Expand Up @@ -81,7 +81,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return StopOnErrorHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Subset.php
Expand Up @@ -94,7 +94,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return SubsetHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/Url.php
Expand Up @@ -127,7 +127,7 @@ public function getOptions(): array
];
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return UrlHandler::class;
}
Expand Down
6 changes: 3 additions & 3 deletions src/RuleInterface.php
Expand Up @@ -28,14 +28,14 @@ interface RuleInterface
public function getName(): string;

/**
* A matching handler class name used for processing this rule.
* A matching handler class name or instance used for processing this rule.
*
* While not required, it's recommended to use rule class name with "Handler" suffix, so for `AtLeast` rule class
* name the handler class name will be `AtLeastHandler` and so on.
*
* All packages handlers are stored within the same namespace as rules, but this is not a strict requirement.
*
* @return string A rule handler class name.
* @return RuleHandlerInterface|string A rule handler class name or instance.
*/
public function getHandlerClassName(): string;
public function getHandler(): string|RuleHandlerInterface;
}
7 changes: 6 additions & 1 deletion src/Validator.php
Expand Up @@ -20,6 +20,7 @@

use function extension_loaded;
use function is_int;
use function is_string;

/**
* Validator validates {@link DataSetInterface} against rules set for data set attributes.
Expand Down Expand Up @@ -140,7 +141,11 @@ private function validateInternal(mixed $value, iterable $rules, ValidationConte
continue;
}

$ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName());
$ruleHandler = $rule->getHandler();
if (is_string($ruleHandler)) {
$ruleHandler = $this->ruleHandlerResolver->resolve($ruleHandler);
}

$ruleResult = $ruleHandler->validate($value, $rule, $context);
if ($ruleResult->isValid()) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion tests/Rule/NestedTest.php
Expand Up @@ -83,7 +83,7 @@ public function testHandlerClassName(): void
{
$rule = new Nested();

$this->assertSame(NestedHandler::class, $rule->getHandlerClassName());
$this->assertSame(NestedHandler::class, $rule->getHandler());
}

public function dataOptions(): array
Expand Down
2 changes: 1 addition & 1 deletion tests/Rule/RequiredTest.php
Expand Up @@ -27,7 +27,7 @@ public function testDefaultValues(): void
$rule = new Required();

$this->assertInstanceOf(WhenEmpty::class, $rule->getEmptyCriteria());
$this->assertSame(RequiredHandler::class, $rule->getHandlerClassName());
$this->assertSame(RequiredHandler::class, $rule->getHandler());
$this->assertSame('Value cannot be blank.', $rule->getMessage());
$this->assertSame('required', $rule->getName());
$this->assertSame('Value not passed.', $rule->getNotPassedMessage());
Expand Down
34 changes: 34 additions & 0 deletions tests/Rule/RuleWithBuiltInHandler.php
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Validator\Tests\Rule;

use Yiisoft\Validator\Result;
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;

final class RuleWithBuiltInHandler implements RuleInterface, RuleHandlerInterface
{
public function validate(mixed $value, object $rule, ValidationContext $context): Result
{
$result = new Result();

if ($value !== 42) {
$result->addError('Value must be 42.');
}

return $result;
}

public function getName(): string
{
return 'is42';
}

public function getHandler(): string|RuleHandlerInterface
{
return $this;
}
}
2 changes: 1 addition & 1 deletion tests/Support/Rule/NotNullRule/NotNull.php
Expand Up @@ -15,7 +15,7 @@ public function getName(): string
return 'notNull';
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return NotNullHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Rule/RuleWithCustomHandler.php
Expand Up @@ -18,7 +18,7 @@ public function getName(): string
return 'rule-with-custom-handler';
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return $this->handlerClassName;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Rule/RuleWithoutOptions.php
Expand Up @@ -14,7 +14,7 @@ public function getName(): string
return 'test';
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return StubRuleHandler::class;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Rule/StubRule/StubRuleWithOptions.php
Expand Up @@ -22,7 +22,7 @@ public function getOptions(): array
return $this->options;
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return StubRuleHandler::class;
}
Expand Down
17 changes: 15 additions & 2 deletions tests/ValidatorTest.php
Expand Up @@ -26,6 +26,7 @@
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\RulesProviderInterface;
use Yiisoft\Validator\Tests\Rule\RuleWithBuiltInHandler;
use Yiisoft\Validator\Tests\Support\Data\EachNestedObjects\Foo;
use Yiisoft\Validator\Tests\Support\Data\IteratorWithBooleanKey;
use Yiisoft\Validator\Tests\Support\Data\ObjectWithAttributesOnly;
Expand Down Expand Up @@ -314,7 +315,7 @@ public function getName(): string
return 'test';
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return $this->ruleHandler::class;
}
Expand All @@ -336,7 +337,7 @@ public function getName(): string
return 'test';
}

public function getHandlerClassName(): string
public function getHandler(): string
{
return 'NonExistClass';
}
Expand Down Expand Up @@ -1335,4 +1336,16 @@ public function testSimpleForm(array $expectedMessages, ?ValidationContext $vali
$result->getErrorMessagesIndexedByPath()
);
}

public function testRuleWithBuiltInHandler(): void
{
$rule = new RuleWithBuiltInHandler();

$result = (new Validator())->validate(19, $rule);

$this->assertSame(
['' => ['Value must be 42.']],
$result->getErrorMessagesIndexedByPath()
);
}
}

0 comments on commit 4734f7a

Please sign in to comment.