mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 07:45:45 +01:00
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>
48 lines
1,023 B
PHP
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;
|
|
}
|
|
}
|