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.
|
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01: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\Exceptions\KeySetException;
|
|
|
|
use Respect\Validation\Validatable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a keys in a defined structure.
|
|
|
|
*
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
|
|
|
class KeySet extends AllOf
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param AllOf $rule
|
|
|
|
*
|
|
|
|
* @return Validatable
|
|
|
|
*/
|
|
|
|
private function filterAllOf(AllOf $rule)
|
|
|
|
{
|
|
|
|
$rules = $rule->getRules();
|
2017-11-19 19:01:31 +01:00
|
|
|
if (1 != count($rules)) {
|
2015-07-20 12:16:46 +02:00
|
|
|
throw new ComponentException('AllOf rule must have only one Key rule');
|
|
|
|
}
|
|
|
|
|
|
|
|
return current($rules);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-10-18 03:44:47 +02:00
|
|
|
public function addRule($rule, $arguments = [])
|
2015-07-20 12:16:46 +02:00
|
|
|
{
|
|
|
|
if ($rule instanceof AllOf) {
|
|
|
|
$rule = $this->filterAllOf($rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$rule instanceof Key) {
|
|
|
|
throw new ComponentException('KeySet rule accepts only Key rules');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->appendRule($rule);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function addRules(array $rules)
|
|
|
|
{
|
|
|
|
foreach ($rules as $rule) {
|
|
|
|
$this->addRule($rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getKeys()
|
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
$keys = [];
|
2015-07-20 12:16:46 +02:00
|
|
|
foreach ($this->getRules() as $keyRule) {
|
|
|
|
$keys[] = $keyRule->reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $input
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function hasValidStructure($input)
|
|
|
|
{
|
2016-04-27 14:04:44 +02:00
|
|
|
if (!is_array($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-20 12:16:46 +02:00
|
|
|
foreach ($this->getRules() as $keyRule) {
|
|
|
|
if (!array_key_exists($keyRule->reference, $input) && $keyRule->mandatory) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($input[$keyRule->reference]);
|
|
|
|
}
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
return 0 == count($input);
|
2015-07-20 12:16:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws KeySetException
|
|
|
|
*/
|
2017-11-19 19:01:31 +01:00
|
|
|
private function checkKeys($input): void
|
2015-07-20 12:16:46 +02:00
|
|
|
{
|
|
|
|
if (!$this->hasValidStructure($input)) {
|
2015-10-18 03:44:47 +02:00
|
|
|
$params = ['keys' => $this->getKeys()];
|
2015-07-20 12:16:46 +02:00
|
|
|
$exception = $this->reportError($input, $params);
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-01-28 17:16:53 +01:00
|
|
|
public function assert($input): void
|
2015-07-20 12:16:46 +02:00
|
|
|
{
|
|
|
|
$this->checkKeys($input);
|
|
|
|
|
2018-01-28 17:16:53 +01:00
|
|
|
parent::assert($input);
|
2015-07-20 12:16:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-01-28 16:14:00 +01:00
|
|
|
public function check($input): void
|
2015-07-20 12:16:46 +02:00
|
|
|
{
|
|
|
|
$this->checkKeys($input);
|
|
|
|
|
2018-01-28 16:14:00 +01:00
|
|
|
parent::check($input);
|
2015-07-20 12:16:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-01-28 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2015-07-20 12:16:46 +02:00
|
|
|
{
|
|
|
|
if (!$this->hasValidStructure($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::validate($input);
|
|
|
|
}
|
|
|
|
}
|