2015-10-08 18:54:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-10-08 18:54:42 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-10-08 18:54:42 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
2019-04-23 19:35:37 +02:00
|
|
|
use Respect\Validation\Factory;
|
2019-02-07 21:22:10 +01:00
|
|
|
use Respect\Validation\Validatable;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2019-02-09 13:31:32 +01:00
|
|
|
use function array_keys;
|
2019-02-17 19:41:55 +01:00
|
|
|
use function in_array;
|
2015-10-08 18:54:42 +02:00
|
|
|
|
2018-11-25 17:56:04 +01:00
|
|
|
/**
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
final class KeyValue extends AbstractRule
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var int|string
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
private $comparedKey;
|
2019-02-07 20:46:46 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
private $ruleName;
|
2019-02-07 20:46:46 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @var int|string
|
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
private $baseKey;
|
2015-10-08 18:54:42 +02:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param int|string $comparedKey
|
|
|
|
* @param int|string $baseKey
|
|
|
|
*/
|
|
|
|
public function __construct($comparedKey, string $ruleName, $baseKey)
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$this->comparedKey = $comparedKey;
|
|
|
|
$this->ruleName = $ruleName;
|
|
|
|
$this->baseKey = $baseKey;
|
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-01-28 17:16:53 +01:00
|
|
|
public function assert($input): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = $this->getRule($input);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$rule->assert($input[$this->comparedKey]);
|
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
throw $this->overwriteExceptionParams($exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-01-28 16:14:00 +01:00
|
|
|
public function check($input): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = $this->getRule($input);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$rule->check($input[$this->comparedKey]);
|
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
throw $this->overwriteExceptionParams($exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2019-02-07 21:22:10 +01:00
|
|
|
*/
|
2018-01-28 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$rule = $this->getRule($input);
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rule->validate($input[$this->comparedKey]);
|
|
|
|
}
|
2019-04-23 19:35:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function reportError($input, array $extraParams = []): ValidationException
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->overwriteExceptionParams($this->getRule($input)->reportError($input));
|
|
|
|
} catch (ValidationException $exception) {
|
|
|
|
return $this->overwriteExceptionParams($exception);
|
|
|
|
}
|
|
|
|
}
|
2019-05-11 19:59:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
*/
|
|
|
|
private function getRule($input): Validatable
|
|
|
|
{
|
|
|
|
if (!isset($input[$this->comparedKey])) {
|
|
|
|
throw parent::reportError($this->comparedKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($input[$this->baseKey])) {
|
|
|
|
throw parent::reportError($this->baseKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$rule = Factory::getDefaultInstance()->rule($this->ruleName, [$input[$this->baseKey]]);
|
2020-07-22 14:02:39 +02:00
|
|
|
$rule->setName((string) $this->comparedKey);
|
2019-05-11 19:59:12 +02:00
|
|
|
} catch (ComponentException $exception) {
|
|
|
|
throw parent::reportError($input, ['component' => true]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function overwriteExceptionParams(ValidationException $exception): ValidationException
|
|
|
|
{
|
|
|
|
$params = [];
|
|
|
|
foreach (array_keys($exception->getParams()) as $key) {
|
|
|
|
if (in_array($key, ['template', 'translator'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$params[$key] = $this->baseKey;
|
|
|
|
}
|
|
|
|
$params['name'] = $this->comparedKey;
|
|
|
|
|
|
|
|
$exception->updateParams($params);
|
|
|
|
|
|
|
|
return $exception;
|
|
|
|
}
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|