2018-05-22 23:23:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Wikimedia\ParamValidator;
|
|
|
|
|
|
|
|
|
|
use Throwable;
|
|
|
|
|
use UnexpectedValueException;
|
2019-09-12 17:44:11 +00:00
|
|
|
use Wikimedia\Message\DataMessageValue;
|
2018-05-22 23:23:20 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Error reporting for ParamValidator
|
|
|
|
|
*
|
2020-06-29 12:13:29 +00:00
|
|
|
* @newable
|
2018-05-22 23:23:20 +00:00
|
|
|
* @since 1.34
|
2019-11-01 17:48:59 +00:00
|
|
|
* @unstable
|
2018-05-22 23:23:20 +00:00
|
|
|
*/
|
|
|
|
|
class ValidationException extends UnexpectedValueException {
|
|
|
|
|
|
2019-09-12 17:44:11 +00:00
|
|
|
/** @var DataMessageValue */
|
|
|
|
|
protected $failureMessage;
|
|
|
|
|
|
2018-05-22 23:23:20 +00:00
|
|
|
/** @var string */
|
|
|
|
|
protected $paramName;
|
|
|
|
|
|
|
|
|
|
/** @var mixed */
|
|
|
|
|
protected $paramValue;
|
|
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
|
protected $settings;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-13 08:53:06 +00:00
|
|
|
* @stable to call
|
2021-11-26 15:21:17 +00:00
|
|
|
* @param DataMessageValue $failureMessage
|
2018-05-22 23:23:20 +00:00
|
|
|
* @param string $name Parameter name being validated
|
|
|
|
|
* @param mixed $value Value of the parameter
|
|
|
|
|
* @param array $settings Settings array being used for validation
|
2019-12-29 19:44:43 +00:00
|
|
|
* @param Throwable|null $previous Previous throwable causing this failure
|
2018-05-22 23:23:20 +00:00
|
|
|
*/
|
2019-09-12 17:44:11 +00:00
|
|
|
public function __construct(
|
2024-10-16 18:58:33 +00:00
|
|
|
DataMessageValue $failureMessage, $name, $value, $settings, ?Throwable $previous = null
|
2019-09-12 17:44:11 +00:00
|
|
|
) {
|
|
|
|
|
$this->failureMessage = $failureMessage;
|
2018-05-22 23:23:20 +00:00
|
|
|
$this->paramName = $name;
|
|
|
|
|
$this->paramValue = $value;
|
|
|
|
|
$this->settings = $settings;
|
2019-09-12 17:44:11 +00:00
|
|
|
|
|
|
|
|
// Parent class needs some static English message.
|
|
|
|
|
$msg = "Validation of `$name` failed: " . $failureMessage->getCode();
|
|
|
|
|
$data = $failureMessage->getData();
|
|
|
|
|
if ( $data ) {
|
|
|
|
|
$msg .= ' ' . json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
|
|
|
|
|
}
|
|
|
|
|
parent::__construct( $msg, 0, $previous );
|
2018-05-22 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-09-12 17:44:11 +00:00
|
|
|
* Fetch the validation failure message
|
|
|
|
|
*
|
|
|
|
|
* Users are encouraged to use this with an appropriate message formatter rather
|
|
|
|
|
* than relying on the limited English text returned by getMessage().
|
|
|
|
|
*
|
|
|
|
|
* @return DataMessageValue
|
2018-05-22 23:23:20 +00:00
|
|
|
*/
|
2019-09-12 17:44:11 +00:00
|
|
|
public function getFailureMessage() {
|
|
|
|
|
return $this->failureMessage;
|
2018-05-22 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the parameter name that failed validation
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getParamName() {
|
|
|
|
|
return $this->paramName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the parameter value that failed validation
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getParamValue() {
|
|
|
|
|
return $this->paramValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the settings array that failed validation
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getSettings() {
|
|
|
|
|
return $this->settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|