ValidationException.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Exceptions;
  8. use InvalidArgumentException;
  9. use Respect\Validation\Message\Formatter;
  10. use function key;
  11. /**
  12. * Default exception class for rule validations.
  13. *
  14. * @author Alexandre Gomes Gaigalas <alganet@gmail.com>
  15. * @author Henrique Moody <henriquemoody@gmail.com>
  16. */
  17. class ValidationException extends InvalidArgumentException implements Exception
  18. {
  19. public const MODE_DEFAULT = 'default';
  20. public const MODE_NEGATIVE = 'negative';
  21. public const STANDARD = 'standard';
  22. /**
  23. * Contains the default templates for exception message.
  24. *
  25. * @var string[][]
  26. */
  27. protected $defaultTemplates = [
  28. self::MODE_DEFAULT => [
  29. self::STANDARD => '{{name}} 必须有效',
  30. ],
  31. self::MODE_NEGATIVE => [
  32. self::STANDARD => '{{name}} 不能有效',
  33. ],
  34. ];
  35. /**
  36. * @var mixed
  37. */
  38. private $input;
  39. /**
  40. * @var string
  41. */
  42. private $id;
  43. /**
  44. * @var string
  45. */
  46. private $mode = self::MODE_DEFAULT;
  47. /**
  48. * @var mixed[]
  49. */
  50. private $params = [];
  51. /**
  52. * @var Formatter
  53. */
  54. private $formatter;
  55. /**
  56. * @var string
  57. */
  58. private $template;
  59. /**
  60. * @param mixed $input
  61. * @param mixed[] $params
  62. */
  63. public function __construct($input, string $id, array $params, Formatter $formatter)
  64. {
  65. $this->input = $input;
  66. $this->id = $id;
  67. $this->params = $params;
  68. $this->formatter = $formatter;
  69. $this->template = $this->chooseTemplate();
  70. parent::__construct($this->createMessage());
  71. }
  72. public function getId(): string
  73. {
  74. return $this->id;
  75. }
  76. /**
  77. * @return mixed[]
  78. */
  79. public function getParams(): array
  80. {
  81. return $this->params;
  82. }
  83. /**
  84. * @return mixed|null
  85. */
  86. public function getParam(string $name)
  87. {
  88. return $this->params[$name] ?? null;
  89. }
  90. public function setParam(string $name, mixed $value): void
  91. {
  92. $this->params[$name] = $value;
  93. }
  94. public function updateMode(string $mode): void
  95. {
  96. $this->mode = $mode;
  97. $this->message = $this->createMessage();
  98. }
  99. public function updateTemplate(string $template): void
  100. {
  101. $this->template = $template;
  102. $this->message = $this->createMessage();
  103. }
  104. /**
  105. * @param mixed[] $params
  106. */
  107. public function updateParams(array $params): void
  108. {
  109. $this->params = $params;
  110. $this->message = $this->createMessage();
  111. }
  112. public function hasCustomTemplate(): bool
  113. {
  114. return isset($this->defaultTemplates[$this->mode][$this->template]) === false;
  115. }
  116. protected function chooseTemplate(): string
  117. {
  118. return (string) key($this->defaultTemplates[$this->mode]);
  119. }
  120. private function createMessage(): string
  121. {
  122. return $this->formatter->format(
  123. $this->defaultTemplates[$this->mode][$this->template] ?? $this->template,
  124. $this->input,
  125. $this->params
  126. );
  127. }
  128. public function __toString(): string
  129. {
  130. return $this->getMessage();
  131. }
  132. }