Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / Rest / Response.php
1 <?php
2
3 namespace MediaWiki\Rest;
4
5 use HttpStatus;
6 use Psr\Http\Message\StreamInterface;
7
8 class Response implements ResponseInterface {
9 /** @var int */
10 private $statusCode = 200;
11
12 /** @var string */
13 private $reasonPhrase = 'OK';
14
15 /** @var string */
16 private $protocolVersion = '1.1';
17
18 /** @var StreamInterface */
19 private $body;
20
21 /** @var HeaderContainer */
22 private $headerContainer;
23
24 /** @var array */
25 private $cookies = [];
26
27 /**
28 * @internal Use ResponseFactory
29 * @param string $bodyContents
30 */
31 public function __construct( $bodyContents = '' ) {
32 $this->body = new StringStream( $bodyContents );
33 $this->headerContainer = new HeaderContainer;
34 }
35
36 public function getStatusCode() {
37 return $this->statusCode;
38 }
39
40 public function getReasonPhrase() {
41 return $this->reasonPhrase;
42 }
43
44 public function setStatus( $code, $reasonPhrase = '' ) {
45 $this->statusCode = $code;
46 if ( $reasonPhrase === '' ) {
47 $reasonPhrase = HttpStatus::getMessage( $code ) ?? '';
48 }
49 $this->reasonPhrase = $reasonPhrase;
50 }
51
52 public function getProtocolVersion() {
53 return $this->protocolVersion;
54 }
55
56 public function getHeaders() {
57 return $this->headerContainer->getHeaders();
58 }
59
60 public function hasHeader( $name ) {
61 return $this->headerContainer->hasHeader( $name );
62 }
63
64 public function getHeader( $name ) {
65 return $this->headerContainer->getHeader( $name );
66 }
67
68 public function getHeaderLine( $name ) {
69 return $this->headerContainer->getHeaderLine( $name );
70 }
71
72 public function getBody() {
73 return $this->body;
74 }
75
76 public function setProtocolVersion( $version ) {
77 $this->protocolVersion = $version;
78 }
79
80 public function setHeader( $name, $value ) {
81 $this->headerContainer->setHeader( $name, $value );
82 }
83
84 public function addHeader( $name, $value ) {
85 $this->headerContainer->addHeader( $name, $value );
86 }
87
88 public function removeHeader( $name ) {
89 $this->headerContainer->removeHeader( $name );
90 }
91
92 public function setBody( StreamInterface $body ) {
93 $this->body = $body;
94 }
95
96 public function getRawHeaderLines() {
97 return $this->headerContainer->getRawHeaderLines();
98 }
99
100 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
101 $this->cookies[] = [
102 'name' => $name,
103 'value' => $value,
104 'expire' => $expire,
105 'options' => $options
106 ];
107 }
108
109 public function getCookies() {
110 return $this->cookies;
111 }
112 }