Parameter validation is based on parameter definitions like those in the Action API, using the new ParamValidator library. Handlers should use the provided Handler methods to access parameters rather than fetching them directly from the RequestInterface. Body validation allows the handler to have the (non-form-data) body of a request parsed and validated. The only validator included in this patch ignores the body entirely; future patches may implement validation for JSON bodies based on JSON schemas, or the like. Bug: T223239 Change-Id: I3c37ea2b432840514b6bff90007c8403989225d5
40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Rest;
|
|
|
|
/**
|
|
* A handler base class which unpacks parameters from the path template and
|
|
* passes them as formal parameters to run().
|
|
*
|
|
* run() must be declared in the subclass. It cannot be declared as abstract
|
|
* here because it has a variable parameter list.
|
|
* @todo Declare it as abstract after dropping HHVM
|
|
*
|
|
* @package MediaWiki\Rest
|
|
*/
|
|
class SimpleHandler extends Handler {
|
|
public function execute() {
|
|
$paramSettings = $this->getParamSettings();
|
|
$validatedParams = $this->getValidatedParams();
|
|
$unvalidatedParams = [];
|
|
$params = [];
|
|
foreach ( $this->getRequest()->getPathParams() as $name => $value ) {
|
|
$source = $paramSettings[$name][self::PARAM_SOURCE] ?? 'unknown';
|
|
if ( $source !== 'path' ) {
|
|
$unvalidatedParams[] = $name;
|
|
$params[] = $value;
|
|
} else {
|
|
$params[] = $validatedParams[$name];
|
|
}
|
|
}
|
|
|
|
if ( $unvalidatedParams ) {
|
|
throw new \LogicException(
|
|
'Path parameters were not validated: ' . implode( ', ', $unvalidatedParams )
|
|
);
|
|
}
|
|
|
|
// @phan-suppress-next-line PhanUndeclaredMethod
|
|
return $this->run( ...$params );
|
|
}
|
|
}
|