mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
Currently, we convert the properties of a rule into parameters and pass them to the exceptions. That complicates things for a few reasons: 1. The exception knows too much: there's a lot of information in an object, and the exception would only need a few parameters to work correctly. 2. Any variable change becomes a backward compatibility break: if we change the name of the variable type in a rule, even if it's a private one, we may need to change the template, which is a backward compatibility break. 3. The factory is bloated because of introspection tricks: it reads the properties from the class, even from the parent, and then passes it to the exception. Of course, that means we introduce another method to `Validatable`, but in most cases, extending `AbstractRule` is enough to create a new rule. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
144 lines
3.3 KiB
PHP
144 lines
3.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
use Respect\Validation\NonNegatable;
|
|
use Respect\Validation\Validatable;
|
|
|
|
use function array_key_exists;
|
|
use function array_map;
|
|
use function count;
|
|
use function current;
|
|
use function is_array;
|
|
|
|
final class KeySet extends AbstractWrapper implements NonNegatable
|
|
{
|
|
public const TEMPLATE_NONE = 'none';
|
|
public const TEMPLATE_SOME = 'some';
|
|
public const TEMPLATE_STRUCTURE = 'structure';
|
|
public const TEMPLATE_STRUCTURE_EXTRA = 'structure_extra';
|
|
|
|
/**
|
|
* @var mixed[]
|
|
*/
|
|
private readonly array $keys;
|
|
|
|
/**
|
|
* @var mixed[]
|
|
*/
|
|
private array $extraKeys = [];
|
|
|
|
/**
|
|
* @var Key[]
|
|
*/
|
|
private readonly array $keyRules;
|
|
|
|
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));
|
|
}
|
|
|
|
public function assert(mixed $input): void
|
|
{
|
|
if (!$this->hasValidStructure($input)) {
|
|
throw $this->reportError($input);
|
|
}
|
|
|
|
parent::assert($input);
|
|
}
|
|
|
|
public function check(mixed $input): void
|
|
{
|
|
if (!$this->hasValidStructure($input)) {
|
|
throw $this->reportError($input);
|
|
}
|
|
|
|
parent::check($input);
|
|
}
|
|
|
|
public function validate(mixed $input): bool
|
|
{
|
|
if (!$this->hasValidStructure($input)) {
|
|
return false;
|
|
}
|
|
|
|
return parent::validate($input);
|
|
}
|
|
|
|
public function getTemplate(mixed $input): string
|
|
{
|
|
if ($this->template !== null) {
|
|
return $this->template;
|
|
}
|
|
|
|
if (count($this->extraKeys)) {
|
|
return self::TEMPLATE_STRUCTURE_EXTRA;
|
|
}
|
|
|
|
return KeySet::TEMPLATE_STRUCTURE;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function getParams(): array
|
|
{
|
|
return [
|
|
'keys' => $this->keys,
|
|
'extraKeys' => $this->extraKeys,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @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()));
|
|
}
|
|
|
|
private function getKeyReference(Key $rule): mixed
|
|
{
|
|
return $rule->getReference();
|
|
}
|
|
|
|
private function hasValidStructure(mixed $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()]);
|
|
}
|
|
|
|
foreach ($input as $extraKey => &$ignoreValue) {
|
|
$this->extraKeys[] = $extraKey;
|
|
}
|
|
|
|
return count($input) == 0;
|
|
}
|
|
}
|