3 namespace Wikimedia\ParamValidator
;
5 use Wikimedia\ParamValidator\Util\UploadedFile
;
8 * Simple Callbacks implementation for $_GET/$_POST/$_FILES data
10 * Options array keys used by this class:
11 * - 'useHighLimits': (bool) Return value from useHighLimits()
16 class SimpleCallbacks
implements Callbacks
{
18 /** @var (string|string[])[] $_GET/$_POST data */
21 /** @var (array|UploadedFile)[] $_FILES data or UploadedFile instances */
24 /** @var array Any recorded conditions */
25 private $conditions = [];
28 * @param (string|string[])[] $params Data from $_POST + $_GET
29 * @param array[] $files Data from $_FILES
31 public function __construct( array $params, array $files = [] ) {
32 $this->params
= $params;
33 $this->files
= $files;
36 public function hasParam( $name, array $options ) {
37 return isset( $this->params
[$name] );
40 public function getValue( $name, $default, array $options ) {
41 return $this->params
[$name] ??
$default;
44 public function hasUpload( $name, array $options ) {
45 return isset( $this->files
[$name] );
48 public function getUploadedFile( $name, array $options ) {
49 $file = $this->files
[$name] ??
null;
50 if ( $file && !$file instanceof UploadedFile
) {
51 $file = new UploadedFile( $file );
52 $this->files
[$name] = $file;
57 public function recordCondition( ValidationException
$condition, array $options ) {
58 $this->conditions
[] = $condition;
62 * Fetch any recorded conditions
65 public function getRecordedConditions() {
66 return $this->conditions
;
70 * Clear any recorded conditions
72 public function clearRecordedConditions() {
73 $this->conditions
= [];
76 public function useHighLimits( array $options ) {
77 return !empty( $options['useHighLimits'] );