mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 00:32:16 +02:00
8573bc5d45
When you have a chain of rules in the Validation and overwrite the name with "setName()," it's impossible to get the messages from all rules in the chain as an array because they all have the same name. These changes will change that behavior by creating a more explicit distinction between "IDs" and "names." The "IDs" will remain unchangeable, while we can always overwrite the names. That means that the array messages will look more similar to the chain, and it will be possible to overwrite the messages from multiple rules in the same chain. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use Respect\Validation\Helpers\CanBindEvaluateRule;
|
|
use Respect\Validation\Helpers\CanExtractPropertyValue;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Rules\Core\Wrapper;
|
|
use Respect\Validation\Validatable;
|
|
|
|
final class Property extends Wrapper
|
|
{
|
|
use CanBindEvaluateRule;
|
|
use CanExtractPropertyValue;
|
|
|
|
public function __construct(
|
|
private readonly string $propertyName,
|
|
Validatable $rule,
|
|
) {
|
|
$rule->setName($rule->getName() ?? $propertyName);
|
|
parent::__construct($rule);
|
|
}
|
|
|
|
public function evaluate(mixed $input): Result
|
|
{
|
|
$propertyExistsResult = $this->bindEvaluate(new PropertyExists($this->propertyName), $this, $input);
|
|
if (!$propertyExistsResult->isValid) {
|
|
return $propertyExistsResult;
|
|
}
|
|
|
|
$childResult = $this->rule
|
|
->evaluate($this->extractPropertyValue($input, $this->propertyName))
|
|
->withId($this->propertyName);
|
|
|
|
return (new Result($childResult->isValid, $input, $this))
|
|
->withChildren($childResult)
|
|
->withId($this->propertyName)
|
|
->withNameIfMissing($this->rule->getName() ?? $this->propertyName);
|
|
}
|
|
}
|