respect-validation/library/Rules/KeySet.php

133 lines
2.9 KiB
PHP
Raw Normal View History

2015-07-20 12:16:46 +02:00
<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
2015-07-20 12:16:46 +02:00
*/
declare(strict_types=1);
2015-07-20 12:16:46 +02:00
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Validatable;
use function array_key_exists;
use function array_map;
use function count;
use function current;
use function is_array;
2015-07-20 12:16:46 +02:00
/**
* Validates a keys in a defined structure.
*
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
2015-07-20 12:16:46 +02:00
*/
final class KeySet extends AbstractWrapper
2015-07-20 12:16:46 +02:00
{
/**
* @var mixed[]
2015-07-20 12:16:46 +02:00
*/
private $keys;
2015-07-20 12:16:46 +02:00
/**
* @var Key[]
*/
private $keyRules;
2015-07-20 12:16:46 +02:00
/**
* Initializes the rule.
*
* phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation
* @param Validatable ...$validatables
2015-07-20 12:16:46 +02:00
*/
public function __construct(Validatable ...$validatables)
2015-07-20 12:16:46 +02:00
{
$this->keyRules = array_map([$this, 'getKeyRule'], $validatables);
$this->keys = array_map([$this, 'getKeyReference'], $this->keyRules);
2015-07-20 12:16:46 +02:00
parent::__construct(new AllOf(...$this->keyRules));
2015-07-20 12:16:46 +02:00
}
/**
* {@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);
}
2015-07-20 12:16:46 +02:00
/**
* @throws ComponentException
2015-07-20 12:16:46 +02:00
*/
private function getKeyRule(Validatable $validatable): Key
2015-07-20 12:16:46 +02:00
{
if ($validatable instanceof Key) {
return $validatable;
}
if (!$validatable instanceof AllOf || count($validatable->getRules()) !== 1) {
throw new ComponentException('KeySet rule accepts only Key rules');
2015-07-20 12:16:46 +02:00
}
return $this->getKeyRule(current($validatable->getRules()));
2015-07-20 12:16:46 +02:00
}
/**
* @return mixed
2015-07-20 12:16:46 +02:00
*/
private function getKeyReference(Key $rule)
2015-07-20 12:16:46 +02:00
{
return $rule->getReference();
2015-07-20 12:16:46 +02:00
}
/**
* @param mixed $input
2015-07-20 12:16:46 +02:00
*/
private function hasValidStructure($input): bool
2015-07-20 12:16:46 +02:00
{
if (!is_array($input)) {
return false;
}
foreach ($this->keyRules as $keyRule) {
if (!array_key_exists($keyRule->getReference(), $input) && $keyRule->isMandatory()) {
2015-07-20 12:16:46 +02:00
return false;
}
unset($input[$keyRule->getReference()]);
2015-07-20 12:16:46 +02:00
}
return count($input) == 0;
2015-07-20 12:16:46 +02:00
}
}