ParamValidator: Flag as unstable for 1.34
[lhc/web/wiklou.git] / includes / libs / ParamValidator / SimpleCallbacks.php
1 <?php
2
3 namespace Wikimedia\ParamValidator;
4
5 use Wikimedia\ParamValidator\Util\UploadedFile;
6
7 /**
8 * Simple Callbacks implementation for $_GET/$_POST/$_FILES data
9 *
10 * Options array keys used by this class:
11 * - 'useHighLimits': (bool) Return value from useHighLimits()
12 *
13 * @since 1.34
14 * @unstable
15 */
16 class SimpleCallbacks implements Callbacks {
17
18 /** @var (string|string[])[] $_GET/$_POST data */
19 private $params;
20
21 /** @var (array|UploadedFile)[] $_FILES data or UploadedFile instances */
22 private $files;
23
24 /** @var array Any recorded conditions */
25 private $conditions = [];
26
27 /**
28 * @param (string|string[])[] $params Data from $_POST + $_GET
29 * @param array[] $files Data from $_FILES
30 */
31 public function __construct( array $params, array $files = [] ) {
32 $this->params = $params;
33 $this->files = $files;
34 }
35
36 public function hasParam( $name, array $options ) {
37 return isset( $this->params[$name] );
38 }
39
40 public function getValue( $name, $default, array $options ) {
41 return $this->params[$name] ?? $default;
42 }
43
44 public function hasUpload( $name, array $options ) {
45 return isset( $this->files[$name] );
46 }
47
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;
53 }
54 return $file;
55 }
56
57 public function recordCondition( ValidationException $condition, array $options ) {
58 $this->conditions[] = $condition;
59 }
60
61 /**
62 * Fetch any recorded conditions
63 * @return array[]
64 */
65 public function getRecordedConditions() {
66 return $this->conditions;
67 }
68
69 /**
70 * Clear any recorded conditions
71 */
72 public function clearRecordedConditions() {
73 $this->conditions = [];
74 }
75
76 public function useHighLimits( array $options ) {
77 return !empty( $options['useHighLimits'] );
78 }
79
80 }