Completed
Pull Request — master (#38)
by Sam
03:09
created

MultipartRequest::setMultipartParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mediawiki\Api;
4
5
use Exception;
6
7
/**
8
 * A MultipartRequest is the same as a FluentRequest with additional support for setting request
9
 * parameters (both normal parameters and headers) on multipart requests.
10
 *
11
 * @link http://docs.guzzlephp.org/en/stable/request-options.html#multipart
12
 */
13
class MultipartRequest extends FluentRequest {
14
15
	/** @var mixed[] */
16
	protected $multipartParams = [];
17
18
	/**
19
	 * Check the structure of a multipart parameter array.
20
	 *
21
	 * @param mixed[] $params The multipart parameters to check.
22
	 *
23
	 * @throws Exception
24
	 */
25
	protected function checkMultipartParams( $params ) {
26
		foreach ( $params as $key => $val ) {
27
			if ( !is_array( $val ) ) {
28
				throw new Exception( "Parameter '$key' must be an array." );
29
			}
30
			if ( !in_array( $key, $this->getParams() ) ) {
31
				throw new Exception( "Parameter '$key' is not already set on this request." );
32
			}
33
		}
34
	}
35
36
	/**
37
	 * Set all multipart parameters, replacing all existing ones.
38
	 *
39
	 * @param $params
40
	 * @return $this
41
	 */
42
	public function setMultipartParams( $params ) {
43
		$this->checkMultipartParams( $params );
44
		$this->multipartParams = $params;
45
		return $this;
46
	}
47
48
	/**
49
	 * Add extra multipart parameters.
50
	 *
51
	 * @param mixed[] $params
52
	 *
53
	 * @return $this
54
	 */
55
	public function addMultipartParams( $params ) {
56
		$this->checkMultipartParams( $params );
57
		$this->multipartParams = array_merge( $this->multipartParams, $params );
58
		return $this;
59
	}
60
61
	/**
62
	 * Get all multipart request parameters.
63
	 *
64
	 * @return mixed[]
65
	 */
66
	public function getMultipartParams() {
67
		return $this->multipartParams;
68
	}
69
}
70