2021-12-23 09:10:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\TemplateData;
|
|
|
|
|
2024-01-04 20:53:10 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2021-12-23 09:10:28 +00:00
|
|
|
use stdClass;
|
|
|
|
|
2023-08-23 07:29:25 +00:00
|
|
|
/**
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
2021-12-23 09:10:28 +00:00
|
|
|
class TemplateDataValidator {
|
|
|
|
|
|
|
|
public const PREDEFINED_FORMATS = [
|
|
|
|
'block' => "{{_\n| _ = _\n}}",
|
|
|
|
'inline' => '{{_|_=_}}',
|
|
|
|
];
|
|
|
|
|
|
|
|
private const VALID_ROOT_KEYS = [
|
|
|
|
'description',
|
|
|
|
'params',
|
|
|
|
'paramOrder',
|
|
|
|
'sets',
|
|
|
|
'maps',
|
|
|
|
'format',
|
|
|
|
];
|
|
|
|
|
|
|
|
private const VALID_PARAM_KEYS = [
|
|
|
|
'label',
|
|
|
|
'required',
|
|
|
|
'suggested',
|
|
|
|
'description',
|
|
|
|
'example',
|
|
|
|
'deprecated',
|
|
|
|
'aliases',
|
|
|
|
'autovalue',
|
|
|
|
'default',
|
|
|
|
'inherits',
|
|
|
|
'type',
|
|
|
|
'suggestedvalues',
|
|
|
|
];
|
|
|
|
|
|
|
|
private const VALID_TYPES = [
|
|
|
|
'content',
|
|
|
|
'line',
|
|
|
|
'number',
|
|
|
|
'boolean',
|
|
|
|
'string',
|
|
|
|
'date',
|
|
|
|
'unbalanced-wikitext',
|
|
|
|
'unknown',
|
|
|
|
'url',
|
|
|
|
'wiki-page-name',
|
|
|
|
'wiki-user-name',
|
|
|
|
'wiki-file-name',
|
|
|
|
'wiki-template-name',
|
|
|
|
];
|
|
|
|
|
2021-12-23 15:08:01 +00:00
|
|
|
/** @var string[] */
|
2023-08-23 07:29:25 +00:00
|
|
|
private array $validParameterTypes;
|
2021-12-23 15:08:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $additionalParameterTypes
|
|
|
|
*/
|
|
|
|
public function __construct( array $additionalParameterTypes ) {
|
|
|
|
$this->validParameterTypes = array_merge( self::VALID_TYPES, $additionalParameterTypes );
|
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $data
|
|
|
|
*
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
public function validate( $data ): Status {
|
|
|
|
if ( $data === null ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-parse' );
|
|
|
|
}
|
|
|
|
|
2022-02-01 14:49:06 +00:00
|
|
|
if ( !( $data instanceof stdClass ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'templatedata', 'object' );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $data as $key => $value ) {
|
|
|
|
if ( !in_array( $key, self::VALID_ROOT_KEYS ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-unknown', $key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root.description
|
|
|
|
if ( isset( $data->description ) ) {
|
|
|
|
if ( !$this->isValidInterfaceText( $data->description ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'description',
|
|
|
|
'string|object' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root.format
|
2021-12-23 15:08:01 +00:00
|
|
|
if ( isset( $data->format ) ) {
|
|
|
|
if ( !is_string( $data->format ) ||
|
|
|
|
!( isset( self::PREDEFINED_FORMATS[$data->format] ) ||
|
|
|
|
$this->isValidCustomFormatString( $data->format )
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-format', 'format' );
|
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Root.params
|
|
|
|
if ( !isset( $data->params ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-missing', 'params', 'object' );
|
|
|
|
}
|
|
|
|
|
2022-02-01 14:49:06 +00:00
|
|
|
if ( !( $data->params instanceof stdClass ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'params', 'object' );
|
|
|
|
}
|
|
|
|
|
2021-12-23 15:08:01 +00:00
|
|
|
return $this->validateParameters( $data->params ) ??
|
|
|
|
$this->validateParameterOrder( $data->paramOrder ?? null, $data->params ) ??
|
|
|
|
$this->validateSets( $data->sets ?? [], $data->params ) ??
|
|
|
|
$this->validateMaps( $data->maps ?? (object)[], $data->params ) ??
|
2020-08-26 11:25:10 +00:00
|
|
|
Status::newGood( $data );
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param stdClass $params
|
|
|
|
* @return Status|null Null on success, otherwise a Status object with the error message
|
|
|
|
*/
|
|
|
|
private function validateParameters( stdClass $params ): ?Status {
|
|
|
|
foreach ( $params as $paramName => $param ) {
|
2023-04-11 16:20:31 +00:00
|
|
|
if ( trim( $paramName ) === '' ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-unnamed-parameter' );
|
|
|
|
}
|
|
|
|
|
2022-02-01 14:49:06 +00:00
|
|
|
if ( !( $param instanceof stdClass ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type', "params.{$paramName}",
|
|
|
|
'object' );
|
|
|
|
}
|
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
$status = $this->validateParameter( $paramName, $param );
|
|
|
|
if ( $status ) {
|
|
|
|
return $status;
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
if ( isset( $param->inherits ) && !isset( $params->{ $param->inherits } ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-missing',
|
|
|
|
"params.{$param->inherits}" );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $paramName
|
|
|
|
* @param stdClass $param
|
|
|
|
* @return Status|null Null on success, otherwise a Status object with the error message
|
|
|
|
*/
|
|
|
|
private function validateParameter( string $paramName, stdClass $param ): ?Status {
|
|
|
|
foreach ( $param as $key => $value ) {
|
|
|
|
if ( !in_array( $key, self::VALID_PARAM_KEYS ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-unknown',
|
|
|
|
"params.{$paramName}.{$key}" );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.label
|
|
|
|
if ( isset( $param->label ) ) {
|
|
|
|
if ( !$this->isValidInterfaceText( $param->label ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.label", 'string|object' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.required
|
|
|
|
if ( isset( $param->required ) ) {
|
|
|
|
if ( !is_bool( $param->required ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.required", 'boolean' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.suggested
|
|
|
|
if ( isset( $param->suggested ) ) {
|
|
|
|
if ( !is_bool( $param->suggested ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.suggested", 'boolean' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.description
|
|
|
|
if ( isset( $param->description ) ) {
|
|
|
|
if ( !$this->isValidInterfaceText( $param->description ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.description", 'string|object' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.example
|
|
|
|
if ( isset( $param->example ) ) {
|
|
|
|
if ( !$this->isValidInterfaceText( $param->example ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.example", 'string|object' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.deprecated
|
|
|
|
if ( isset( $param->deprecated ) ) {
|
|
|
|
if ( !is_bool( $param->deprecated ) && !is_string( $param->deprecated ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.deprecated", 'boolean|string' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.aliases
|
|
|
|
if ( isset( $param->aliases ) ) {
|
|
|
|
if ( !is_array( $param->aliases ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.aliases", 'array' );
|
|
|
|
}
|
2021-12-23 15:08:01 +00:00
|
|
|
foreach ( $param->aliases as $i => $alias ) {
|
|
|
|
if ( !is_int( $alias ) && !is_string( $alias ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
2022-07-15 09:04:30 +00:00
|
|
|
"params.{$paramName}.aliases[$i]", 'int|string' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-15 09:04:30 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.autovalue
|
|
|
|
if ( isset( $param->autovalue ) ) {
|
|
|
|
if ( !is_string( $param->autovalue ) ) {
|
|
|
|
// TODO: Validate the autovalue values.
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.autovalue", 'string' );
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.default
|
|
|
|
if ( isset( $param->default ) ) {
|
|
|
|
if ( !$this->isValidInterfaceText( $param->default ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.default", 'string|object' );
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.type
|
|
|
|
if ( isset( $param->type ) ) {
|
|
|
|
if ( !is_string( $param->type ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.type", 'string' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 15:08:01 +00:00
|
|
|
if ( !in_array( $param->type, $this->validParameterTypes ) ) {
|
2022-07-15 09:04:30 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-value',
|
|
|
|
'params.' . $paramName . '.type' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
// Param.suggestedvalues
|
|
|
|
if ( isset( $param->suggestedvalues ) ) {
|
|
|
|
if ( !is_array( $param->suggestedvalues ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.suggestedvalues", 'array' );
|
|
|
|
}
|
|
|
|
foreach ( $param->suggestedvalues as $i => $value ) {
|
|
|
|
if ( !is_string( $value ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.suggestedvalues[$i]", 'string' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 09:04:30 +00:00
|
|
|
return null;
|
2021-12-23 10:31:01 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2021-12-23 10:31:01 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $paramOrder
|
|
|
|
* @param stdClass $params
|
|
|
|
*
|
|
|
|
* @return Status|null
|
|
|
|
*/
|
|
|
|
private function validateParameterOrder( $paramOrder, stdClass $params ): ?Status {
|
|
|
|
if ( $paramOrder === null ) {
|
|
|
|
return null;
|
|
|
|
} elseif ( !is_array( $paramOrder ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'paramOrder', 'array' );
|
|
|
|
} elseif ( count( $paramOrder ) < count( (array)$params ) ) {
|
2023-08-21 10:42:33 +00:00
|
|
|
$missing = array_diff( array_keys( (array)$params ), $paramOrder );
|
|
|
|
return Status::newFatal( 'templatedata-invalid-missing',
|
2023-08-23 07:29:25 +00:00
|
|
|
'paramOrder[ "' . implode( '", "', $missing ) . '" ]' );
|
2021-12-23 10:31:01 +00:00
|
|
|
}
|
2021-12-23 09:10:28 +00:00
|
|
|
|
2021-12-23 10:31:01 +00:00
|
|
|
// Validate each of the values corresponds to a parameter and that there are no
|
|
|
|
// duplicates
|
|
|
|
$seen = [];
|
|
|
|
foreach ( $paramOrder as $i => $param ) {
|
|
|
|
if ( !isset( $params->$param ) ) {
|
2023-08-21 10:42:33 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-value', "paramOrder[ \"$param\" ]" );
|
2021-12-23 10:31:01 +00:00
|
|
|
}
|
|
|
|
if ( isset( $seen[$param] ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-duplicate-value',
|
|
|
|
"paramOrder[$i]", "paramOrder[{$seen[$param]}]", $param );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
2021-12-23 10:31:01 +00:00
|
|
|
$seen[$param] = $i;
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 10:31:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-23 15:08:01 +00:00
|
|
|
* @param mixed $sets
|
2021-12-23 10:31:01 +00:00
|
|
|
* @param stdClass $params
|
|
|
|
*
|
|
|
|
* @return Status|null
|
|
|
|
*/
|
2021-12-23 15:08:01 +00:00
|
|
|
private function validateSets( $sets, stdClass $params ): ?Status {
|
|
|
|
if ( !is_array( $sets ) ) {
|
2021-12-23 10:31:01 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'sets', 'array' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 10:31:01 +00:00
|
|
|
foreach ( $sets as $setNr => $setObj ) {
|
2022-02-01 14:49:06 +00:00
|
|
|
if ( !( $setObj instanceof stdClass ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-value', "sets.{$setNr}" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !isset( $setObj->label ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-missing', "sets.{$setNr}.label",
|
|
|
|
'string|object' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$this->isValidInterfaceText( $setObj->label ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', "sets.{$setNr}.label",
|
|
|
|
'string|object' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !isset( $setObj->params ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-missing', "sets.{$setNr}.params",
|
|
|
|
'array' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !is_array( $setObj->params ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', "sets.{$setNr}.params",
|
|
|
|
'array' );
|
|
|
|
}
|
|
|
|
|
2024-04-08 11:48:33 +00:00
|
|
|
if ( !$setObj->params ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-empty-array',
|
|
|
|
"sets.{$setNr}.params" );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $setObj->params as $i => $param ) {
|
2021-12-23 10:31:01 +00:00
|
|
|
if ( !isset( $params->$param ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-value',
|
|
|
|
"sets.{$setNr}.params[$i]" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 10:31:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-23 15:08:01 +00:00
|
|
|
* @param mixed $maps
|
2021-12-23 10:31:01 +00:00
|
|
|
* @param stdClass $params
|
|
|
|
*
|
|
|
|
* @return Status|null
|
|
|
|
*/
|
2021-12-23 15:08:01 +00:00
|
|
|
private function validateMaps( $maps, stdClass $params ): ?Status {
|
|
|
|
if ( !( $maps instanceof stdClass ) ) {
|
2021-12-23 10:31:01 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'maps', 'object' );
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 10:31:01 +00:00
|
|
|
foreach ( $maps as $consumerId => $map ) {
|
2022-02-01 14:49:06 +00:00
|
|
|
if ( !( $map instanceof stdClass ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type', "maps.$consumerId",
|
|
|
|
'object' );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $map as $key => $value ) {
|
|
|
|
// Key is not validated as this is used by a third-party application
|
|
|
|
// Value must be 2d array of parameter names, 1d array of parameter names, or valid
|
|
|
|
// parameter name
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
foreach ( $value as $key2 => $value2 ) {
|
|
|
|
if ( is_array( $value2 ) ) {
|
|
|
|
foreach ( $value2 as $key3 => $value3 ) {
|
|
|
|
if ( !is_string( $value3 ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"maps.{$consumerId}.{$key}[$key2][$key3]", 'string' );
|
|
|
|
}
|
2021-12-23 10:31:01 +00:00
|
|
|
if ( !isset( $params->$value3 ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-param', $value3,
|
|
|
|
"maps.$consumerId.{$key}[$key2][$key3]" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ( is_string( $value2 ) ) {
|
2021-12-23 10:31:01 +00:00
|
|
|
if ( !isset( $params->$value2 ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-param', $value2,
|
|
|
|
"maps.$consumerId.{$key}[$key2]" );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"maps.{$consumerId}.{$key}[$key2]", 'string|array' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ( is_string( $value ) ) {
|
2021-12-23 10:31:01 +00:00
|
|
|
if ( !isset( $params->$value ) ) {
|
2021-12-23 09:10:28 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-param', $value,
|
|
|
|
"maps.{$consumerId}.{$key}" );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"maps.{$consumerId}.{$key}", 'string|array' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 10:31:01 +00:00
|
|
|
|
|
|
|
return null;
|
2021-12-23 09:10:28 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 09:40:19 +00:00
|
|
|
private function isValidCustomFormatString( ?string $format ): bool {
|
|
|
|
return $format && preg_match( '/^\n?{{ *_+\n? *\|\n? *_+ *= *_+\n? *}}\n?$/', $format );
|
|
|
|
}
|
|
|
|
|
2021-12-23 09:10:28 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $text
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isValidInterfaceText( $text ): bool {
|
|
|
|
if ( $text instanceof stdClass ) {
|
|
|
|
$isEmpty = true;
|
|
|
|
// An (array) cast would return private/protected properties as well
|
|
|
|
foreach ( get_object_vars( $text ) as $languageCode => $string ) {
|
|
|
|
// TODO: Do we need to validate if these are known interface language codes?
|
|
|
|
if ( !is_string( $languageCode ) ||
|
|
|
|
ltrim( $languageCode ) === '' ||
|
|
|
|
!is_string( $string )
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$isEmpty = false;
|
|
|
|
}
|
|
|
|
return !$isEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_string( $text );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|