ValidationException.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Wikimedia\ParamValidator;
  3. use Exception;
  4. use Throwable;
  5. use UnexpectedValueException;
  6. /**
  7. * Error reporting for ParamValidator
  8. *
  9. * @since 1.34
  10. * @unstable
  11. */
  12. class ValidationException extends UnexpectedValueException {
  13. /** @var string */
  14. protected $paramName;
  15. /** @var mixed */
  16. protected $paramValue;
  17. /** @var array */
  18. protected $settings;
  19. /** @var string */
  20. protected $failureCode;
  21. /** @var (string|int|string[])[] */
  22. protected $failureData;
  23. /**
  24. * @param string $name Parameter name being validated
  25. * @param mixed $value Value of the parameter
  26. * @param array $settings Settings array being used for validation
  27. * @param string $code Failure code. See getFailureCode() for requirements.
  28. * @param (string|int|string[])[] $data Data for the failure code.
  29. * See getFailureData() for requirements.
  30. * @param Throwable|Exception|null $previous Previous exception causing this failure
  31. */
  32. public function __construct( $name, $value, $settings, $code, $data, $previous = null ) {
  33. parent::__construct( self::formatMessage( $name, $code, $data ), 0, $previous );
  34. $this->paramName = $name;
  35. $this->paramValue = $value;
  36. $this->settings = $settings;
  37. $this->failureCode = $code;
  38. $this->failureData = $data;
  39. }
  40. /**
  41. * Make a simple English message for the exception
  42. * @param string $name
  43. * @param string $code
  44. * @param array $data
  45. * @return string
  46. */
  47. private static function formatMessage( $name, $code, $data ) {
  48. $ret = "Validation of `$name` failed: $code";
  49. foreach ( $data as $k => $v ) {
  50. if ( is_array( $v ) ) {
  51. $v = implode( ', ', $v );
  52. }
  53. $ret .= "; $k => $v";
  54. }
  55. return $ret;
  56. }
  57. /**
  58. * Fetch the parameter name that failed validation
  59. * @return string
  60. */
  61. public function getParamName() {
  62. return $this->paramName;
  63. }
  64. /**
  65. * Fetch the parameter value that failed validation
  66. * @return mixed
  67. */
  68. public function getParamValue() {
  69. return $this->paramValue;
  70. }
  71. /**
  72. * Fetch the settings array that failed validation
  73. * @return array
  74. */
  75. public function getSettings() {
  76. return $this->settings;
  77. }
  78. /**
  79. * Fetch the validation failure code
  80. *
  81. * A validation failure code is a reasonably short string matching the regex
  82. * `/^[a-z][a-z0-9-]*$/`.
  83. *
  84. * Users are encouraged to use this with a suitable i18n mechanism rather
  85. * than relying on the limited English text returned by getMessage().
  86. *
  87. * @return string
  88. */
  89. public function getFailureCode() {
  90. return $this->failureCode;
  91. }
  92. /**
  93. * Fetch the validation failure data
  94. *
  95. * This returns additional data relevant to the particular failure code.
  96. *
  97. * Keys in the array are short ASCII strings. Values are strings or
  98. * integers, or arrays of strings intended to be displayed as a
  99. * comma-separated list. For any particular code the same keys are always
  100. * returned in the same order, making it safe to use array_values() and
  101. * access them positionally if that is desired.
  102. *
  103. * For example, the data for a hypothetical "integer-out-of-range" code
  104. * might have data `[ 'min' => 0, 'max' => 100 ]` indicating the range of
  105. * allowed values.
  106. *
  107. * @return (string|int|string[])[]
  108. */
  109. public function getFailureData() {
  110. return $this->failureData;
  111. }
  112. }