respect-validation/library/Rules/Core/Reducer.php
2025-12-18 19:03:39 +01:00

50 lines
1.1 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules\Core;
use Respect\Validation\Rule;
use Respect\Validation\Rules\AllOf;
use Respect\Validation\Rules\Named;
use Respect\Validation\Rules\Templated;
final class Reducer extends Wrapper
{
public function __construct(Rule $rule1, Rule ...$rules)
{
parent::__construct($rules === [] ? $rule1 : new AllOf($rule1, ...$rules));
}
public function withTemplate(string|null $template): self
{
if ($template === null) {
return $this;
}
return new self(new Templated($this->rule, $template));
}
public function withName(string|null $name): self
{
if ($name === null) {
return $this;
}
return new self(new Named($this->rule, $name));
}
public function getName(): string|null
{
if ($this->rule instanceof Nameable) {
return $this->rule->getName();
}
return null;
}
}