mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TemplateData
synced 2024-11-27 17:20:01 +00:00
Merge "Split parameter validation off as separate methods"
This commit is contained in:
commit
e1b5970d3f
|
@ -118,12 +118,60 @@ class TemplateDataValidator {
|
||||||
// to decide whether 'inherits' will add it or not.
|
// to decide whether 'inherits' will add it or not.
|
||||||
$unnormalizedParams = unserialize( serialize( $data->params ) );
|
$unnormalizedParams = unserialize( serialize( $data->params ) );
|
||||||
|
|
||||||
|
$status = $this->validateParameters( $data->params );
|
||||||
|
if ( $status ) {
|
||||||
|
return $status;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ( $data->params as $paramName => $param ) {
|
foreach ( $data->params as $paramName => $param ) {
|
||||||
|
if ( isset( $param->inherits ) ) {
|
||||||
|
$parentParam = $data->params->{ $param->inherits };
|
||||||
|
foreach ( $parentParam as $key => $value ) {
|
||||||
|
if ( !isset( $unnormalizedParams->$paramName->$key ) ) {
|
||||||
|
$param->$key = is_object( $value ) ? clone $value : $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unset( $param->inherits );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->validateParameterOrder( $data->paramOrder ?? null, $data->params ) ??
|
||||||
|
$this->validateSets( $data->sets, $data->params ) ??
|
||||||
|
$this->validateMaps( $data->maps, $data->params ) ??
|
||||||
|
Status::newGood();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 ) {
|
||||||
if ( !( $param instanceof stdClass ) ) {
|
if ( !( $param instanceof stdClass ) ) {
|
||||||
return Status::newFatal( 'templatedata-invalid-type', "params.{$paramName}",
|
return Status::newFatal( 'templatedata-invalid-type', "params.{$paramName}",
|
||||||
'object' );
|
'object' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$status = $this->validateParameter( $paramName, $param );
|
||||||
|
if ( $status ) {
|
||||||
|
return $status;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( isset( $param->inherits ) && !isset( $params->{ $param->inherits } ) ) {
|
||||||
|
return Status::newFatal( 'templatedata-invalid-missing',
|
||||||
|
"params.{$param->inherits}" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 ) {
|
foreach ( $param as $key => $value ) {
|
||||||
if ( !in_array( $key, self::VALID_PARAM_KEYS ) ) {
|
if ( !in_array( $key, self::VALID_PARAM_KEYS ) ) {
|
||||||
return Status::newFatal( 'templatedata-invalid-unknown',
|
return Status::newFatal( 'templatedata-invalid-unknown',
|
||||||
|
@ -269,30 +317,8 @@ class TemplateDataValidator {
|
||||||
} else {
|
} else {
|
||||||
$param->suggestedvalues = [];
|
$param->suggestedvalues = [];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Param.inherits
|
return null;
|
||||||
// Done afterwards to avoid code duplication
|
|
||||||
foreach ( $data->params as $paramName => $param ) {
|
|
||||||
if ( isset( $param->inherits ) ) {
|
|
||||||
if ( !isset( $data->params->{ $param->inherits } ) ) {
|
|
||||||
return Status::newFatal( 'templatedata-invalid-missing',
|
|
||||||
"params.{$param->inherits}" );
|
|
||||||
}
|
|
||||||
$parentParam = $data->params->{ $param->inherits };
|
|
||||||
foreach ( $parentParam as $key => $value ) {
|
|
||||||
if ( !isset( $unnormalizedParams->$paramName->$key ) ) {
|
|
||||||
$param->$key = is_object( $value ) ? clone $value : $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unset( $param->inherits );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->validateParameterOrder( $data->paramOrder ?? null, $data->params ) ??
|
|
||||||
$this->validateSets( $data->sets, $data->params ) ??
|
|
||||||
$this->validateMaps( $data->maps, $data->params ) ??
|
|
||||||
Status::newGood();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue