| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?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\Exceptions\ValidationException;
- use Respect\Validation\Factory;
- use Respect\Validation\Validatable;
- use function array_keys;
- use function in_array;
- /**
- * @author Henrique Moody <henriquemoody@gmail.com>
- */
- final class KeyValue extends AbstractRule
- {
- /**
- * @var int|string
- */
- private $comparedKey;
- /**
- * @var string
- */
- private $ruleName;
- /**
- * @var int|string
- */
- private $baseKey;
- /**
- * @param int|string $comparedKey
- * @param int|string $baseKey
- */
- public function __construct($comparedKey, string $ruleName, $baseKey)
- {
- $this->comparedKey = $comparedKey;
- $this->ruleName = $ruleName;
- $this->baseKey = $baseKey;
- }
- /**
- * {@inheritDoc}
- */
- public function assert($input): void
- {
- $rule = $this->getRule($input);
- try {
- $rule->assert($input[$this->comparedKey]);
- } catch (ValidationException $exception) {
- throw $this->overwriteExceptionParams($exception);
- }
- }
- /**
- * {@inheritDoc}
- */
- public function check($input): void
- {
- $rule = $this->getRule($input);
- try {
- $rule->check($input[$this->comparedKey]);
- } catch (ValidationException $exception) {
- throw $this->overwriteExceptionParams($exception);
- }
- }
- /**
- * {@inheritDoc}
- */
- public function validate($input): bool
- {
- try {
- $rule = $this->getRule($input);
- } catch (ValidationException $e) {
- return false;
- }
- return $rule->validate($input[$this->comparedKey]);
- }
- /**
- * {@inheritDoc}
- */
- public function reportError($input, array $extraParams = []): ValidationException
- {
- try {
- return $this->overwriteExceptionParams($this->getRule($input)->reportError($input));
- } catch (ValidationException $exception) {
- return $this->overwriteExceptionParams($exception);
- }
- }
- /**
- * @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]]);
- $rule->setName((string) $this->comparedKey);
- } 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;
- }
- }
|