respect-validation/library/Rules/KeySet.php

136 lines
2.9 KiB
PHP
Raw Normal View History

2015-07-20 12:16:46 +02:00
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
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.
*
* @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
}
/**
* @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
|| 1 !== count($validatable->getRules())) {
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->reference;
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) {
2015-07-20 12:16:46 +02:00
if (!array_key_exists($keyRule->reference, $input) && $keyRule->mandatory) {
return false;
}
unset($input[$keyRule->reference]);
}
return 0 == count($input);
2015-07-20 12:16:46 +02:00
}
/**
* {@inheritdoc}
*/
public function assert($input): void
2015-07-20 12:16:46 +02:00
{
if (!$this->hasValidStructure($input)) {
throw $this->reportError($input);
}
2015-07-20 12:16:46 +02:00
parent::assert($input);
2015-07-20 12:16:46 +02:00
}
/**
* {@inheritdoc}
*/
public function check($input): void
2015-07-20 12:16:46 +02:00
{
if (!$this->hasValidStructure($input)) {
throw $this->reportError($input);
}
2015-07-20 12:16:46 +02:00
parent::check($input);
2015-07-20 12:16:46 +02:00
}
/**
* {@inheritdoc}
*/
public function validate($input): bool
2015-07-20 12:16:46 +02:00
{
if (!$this->hasValidStructure($input)) {
return false;
}
return parent::validate($input);
}
}