2022-12-24 22:10:40 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2024-01-31 22:15:08 +08:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2022-12-24 22:10:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
2024-01-31 22:15:08 +08:00
|
|
|
use Respect\Validation\NonNegatable;
|
2022-12-24 22:10:40 +08:00
|
|
|
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 22:15:08 +08:00
|
|
|
final class KeySet extends AbstractWrapper implements NonNegatable
|
2022-12-24 22:10:40 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var mixed[]
|
|
|
|
*/
|
|
|
|
private $keys;
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
/**
|
|
|
|
* @var mixed[]
|
|
|
|
*/
|
|
|
|
private $extraKeys = [];
|
|
|
|
|
2022-12-24 22:10:40 +08:00
|
|
|
/**
|
|
|
|
* @var Key[]
|
|
|
|
*/
|
|
|
|
private $keyRules;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the rule.
|
|
|
|
*
|
2024-01-31 22:15:08 +08:00
|
|
|
* phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation
|
|
|
|
* @param Validatable ...$validatables
|
2022-12-24 22:10:40 +08:00
|
|
|
*/
|
|
|
|
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 22:15:08 +08:00
|
|
|
foreach ($input as $extraKey => &$ignoreValue) {
|
|
|
|
$this->extraKeys[] = $extraKey;
|
|
|
|
}
|
|
|
|
|
2022-12-24 22:10:40 +08:00
|
|
|
return count($input) == 0;
|
|
|
|
}
|
|
|
|
}
|