6use InvalidArgumentException;
18 private const CT_PLAIN =
'text/plain; charset=utf-8';
19 private const CT_HTML =
'text/html; charset=utf-8';
20 private const CT_JSON =
'application/json';
40 $json = json_encode( $value,
41 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE );
42 if ( $json ===
false ) {
64 public function createJson( $value, $contentType =
null ) {
67 $response->setHeader(
'Content-Type', $contentType );
83 $response->setStatus( 204 );
98 $response->setStatus( 301 );
114 $response->setStatus( 302 );
128 $response->setStatus( 307 );
142 $response->setStatus( 303 );
158 $response->setStatus( 304 );
170 if ( $errorCode < 400 || $errorCode >= 600 ) {
171 throw new InvalidArgumentException(
'error code must be 4xx or 5xx' );
174 'httpCode' => $errorCode,
175 'httpReason' => HttpStatus::getMessage( $errorCode )
178 $response->setStatus( $errorCode );
194 array $extraData = []
198 array_merge( $extraData, $this->
formatMessage( $messageValue ) )
210 $exception->getCode(),
211 $exception->getMessageValue(),
212 (array)$exception->getErrorData()
215 return $exception->getResponse();
218 $response->setStatus( $exception->getCode() );
220 if ( in_array( $exception->getCode(), [ 204, 304 ],
true ) ) {
221 $response = $this->
create();
222 $response->setStatus( $exception->getCode() );
225 $exception->getCode(),
227 [
'message' => $exception->getMessage() ],
228 (array)$exception->getErrorData()
234 'message' =>
'Error: exception of type ' . get_class( $exception ),
235 'exception' => MWExceptionHandler::getStructuredExceptionData( $exception )
251 $originalValue = $value;
252 if ( is_scalar( $value ) ) {
253 $data = [
'value' => $value ];
254 } elseif ( is_array( $value ) || $value instanceof stdClass ) {
257 $type = gettype( $originalValue );
258 if (
$type ===
'object' ) {
259 $type = get_class( $originalValue );
261 throw new InvalidArgumentException( __METHOD__ .
": Invalid return value type $type" );
274 $response->setHeader(
'Content-Type', self::CT_HTML );
275 $response->setHeader(
'Location', $target );
286 $url = htmlspecialchars( $url );
287 return "<!doctype html><title>Redirect</title><a href=\"$url\">$url</a>";
291 if ( !$this->textFormatters ) {
296 foreach ( $this->textFormatters as $formatter ) {
297 $lang = LanguageCode::bcp47( $formatter->getLangCode() );
298 $messageText = $formatter->format( $messageValue );
299 $translations[
$lang] = $messageText;
301 return [
'messageTranslations' => $translations ];
Methods for dealing with language codes.
Handler class for MWExceptions.
if(!isset( $args[0])) $lang