mirror of
https://github.com/Respect/Validation.git
synced 2026-03-14 14:25:45 +01:00
- Add PrefixMapGenerator to produce COMPOSABLE/COMPOSABLE_WITH_ARGUMENT constants from #[Mixin] attributes, replacing hand-written arrays - Move Prefix transformer to reference generated PrefixMap constants - Extract NamespaceScanner from MixinGenerator for shared directory scanning - Introduce FluentBuilder subnamespace for builder-chain generators (MixinGenerator, PrefixMapGenerator, MethodBuilder, Mixin attribute) - Add CodeGenerator interface and Config class as shared CodeGen contracts
61 lines
1.7 KiB
PHP
61 lines
1.7 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>
|
|
* SPDX-FileContributor: Jens Segers <segers.jens@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Validators;
|
|
|
|
use Attribute;
|
|
use Respect\Dev\CodeGen\FluentBuilder\Mixin;
|
|
use Respect\Validation\Message\Template;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Validator;
|
|
|
|
use function array_map;
|
|
|
|
#[Mixin(prefix: 'nullOr', exclude: ['all', 'key', 'property', 'not', 'nullOr', 'undefOr'])]
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
|
|
#[Template(
|
|
'or must be null',
|
|
'and must not be null',
|
|
)]
|
|
final readonly class NullOr implements Validator
|
|
{
|
|
public function __construct(
|
|
private Validator $validator,
|
|
) {
|
|
}
|
|
|
|
public function evaluate(mixed $input): Result
|
|
{
|
|
$result = $this->validator->evaluate($input);
|
|
if ($input !== null) {
|
|
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('nullOr'))
|
|
->withAdjacent(Result::of($result->hasPassed, $result->input, $this));
|
|
}
|
|
|
|
return $result->withChildren(...array_map(fn(Result $child) => $this->enrichResult($child), $result->children));
|
|
}
|
|
}
|