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
|
|
|
* @since 2.4.0 |
14
|
|
|
*/ |
15
|
|
|
class MultipartRequest extends FluentRequest { |
16
|
|
|
|
17
|
|
|
/** @var mixed[] */ |
18
|
|
|
protected $multipartParams = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Check the structure of a multipart parameter array. |
22
|
|
|
* |
23
|
|
|
* @param mixed[] $params The multipart parameters to check. |
24
|
|
|
* |
25
|
|
|
* @throws Exception |
26
|
|
|
*/ |
27
|
2 |
|
protected function checkMultipartParams( $params ) { |
28
|
2 |
|
foreach ( $params as $key => $val ) { |
29
|
2 |
|
if ( !is_array( $val ) ) { |
30
|
|
|
throw new Exception( "Parameter '$key' must be an array." ); |
31
|
|
|
} |
32
|
2 |
|
if ( !in_array( $key, array_keys( $this->getParams() ) ) ) { |
33
|
1 |
|
throw new Exception( "Parameter '$key' is not already set on this request." ); |
34
|
|
|
} |
35
|
1 |
|
} |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set all multipart parameters, replacing all existing ones. |
40
|
|
|
* |
41
|
|
|
* Each key of the array passed in here must be the name of a parameter already set on this |
42
|
|
|
* request object. |
43
|
|
|
* |
44
|
|
|
* @param mixed[] $params The multipart parameters to use. |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function setMultipartParams( $params ) { |
48
|
|
|
$this->checkMultipartParams( $params ); |
49
|
|
|
$this->multipartParams = $params; |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add extra multipart parameters. |
55
|
|
|
* |
56
|
|
|
* Each key of the array passed in here must be the name of a parameter already set on this |
57
|
|
|
* request object. |
58
|
|
|
* |
59
|
|
|
* @param mixed[] $params The multipart parameters to add to any already present. |
60
|
|
|
* |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
2 |
|
public function addMultipartParams( $params ) { |
64
|
2 |
|
$this->checkMultipartParams( $params ); |
65
|
1 |
|
$this->multipartParams = array_merge( $this->multipartParams, $params ); |
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all multipart request parameters. |
71
|
|
|
* |
72
|
|
|
* @return mixed[] |
73
|
|
|
*/ |
74
|
1 |
|
public function getMultipartParams() { |
75
|
1 |
|
return $this->multipartParams; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|