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()
15 class SimpleCallbacks
implements Callbacks
{
17 /** @var (string|string[])[] $_GET/$_POST data */
20 /** @var (array|UploadedFile)[] $_FILES data or UploadedFile instances */
23 /** @var array Any recorded conditions */
24 private $conditions = [];
27 * @param (string|string[])[] $params Data from $_POST + $_GET
28 * @param array[] $files Data from $_FILES
30 public function __construct( array $params, array $files = [] ) {
31 $this->params
= $params;
32 $this->files
= $files;
35 public function hasParam( $name, array $options ) {
36 return isset( $this->params
[$name] );
39 public function getValue( $name, $default, array $options ) {
40 return $this->params
[$name] ??
$default;
43 public function hasUpload( $name, array $options ) {
44 return isset( $this->files
[$name] );
47 public function getUploadedFile( $name, array $options ) {
48 $file = $this->files
[$name] ??
null;
49 if ( $file && !$file instanceof UploadedFile
) {
50 $file = new UploadedFile( $file );
51 $this->files
[$name] = $file;
56 public function recordCondition( ValidationException
$condition, array $options ) {
57 $this->conditions
[] = $condition;
61 * Fetch any recorded conditions
64 public function getRecordedConditions() {
65 return $this->conditions
;
69 * Clear any recorded conditions
71 public function clearRecordedConditions() {
72 $this->conditions
= [];
75 public function useHighLimits( array $options ) {
76 return !empty( $options['useHighLimits'] );