respect-validation/library/Rules/KeyExists.php
Henrique Moody fe68eab37d
Allow to validate ArrayObject in key-related values
Because of that, I also updated some data providers to distinguish
between "values" and "types", similar to some of the rules we already
have.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-08 21:36:49 +01:00

48 lines
1,023 B
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use ArrayAccess;
use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
use function array_key_exists;
use function is_array;
#[Template(
'{{name}} must be present',
'{{name}} must not be present',
)]
final class KeyExists extends Standard
{
public function __construct(
private readonly int|string $key
) {
}
public function evaluate(mixed $input): Result
{
return new Result($this->hasKey($input), $input, $this, name: (string) $this->key);
}
private function hasKey(mixed $input): bool
{
if (is_array($input)) {
return array_key_exists($this->key, $input);
}
if ($input instanceof ArrayAccess) {
return $input->offsetExists($this->key);
}
return false;
}
}