File: SimpleCallbacks.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (89 lines) | stat: -rw-r--r-- 2,100 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?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;
		}
		// @phan-suppress-next-line PhanTypeMismatchReturnNullable False positive
		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'] );
	}

}