| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest; |
| 4 |
|
| 5 |
use HttpStatus; |
| 6 |
use Psr\Http\Message\StreamInterface; |
| 7 |
|
| 8 |
class Response implements ResponseInterface { |
| 9 |
|
| 10 |
private int $statusCode = 200; |
| 11 |
private string $reasonPhrase = 'OK'; |
| 12 |
private string $protocolVersion = '1.1'; |
| 13 |
private StreamInterface $body; |
| 14 |
private HeaderContainer $headerContainer; |
| 15 |
private array $cookies = []; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public function __construct( $body = '' ) { |
| 23 |
if ( is_string( $body ) ) { |
| 24 |
$body = new StringStream( $body ); |
| 25 |
} |
| 26 |
|
| 27 |
$this->body = $body; |
| 28 |
$this->headerContainer = new HeaderContainer; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public static function cast( ResponseInterface $iResponse ): Response { |
| 35 |
if ( $iResponse instanceof Response ) { |
| 36 |
return $iResponse; |
| 37 |
} |
| 38 |
|
| 39 |
$resp = new Response( |
| 40 |
$iResponse->getBody() |
| 41 |
); |
| 42 |
|
| 43 |
foreach ( $iResponse->getHeaders() as $name => $values ) { |
| 44 |
$resp->setHeader( $name, $values ); |
| 45 |
} |
| 46 |
|
| 47 |
return $resp; |
| 48 |
} |
| 49 |
|
| 50 |
public function getStatusCode() { |
| 51 |
return $this->statusCode; |
| 52 |
} |
| 53 |
|
| 54 |
public function getReasonPhrase() { |
| 55 |
return $this->reasonPhrase; |
| 56 |
} |
| 57 |
|
| 58 |
public function setStatus( $code, $reasonPhrase = '' ) { |
| 59 |
$this->statusCode = $code; |
| 60 |
if ( $reasonPhrase === '' ) { |
| 61 |
$reasonPhrase = HttpStatus::getMessage( $code ) ?? ''; |
| 62 |
} |
| 63 |
$this->reasonPhrase = $reasonPhrase; |
| 64 |
} |
| 65 |
|
| 66 |
public function getProtocolVersion() { |
| 67 |
return $this->protocolVersion; |
| 68 |
} |
| 69 |
|
| 70 |
public function getHeaders() { |
| 71 |
return $this->headerContainer->getHeaders(); |
| 72 |
} |
| 73 |
|
| 74 |
public function hasHeader( $name ) { |
| 75 |
return $this->headerContainer->hasHeader( $name ); |
| 76 |
} |
| 77 |
|
| 78 |
public function getHeader( $name ) { |
| 79 |
return $this->headerContainer->getHeader( $name ); |
| 80 |
} |
| 81 |
|
| 82 |
public function getHeaderLine( $name ) { |
| 83 |
return $this->headerContainer->getHeaderLine( $name ); |
| 84 |
} |
| 85 |
|
| 86 |
public function getBody() { |
| 87 |
return $this->body; |
| 88 |
} |
| 89 |
|
| 90 |
public function setProtocolVersion( $version ) { |
| 91 |
$this->protocolVersion = $version; |
| 92 |
} |
| 93 |
|
| 94 |
public function setHeader( $name, $value ) { |
| 95 |
$this->headerContainer->setHeader( $name, $value ); |
| 96 |
} |
| 97 |
|
| 98 |
public function addHeader( $name, $value ) { |
| 99 |
$this->headerContainer->addHeader( $name, $value ); |
| 100 |
} |
| 101 |
|
| 102 |
public function removeHeader( $name ) { |
| 103 |
$this->headerContainer->removeHeader( $name ); |
| 104 |
} |
| 105 |
|
| 106 |
public function setBody( StreamInterface $body ) { |
| 107 |
$this->body = $body; |
| 108 |
} |
| 109 |
|
| 110 |
public function getRawHeaderLines() { |
| 111 |
return $this->headerContainer->getRawHeaderLines(); |
| 112 |
} |
| 113 |
|
| 114 |
public function setCookie( $name, $value, $expire = 0, $options = [] ) { |
| 115 |
$this->cookies[] = [ |
| 116 |
'name' => $name, |
| 117 |
'value' => $value, |
| 118 |
'expire' => $expire, |
| 119 |
'options' => $options |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
public function getCookies() { |
| 124 |
return $this->cookies; |
| 125 |
} |
| 126 |
} |