mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
The `Id`, `Name`, and `Path` value objects are not only message-related concerns, they're part of the core of the library, hence it makes sense to place them at the root namespace.
43 lines
1 KiB
PHP
43 lines
1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use Attribute;
|
|
use Respect\Validation\Path;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Rule;
|
|
use Respect\Validation\Rules\Core\KeyRelated;
|
|
use Respect\Validation\Rules\Core\Wrapper;
|
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
|
|
final class Key extends Wrapper implements KeyRelated
|
|
{
|
|
public function __construct(
|
|
private readonly int|string $key,
|
|
Rule $rule,
|
|
) {
|
|
parent::__construct($rule);
|
|
}
|
|
|
|
public function getKey(): int|string
|
|
{
|
|
return $this->key;
|
|
}
|
|
|
|
public function evaluate(mixed $input): Result
|
|
{
|
|
$keyExistsResult = (new KeyExists($this->key))->evaluate($input);
|
|
if (!$keyExistsResult->hasPassed) {
|
|
return $keyExistsResult->withNameFrom($this->rule);
|
|
}
|
|
|
|
return $this->rule->evaluate($input[$this->key])->withPath(new Path($this->key));
|
|
}
|
|
}
|