KeyValue.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. declare(strict_types=1);
  7. namespace Respect\Validation\Rules;
  8. use Respect\Validation\Exceptions\ComponentException;
  9. use Respect\Validation\Exceptions\ValidationException;
  10. use Respect\Validation\Factory;
  11. use Respect\Validation\Validatable;
  12. use function array_keys;
  13. use function in_array;
  14. /**
  15. * @author Henrique Moody <henriquemoody@gmail.com>
  16. */
  17. final class KeyValue extends AbstractRule
  18. {
  19. /**
  20. * @var int|string
  21. */
  22. private $comparedKey;
  23. /**
  24. * @var string
  25. */
  26. private $ruleName;
  27. /**
  28. * @var int|string
  29. */
  30. private $baseKey;
  31. /**
  32. * @param int|string $comparedKey
  33. * @param int|string $baseKey
  34. */
  35. public function __construct($comparedKey, string $ruleName, $baseKey)
  36. {
  37. $this->comparedKey = $comparedKey;
  38. $this->ruleName = $ruleName;
  39. $this->baseKey = $baseKey;
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function assert($input): void
  45. {
  46. $rule = $this->getRule($input);
  47. try {
  48. $rule->assert($input[$this->comparedKey]);
  49. } catch (ValidationException $exception) {
  50. throw $this->overwriteExceptionParams($exception);
  51. }
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function check($input): void
  57. {
  58. $rule = $this->getRule($input);
  59. try {
  60. $rule->check($input[$this->comparedKey]);
  61. } catch (ValidationException $exception) {
  62. throw $this->overwriteExceptionParams($exception);
  63. }
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function validate($input): bool
  69. {
  70. try {
  71. $rule = $this->getRule($input);
  72. } catch (ValidationException $e) {
  73. return false;
  74. }
  75. return $rule->validate($input[$this->comparedKey]);
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function reportError($input, array $extraParams = []): ValidationException
  81. {
  82. try {
  83. return $this->overwriteExceptionParams($this->getRule($input)->reportError($input));
  84. } catch (ValidationException $exception) {
  85. return $this->overwriteExceptionParams($exception);
  86. }
  87. }
  88. /**
  89. * @param mixed $input
  90. */
  91. private function getRule($input): Validatable
  92. {
  93. if (!isset($input[$this->comparedKey])) {
  94. throw parent::reportError($this->comparedKey);
  95. }
  96. if (!isset($input[$this->baseKey])) {
  97. throw parent::reportError($this->baseKey);
  98. }
  99. try {
  100. $rule = Factory::getDefaultInstance()->rule($this->ruleName, [$input[$this->baseKey]]);
  101. $rule->setName((string) $this->comparedKey);
  102. } catch (ComponentException $exception) {
  103. throw parent::reportError($input, ['component' => true]);
  104. }
  105. return $rule;
  106. }
  107. private function overwriteExceptionParams(ValidationException $exception): ValidationException
  108. {
  109. $params = [];
  110. foreach (array_keys($exception->getParams()) as $key) {
  111. if (in_array($key, ['template', 'translator'])) {
  112. continue;
  113. }
  114. $params[$key] = $this->baseKey;
  115. }
  116. $params['name'] = $this->comparedKey;
  117. $exception->updateParams($params);
  118. return $exception;
  119. }
  120. }