HighSpeaker/vendor/workerman/validation/library/Rules/KeySet.php

143 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2022-12-24 19:40:40 +05:30
<?php
/*
2024-01-31 19:45:08 +05:30
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
2022-12-24 19:40:40 +05:30
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
2024-01-31 19:45:08 +05:30
use Respect\Validation\NonNegatable;
2022-12-24 19:40:40 +05:30
use Respect\Validation\Validatable;
use function array_key_exists;
use function array_map;
use function count;
use function current;
use function is_array;
/**
* Validates a keys in a defined structure.
*
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
2024-01-31 19:45:08 +05:30
final class KeySet extends AbstractWrapper implements NonNegatable
2022-12-24 19:40:40 +05:30
{
/**
* @var mixed[]
*/
private $keys;
2024-01-31 19:45:08 +05:30
/**
* @var mixed[]
*/
private $extraKeys = [];
2022-12-24 19:40:40 +05:30
/**
* @var Key[]
*/
private $keyRules;
/**
* Initializes the rule.
*
2024-01-31 19:45:08 +05:30
* phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation
* @param Validatable ...$validatables
2022-12-24 19:40:40 +05:30
*/
public function __construct(Validatable ...$validatables)
{
$this->keyRules = array_map([$this, 'getKeyRule'], $validatables);
$this->keys = array_map([$this, 'getKeyReference'], $this->keyRules);
parent::__construct(new AllOf(...$this->keyRules));
}
/**
* {@inheritDoc}
*/
public function assert($input): void
{
if (!$this->hasValidStructure($input)) {
throw $this->reportError($input);
}
parent::assert($input);
}
/**
* {@inheritDoc}
*/
public function check($input): void
{
if (!$this->hasValidStructure($input)) {
throw $this->reportError($input);
}
parent::check($input);
}
/**
* {@inheritDoc}
*/
public function validate($input): bool
{
if (!$this->hasValidStructure($input)) {
return false;
}
return parent::validate($input);
}
/**
* @throws ComponentException
*/
private function getKeyRule(Validatable $validatable): Key
{
if ($validatable instanceof Key) {
return $validatable;
}
if (!$validatable instanceof AllOf || count($validatable->getRules()) !== 1) {
throw new ComponentException('KeySet rule accepts only Key rules');
}
return $this->getKeyRule(current($validatable->getRules()));
}
/**
* @return mixed
*/
private function getKeyReference(Key $rule)
{
return $rule->getReference();
}
/**
* @param mixed $input
*/
private function hasValidStructure($input): bool
{
if (!is_array($input)) {
return false;
}
foreach ($this->keyRules as $keyRule) {
if (!array_key_exists($keyRule->getReference(), $input) && $keyRule->isMandatory()) {
return false;
}
unset($input[$keyRule->getReference()]);
}
2024-01-31 19:45:08 +05:30
foreach ($input as $extraKey => &$ignoreValue) {
$this->extraKeys[] = $extraKey;
}
2022-12-24 19:40:40 +05:30
return count($input) == 0;
}
}