7use InvalidArgumentException;
20 const CT_HTML =
'text/html; charset=utf-8';
41 $json = json_encode( $value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
42 if ( $json ===
false ) {
64 public function createJson( $value, $contentType =
null ) {
67 $response->setHeader(
'Content-Type', $contentType );
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 )
197 $exception->getMessageValue() );
201 $exception->getCode(),
203 [
'message' => $exception->getMessage() ],
204 (array)$exception->getErrorData()
209 'message' =>
'Error: exception of type ' . get_class( $exception ),
210 'exception' => MWExceptionHandler::getStructuredExceptionData( $exception )
226 $originalValue = $value;
227 if ( is_scalar( $value ) ) {
228 $data = [
'value' => $value ];
229 } elseif ( is_array( $value ) || $value instanceof stdClass ) {
232 $type = gettype( $originalValue );
233 if (
$type ===
'object' ) {
234 $type = get_class( $originalValue );
236 throw new InvalidArgumentException( __METHOD__ .
": Invalid return value type $type" );
249 $response->setHeader(
'Content-Type', self::CT_HTML );
250 $response->setHeader(
'Location', $target );
261 $url = htmlspecialchars( $url );
262 return "<!doctype html><title>Redirect</title><a href=\"$url\">$url</a>";
266 if ( !$this->textFormatters ) {
271 foreach ( $this->textFormatters as $formatter ) {
272 $lang = LanguageCode::bcp47( $formatter->getLangCode() );
273 $messageText = $formatter->format( $messageValue );
274 $translations[
$lang] = $messageText;
276 return [
'messageTranslations' => $translations ];
Methods for dealing with language codes.
Handler class for MWExceptions.
if(!isset( $args[0])) $lang