mirror of
https://github.com/Respect/Validation.git
synced 2026-03-14 14:25:45 +01:00
Replace hardcoded validator class lists with a declarative #[Mixin] attribute and extract the mixin generation logic into a reusable CodeGen namespace under src-dev/CodeGen/. The new MixinGenerator discovers prefix definitions and filtering rules by scanning #[Mixin] attributes on the target namespace's classes, removing the need for hardcoded configuration. It supports configurable interface types (Builder for __callStatic, Chain for __call) with custom suffixes, return types, and root extends. This is the first step toward extracting the code generation into a standalone package that can map __call/__callStatic to any namespace, possibly for Respect/StringFormatter and any kind of project in the future.
63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-FileContributor: Fabio Ribeiro <faabiosr@gmail.com>
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Validators;
|
|
|
|
use Attribute;
|
|
use Respect\Dev\CodeGen\Attributes\Mixin;
|
|
use Respect\Validation\Helpers\CanValidateUndefined;
|
|
use Respect\Validation\Message\Template;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Validator;
|
|
|
|
use function array_map;
|
|
|
|
#[Mixin(prefix: 'undefOr', exclude: ['all', 'key', 'property', 'not', 'nullOr', 'undefOr'])]
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
|
|
#[Template(
|
|
'or must be undefined',
|
|
'and must not be undefined',
|
|
)]
|
|
final readonly class UndefOr implements Validator
|
|
{
|
|
use CanValidateUndefined;
|
|
|
|
public function __construct(
|
|
private Validator $validator,
|
|
) {
|
|
}
|
|
|
|
public function evaluate(mixed $input): Result
|
|
{
|
|
$result = $this->validator->evaluate($input);
|
|
if (!$this->isUndefined($input)) {
|
|
return $this->enrichResult($result);
|
|
}
|
|
|
|
if (!$result->hasPassed) {
|
|
return $this->enrichResult($result->withToggledValidation());
|
|
}
|
|
|
|
return $this->enrichResult($result);
|
|
}
|
|
|
|
private function enrichResult(Result $result): Result
|
|
{
|
|
if ($result->allowsAdjacent()) {
|
|
return $result
|
|
->withId($result->id->withPrefix('undefOr'))
|
|
->withAdjacent(Result::of($result->hasPassed, $result->input, $this));
|
|
}
|
|
|
|
return $result->withChildren(...array_map(fn(Result $child) => $this->enrichResult($child), $result->children));
|
|
}
|
|
}
|