mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 07:45:45 +01:00
Currently, defining translations is quite cumbersome, and the translator callback is passed to the constructor of multiple classes, which makes it quite ugly and could make translations inconsistent. This commit completely changes how translations are done in Validation. Instead of using a callback, it uses a specific class, and `Validator` will pass that object through the objects that render the messages.
110 lines
3.5 KiB
PHP
110 lines
3.5 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation;
|
|
|
|
use ReflectionClass;
|
|
use ReflectionException;
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
use Respect\Validation\Exceptions\InvalidClassException;
|
|
use Respect\Validation\Transformers\Aliases;
|
|
use Respect\Validation\Transformers\DeprecatedAge;
|
|
use Respect\Validation\Transformers\DeprecatedAttribute;
|
|
use Respect\Validation\Transformers\DeprecatedKey;
|
|
use Respect\Validation\Transformers\DeprecatedKeyNested;
|
|
use Respect\Validation\Transformers\DeprecatedKeyValue;
|
|
use Respect\Validation\Transformers\DeprecatedLength;
|
|
use Respect\Validation\Transformers\DeprecatedMinAndMax;
|
|
use Respect\Validation\Transformers\DeprecatedType;
|
|
use Respect\Validation\Transformers\Prefix;
|
|
use Respect\Validation\Transformers\RuleSpec;
|
|
use Respect\Validation\Transformers\Transformer;
|
|
|
|
use function array_merge;
|
|
use function sprintf;
|
|
use function trim;
|
|
use function ucfirst;
|
|
|
|
final class Factory
|
|
{
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
private array $rulesNamespaces = ['Respect\\Validation\\Rules'];
|
|
|
|
public function __construct(
|
|
private readonly Transformer $transformer = new DeprecatedAttribute(
|
|
new DeprecatedKey(
|
|
new DeprecatedKeyValue(
|
|
new DeprecatedMinAndMax(
|
|
new DeprecatedAge(
|
|
new DeprecatedKeyNested(new DeprecatedLength(new DeprecatedType(new Aliases(new Prefix()))))
|
|
)
|
|
)
|
|
)
|
|
)
|
|
)
|
|
) {
|
|
}
|
|
|
|
public function withRuleNamespace(string $rulesNamespace): self
|
|
{
|
|
$clone = clone $this;
|
|
$clone->rulesNamespaces[] = trim($rulesNamespace, '\\');
|
|
|
|
return $clone;
|
|
}
|
|
|
|
/**
|
|
* @param mixed[] $arguments
|
|
*/
|
|
public function rule(string $ruleName, array $arguments = []): Validatable
|
|
{
|
|
return $this->createRuleSpec($this->transformer->transform(new RuleSpec($ruleName, $arguments)));
|
|
}
|
|
|
|
private function createRuleSpec(RuleSpec $ruleSpec): Validatable
|
|
{
|
|
$rule = $this->createRule($ruleSpec->name, $ruleSpec->arguments);
|
|
if ($ruleSpec->wrapper !== null) {
|
|
return $this->createRule($ruleSpec->wrapper->name, array_merge($ruleSpec->wrapper->arguments, [$rule]));
|
|
}
|
|
|
|
return $rule;
|
|
}
|
|
|
|
/**
|
|
* @param mixed[] $arguments
|
|
*/
|
|
private function createRule(string $ruleName, array $arguments = []): Validatable
|
|
{
|
|
foreach ($this->rulesNamespaces as $namespace) {
|
|
try {
|
|
/** @var class-string<Validatable> $name */
|
|
$name = $namespace . '\\' . ucfirst($ruleName);
|
|
$reflection = new ReflectionClass($name);
|
|
if (!$reflection->isSubclassOf(Validatable::class)) {
|
|
throw new InvalidClassException(
|
|
sprintf('"%s" must be an instance of "%s"', $name, Validatable::class)
|
|
);
|
|
}
|
|
|
|
if (!$reflection->isInstantiable()) {
|
|
throw new InvalidClassException(sprintf('"%s" must be instantiable', $name));
|
|
}
|
|
|
|
return $reflection->newInstanceArgs($arguments);
|
|
} catch (ReflectionException) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName));
|
|
}
|
|
}
|