Debian Codegraph @

includes/libs/ParamValidator/ValidationException.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\ParamValidator;
4
5 use Throwable;
6 use UnexpectedValueException;
7 use Wikimedia\Message\DataMessageValue;
8
9 /**
10 * Error reporting for ParamValidator
11 *
12 * @newable
13 * @since 1.34
14 * @unstable
15 */
16 class ValidationException extends UnexpectedValueException {
17
18 /** @var DataMessageValue */
19 protected $failureMessage;
20
21 /** @var string */
22 protected $paramName;
23
24 /** @var mixed */
25 protected $paramValue;
26
27 /** @var array */
28 protected $settings;
29
30 /**
31 * @stable to call
32 * @param DataMessageValue $failureMessage
33 * @param string $name Parameter name being validated
34 * @param mixed $value Value of the parameter
35 * @param array $settings Settings array being used for validation
36 * @param Throwable|null $previous Previous throwable causing this failure
37 */
38 public function __construct(
39 DataMessageValue $failureMessage, $name, $value, $settings, ?Throwable $previous = null
40 ) {
41 $this->failureMessage = $failureMessage;
42 $this->paramName = $name;
43 $this->paramValue = $value;
44 $this->settings = $settings;
45
46 // Parent class needs some static English message.
47 $msg = "Validation of `$name` failed: " . $failureMessage->getCode();
48 $data = $failureMessage->getData();
49 if ( $data ) {
50 $msg .= ' ' . json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
51 }
52 parent::__construct( $msg, 0, $previous );
53 }
54
55 /**
56 * Fetch the validation failure message
57 *
58 * Users are encouraged to use this with an appropriate message formatter rather
59 * than relying on the limited English text returned by getMessage().
60 *
61 * @return DataMessageValue
62 */
63 public function getFailureMessage() {
64 return $this->failureMessage;
65 }
66
67 /**
68 * Fetch the parameter name that failed validation
69 * @return string
70 */
71 public function getParamName() {
72 return $this->paramName;
73 }
74
75 /**
76 * Fetch the parameter value that failed validation
77 * @return mixed
78 */
79 public function getParamValue() {
80 return $this->paramValue;
81 }
82
83 /**
84 * Fetch the settings array that failed validation
85 * @return array
86 */
87 public function getSettings() {
88 return $this->settings;
89 }
90
91 }