Trying to get away with returning a single code and parameter-list that was supposed to represent both human-readable and machine-readable data was a mistake. This patch converts it to use DataMessageValue, which represents the two separately and also provides guidance for supplying translations of all the error codes. This also eliminates the "describeSettings()" method that was trying to serve multiple use cases (in terms of the Action API, action=paraminfo and action=help). It's replaced by two methods that each serve one of the use cases. Also some of the functionality was moved out of the TypeDef base class into ParamValidator, to better match where the constants themselves live. Also I wound up creating a NumericDef base class so FloatDef can share the same range-checking logic that IntegerDef has. I probably should have done that as a separate patch, but untangling it now would be too much work. Bug: T235801 Change-Id: Iea6d4a1d05bb4b92d60415b0f03ff9d3dc99a80b
88 lines
2 KiB
PHP
88 lines
2 KiB
PHP
<?php
|
|
|
|
namespace Wikimedia\ParamValidator;
|
|
|
|
use Wikimedia\Message\DataMessageValue;
|
|
use Wikimedia\ParamValidator\Util\UploadedFile;
|
|
|
|
/**
|
|
* Simple Callbacks implementation for $_GET/$_POST/$_FILES data
|
|
*
|
|
* Options array keys used by this class:
|
|
* - 'useHighLimits': (bool) Return value from useHighLimits()
|
|
*
|
|
* @since 1.34
|
|
* @unstable
|
|
*/
|
|
class SimpleCallbacks implements Callbacks {
|
|
|
|
/** @var (string|string[])[] $_GET/$_POST data */
|
|
private $params;
|
|
|
|
/** @var (array|UploadedFile)[] $_FILES data or UploadedFile instances */
|
|
private $files;
|
|
|
|
/** @var array[] Any recorded conditions */
|
|
private $conditions = [];
|
|
|
|
/**
|
|
* @param (string|string[])[] $params Data from $_POST + $_GET
|
|
* @param array[] $files Data from $_FILES
|
|
*/
|
|
public function __construct( array $params, array $files = [] ) {
|
|
$this->params = $params;
|
|
$this->files = $files;
|
|
}
|
|
|
|
public function hasParam( $name, array $options ) {
|
|
return isset( $this->params[$name] );
|
|
}
|
|
|
|
public function getValue( $name, $default, array $options ) {
|
|
return $this->params[$name] ?? $default;
|
|
}
|
|
|
|
public function hasUpload( $name, array $options ) {
|
|
return isset( $this->files[$name] );
|
|
}
|
|
|
|
public function getUploadedFile( $name, array $options ) {
|
|
$file = $this->files[$name] ?? null;
|
|
if ( $file && !$file instanceof UploadedFile ) {
|
|
$file = new UploadedFile( $file );
|
|
$this->files[$name] = $file;
|
|
}
|
|
return $file;
|
|
}
|
|
|
|
public function recordCondition(
|
|
DataMessageValue $message, $name, $value, array $settings, array $options
|
|
) {
|
|
$this->conditions[] = [
|
|
'message' => $message,
|
|
'name' => $name,
|
|
'value' => $value,
|
|
'settings' => $settings,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Fetch any recorded conditions
|
|
* @return array[]
|
|
*/
|
|
public function getRecordedConditions() {
|
|
return $this->conditions;
|
|
}
|
|
|
|
/**
|
|
* Clear any recorded conditions
|
|
*/
|
|
public function clearRecordedConditions() {
|
|
$this->conditions = [];
|
|
}
|
|
|
|
public function useHighLimits( array $options ) {
|
|
return !empty( $options['useHighLimits'] );
|
|
}
|
|
|
|
}
|