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, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
41 if ( $json ===
false ) {
63 public function createJson( $value, $contentType =
null ) {
66 $response->setHeader(
'Content-Type', $contentType );
82 $response->setStatus( 204 );
97 $response->setStatus( 301 );
113 $response->setStatus( 302 );
127 $response->setStatus( 307 );
141 $response->setStatus( 303 );
157 $response->setStatus( 304 );
169 if ( $errorCode < 400 || $errorCode >= 600 ) {
170 throw new InvalidArgumentException(
'error code must be 4xx or 5xx' );
173 'httpCode' => $errorCode,
174 'httpReason' => HttpStatus::getMessage( $errorCode )
177 $response->setStatus( $errorCode );
193 array $extraData = []
197 array_merge( $extraData, $this->
formatMessage( $messageValue ) )
209 $exception->getCode(),
210 $exception->getMessageValue(),
211 (array)$exception->getErrorData()
216 $exception->getCode(),
218 [
'message' => $exception->getMessage() ],
219 (array)$exception->getErrorData()
224 'message' =>
'Error: exception of type ' . get_class( $exception ),
225 'exception' => MWExceptionHandler::getStructuredExceptionData( $exception )
241 $originalValue = $value;
242 if ( is_scalar( $value ) ) {
243 $data = [
'value' => $value ];
244 } elseif ( is_array( $value ) || $value instanceof stdClass ) {
247 $type = gettype( $originalValue );
248 if (
$type ===
'object' ) {
249 $type = get_class( $originalValue );
251 throw new InvalidArgumentException( __METHOD__ .
": Invalid return value type $type" );
264 $response->setHeader(
'Content-Type', self::CT_HTML );
265 $response->setHeader(
'Location', $target );
276 $url = htmlspecialchars( $url );
277 return "<!doctype html><title>Redirect</title><a href=\"$url\">$url</a>";
281 if ( !$this->textFormatters ) {
286 foreach ( $this->textFormatters as $formatter ) {
287 $lang = LanguageCode::bcp47( $formatter->getLangCode() );
288 $messageText = $formatter->format( $messageValue );
289 $translations[
$lang] = $messageText;
291 return [
'messageTranslations' => $translations ];
Methods for dealing with language codes.
Handler class for MWExceptions.
if(!isset( $args[0])) $lang