mirror of
https://github.com/Respect/Validation.git
synced 2026-03-14 22:35: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.
60 lines
1.7 KiB
PHP
60 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: Danilo Benevides <danilobenevides01@gmail.com>
|
|
* SPDX-FileContributor: Graham Campbell <graham@mineuk.com>
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
* SPDX-FileContributor: Marcelo Araujo <msaraujo@php.net>
|
|
* SPDX-FileContributor: Nick Lombard <github@jigsoft.co.za>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Validators;
|
|
|
|
use Attribute;
|
|
use Respect\Dev\CodeGen\Attributes\Mixin;
|
|
use Respect\Validation\Message\Template;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Validator;
|
|
|
|
use function in_array;
|
|
use function is_array;
|
|
use function mb_strpos;
|
|
|
|
#[Mixin(include: ['length', 'max', 'min'])]
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
|
|
#[Template(
|
|
'{{subject}} must be in {{haystack}}',
|
|
'{{subject}} must not be in {{haystack}}',
|
|
)]
|
|
final readonly class In implements Validator
|
|
{
|
|
public function __construct(
|
|
private mixed $haystack,
|
|
) {
|
|
}
|
|
|
|
public function evaluate(mixed $input): Result
|
|
{
|
|
$parameters = ['haystack' => $this->haystack];
|
|
|
|
return Result::of($this->validate($input), $input, $this, $parameters);
|
|
}
|
|
|
|
private function validate(mixed $input): bool
|
|
{
|
|
if (is_array($this->haystack)) {
|
|
return in_array($input, $this->haystack, strict: true);
|
|
}
|
|
|
|
if ($input === null || $input === '') {
|
|
return $input === $this->haystack;
|
|
}
|
|
|
|
return mb_strpos($this->haystack, (string) $input) !== false;
|
|
}
|
|
}
|