Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.32% |
28 / 31 |
|
72.73% |
8 / 11 |
CRAP | |
0.00% |
0 / 1 |
RequestData | |
90.32% |
28 / 31 |
|
72.73% |
8 / 11 |
15.20 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
getMethod | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUri | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getProtocolVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getServerParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCookieParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getQueryParams | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUploadedFiles | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPostParams | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasBody | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
5.03 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Rest; |
4 | |
5 | use GuzzleHttp\Psr7\Uri; |
6 | use Psr\Http\Message\StreamInterface; |
7 | use Psr\Http\Message\UploadedFileInterface; |
8 | use Psr\Http\Message\UriInterface; |
9 | |
10 | /** |
11 | * This is a Request class that allows data to be injected, for the purposes |
12 | * of testing or internal requests. |
13 | */ |
14 | class RequestData extends RequestBase { |
15 | /** @var string */ |
16 | private $method; |
17 | |
18 | /** @var UriInterface */ |
19 | private $uri; |
20 | |
21 | /** @var string */ |
22 | private $protocolVersion; |
23 | |
24 | /** @var StreamInterface */ |
25 | private $body; |
26 | |
27 | /** @var array */ |
28 | private $serverParams; |
29 | |
30 | /** @var array */ |
31 | private $cookieParams; |
32 | |
33 | /** @var array */ |
34 | private $queryParams; |
35 | |
36 | /** @var UploadedFileInterface[] */ |
37 | private $uploadedFiles; |
38 | |
39 | /** @var array */ |
40 | private $postParams; |
41 | |
42 | /** |
43 | * Construct a RequestData from an array of parameters. |
44 | * |
45 | * @param array $params An associative array of parameters. All parameters |
46 | * have defaults. Parameters are: |
47 | * - method: The HTTP method |
48 | * - uri: The URI |
49 | * - protocolVersion: The HTTP protocol version number |
50 | * - bodyContents: A string giving the request body |
51 | * - serverParams: Equivalent to $_SERVER |
52 | * - cookieParams: Equivalent to $_COOKIE |
53 | * - queryParams: Equivalent to $_GET |
54 | * - uploadedFiles: An array of objects implementing UploadedFileInterface |
55 | * - postParams: Equivalent to $_POST |
56 | * - pathParams: The path template parameters |
57 | * - headers: An array with the key being the header name |
58 | * - cookiePrefix: A prefix to add to cookie names in getCookie() |
59 | */ |
60 | public function __construct( $params = [] ) { |
61 | $this->method = $params['method'] ?? 'GET'; |
62 | $this->uri = $params['uri'] ?? new Uri; |
63 | $this->protocolVersion = $params['protocolVersion'] ?? '1.1'; |
64 | $this->body = new StringStream( $params['bodyContents'] ?? '' ); |
65 | $this->serverParams = $params['serverParams'] ?? []; |
66 | $this->cookieParams = $params['cookieParams'] ?? []; |
67 | $this->queryParams = $params['queryParams'] ?? []; |
68 | $this->uploadedFiles = $params['uploadedFiles'] ?? []; |
69 | $this->postParams = $params['postParams'] ?? []; |
70 | $this->setPathParams( $params['pathParams'] ?? [] ); |
71 | $this->setHeaders( $params['headers'] ?? [] ); |
72 | $this->setParsedBody( $params['parsedBody'] ?? null ); |
73 | parent::__construct( $params['cookiePrefix'] ?? '' ); |
74 | } |
75 | |
76 | public function getMethod() { |
77 | return $this->method; |
78 | } |
79 | |
80 | public function getUri() { |
81 | return $this->uri; |
82 | } |
83 | |
84 | public function getProtocolVersion() { |
85 | return $this->protocolVersion; |
86 | } |
87 | |
88 | public function getBody() { |
89 | return $this->body; |
90 | } |
91 | |
92 | public function getServerParams() { |
93 | return $this->serverParams; |
94 | } |
95 | |
96 | public function getCookieParams() { |
97 | return $this->cookieParams; |
98 | } |
99 | |
100 | public function getQueryParams() { |
101 | return $this->queryParams; |
102 | } |
103 | |
104 | public function getUploadedFiles() { |
105 | return $this->uploadedFiles; |
106 | } |
107 | |
108 | public function getPostParams() { |
109 | return $this->postParams; |
110 | } |
111 | |
112 | public function hasBody(): bool { |
113 | if ( parent::hasBody() ) { |
114 | return true; |
115 | } |
116 | |
117 | if ( $this->parsedBody !== null ) { |
118 | return true; |
119 | } |
120 | |
121 | if ( $this->postParams !== [] ) { |
122 | return true; |
123 | } |
124 | |
125 | if ( $this->getBody()->getSize() > 0 ) { |
126 | return true; |
127 | } |
128 | |
129 | return false; |
130 | } |
131 | |
132 | } |