mediawiki-extensions-Templa.../includes/TemplateDataStatus.php
thiemowmde 584fdcddf6 Extract serialization methods into TemplateDataStatus class
This makes the large Hook class quite a bit smaller.

Change-Id: I55229116eb16ccd9be21d1f34de5e52826ece2bf
2023-03-09 11:46:49 +01:00

51 lines
1.2 KiB
PHP

<?php
namespace MediaWiki\Extension\TemplateData;
use Status;
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;
}
}