Debian Codegraph @

includes/libs/ParamValidator/SimpleCallbacks.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\ParamValidator;
4
5 use Wikimedia\Message\DataMessageValue;
6 use Wikimedia\ParamValidator\Util\UploadedFile;
7
8 /**
9 * Simple Callbacks implementation for $_GET/$_POST/$_FILES data
10 *
11 * Options array keys used by this class:
12 * - 'useHighLimits': (bool) Return value from useHighLimits()
13 *
14 * @since 1.34
15 * @unstable
16 */
17 class SimpleCallbacks implements Callbacks {
18
19 /** @var (string|string[])[] $_GET/$_POST data */
20 private $params;
21
22 /** @var (array|UploadedFile)[] $_FILES data or UploadedFile instances */
23 private $files;
24
25 /** @var array[] Any recorded conditions */
26 private $conditions = [];
27
28 /**
29 * @param (string|string[])[] $params Data from $_POST + $_GET
30 * @param array[] $files Data from $_FILES
31 */
32 public function __construct( array $params, array $files = [] ) {
33 $this->params = $params;
34 $this->files = $files;
35 }
36
37 public function hasParam( $name, array $options ) {
38 return isset( $this->params[$name] );
39 }
40
41 public function getValue( $name, $default, array $options ) {
42 return $this->params[$name] ?? $default;
43 }
44
45 public function hasUpload( $name, array $options ) {
46 return isset( $this->files[$name] );
47 }
48
49 public function getUploadedFile( $name, array $options ) {
50 $file = $this->files[$name] ?? null;
51 if ( $file && !$file instanceof UploadedFile ) {
52 $file = new UploadedFile( $file );
53 $this->files[$name] = $file;
54 }
55 // @phan-suppress-next-line PhanTypeMismatchReturnNullable False positive
56 return $file;
57 }
58
59 public function recordCondition(
60 DataMessageValue $message, $name, $value, array $settings, array $options
61 ) {
62 $this->conditions[] = [
63 'message' => $message,
64 'name' => $name,
65 'value' => $value,
66 'settings' => $settings,
67 ];
68 }
69
70 /**
71 * Fetch any recorded conditions
72 * @return array[]
73 */
74 public function getRecordedConditions() {
75 return $this->conditions;
76 }
77
78 /**
79 * Clear any recorded conditions
80 */
81 public function clearRecordedConditions() {
82 $this->conditions = [];
83 }
84
85 public function useHighLimits( array $options ) {
86 return !empty( $options['useHighLimits'] );
87 }
88
89 }