mediawiki-extensions-Templa.../includes/TemplateDataStatus.php
thiemowmde 8f81812242 Enable and enforce a few extra PHPCS sniffs
This is not really anything new. Most code already followed these
sniffs. This patch just fixes the remaining exceptions. Also:
* Remove PHPDoc blocks that don't add anything but just repeat the
  strict types.
* Remove @file comments in favor of class-level comments.
* Add strict types where possible, most notably some `void`.

Change-Id: Iff6872dff68170b0fc4e82ac2ba3cad385e8773e
2023-09-06 18:15:22 +02:00

54 lines
1.2 KiB
PHP

<?php
namespace MediaWiki\Extension\TemplateData;
use Status;
/**
* @license GPL-2.0-or-later
*/
class TemplateDataStatus {
/**
* @param Status $status
* @return array contains StatusValue ok and errors fields (does not serialize value)
*/
public static function jsonSerialize( Status $status ): array {
if ( $status->isOK() ) {
return [ 'ok' => true ];
}
[ $errorsOnlyStatus, $warningsOnlyStatus ] = $status->splitByErrorType();
// note that non-scalar values are not supported in errors or warnings
return [
'ok' => false,
'errors' => $errorsOnlyStatus->getErrors(),
'warnings' => $warningsOnlyStatus->getErrors()
];
}
/**
* @param Status|array|null $json contains StatusValue ok and errors fields (does not serialize value)
* @return Status|null
*/
public static function newFromJson( $json ): ?Status {
if ( !is_array( $json ) ) {
return $json;
}
if ( $json['ok'] ) {
return Status::newGood();
}
$status = new Status();
foreach ( $json['errors'] as $error ) {
$status->fatal( $error['message'], ...$error['params'] );
}
foreach ( $json['warnings'] as $warning ) {
$status->warning( $warning['message'], ...$warning['params'] );
}
return $status;
}
}