aboutsummaryrefslogtreecommitdiffstats
path: root/includes/Rest/Response.php
blob: 3b0102824609c50eea8412b5de59e686191fc7bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace MediaWiki\Rest;

use HttpStatus;
use Psr\Http\Message\StreamInterface;

class Response implements ResponseInterface {
	/** @var int */
	private $statusCode = 200;

	/** @var string */
	private $reasonPhrase = 'OK';

	/** @var string */
	private $protocolVersion = '1.1';

	/** @var StreamInterface */
	private $body;

	/** @var HeaderContainer */
	private $headerContainer;

	/** @var array */
	private $cookies = [];

	/**
	 * @internal Use ResponseFactory
	 * @param string $bodyContents
	 */
	public function __construct( $bodyContents = '' ) {
		$this->body = new StringStream( $bodyContents );
		$this->headerContainer = new HeaderContainer;
	}

	public function getStatusCode() {
		return $this->statusCode;
	}

	public function getReasonPhrase() {
		return $this->reasonPhrase;
	}

	public function setStatus( $code, $reasonPhrase = '' ) {
		$this->statusCode = $code;
		if ( $reasonPhrase === '' ) {
			$reasonPhrase = HttpStatus::getMessage( $code ) ?? '';
		}
		$this->reasonPhrase = $reasonPhrase;
	}

	public function getProtocolVersion() {
		return $this->protocolVersion;
	}

	public function getHeaders() {
		return $this->headerContainer->getHeaders();
	}

	public function hasHeader( $name ) {
		return $this->headerContainer->hasHeader( $name );
	}

	public function getHeader( $name ) {
		return $this->headerContainer->getHeader( $name );
	}

	public function getHeaderLine( $name ) {
		return $this->headerContainer->getHeaderLine( $name );
	}

	public function getBody() {
		return $this->body;
	}

	public function setProtocolVersion( $version ) {
		$this->protocolVersion = $version;
	}

	public function setHeader( $name, $value ) {
		$this->headerContainer->setHeader( $name, $value );
	}

	public function addHeader( $name, $value ) {
		$this->headerContainer->addHeader( $name, $value );
	}

	public function removeHeader( $name ) {
		$this->headerContainer->removeHeader( $name );
	}

	public function setBody( StreamInterface $body ) {
		$this->body = $body;
	}

	public function getRawHeaderLines() {
		return $this->headerContainer->getRawHeaderLines();
	}

	public function setCookie( $name, $value, $expire = 0, $options = [] ) {
		$this->cookies[] = [
			'name' => $name,
			'value' => $value,
			'expire' => $expire,
			'options' => $options
		];
	}

	public function getCookies() {
		return $this->cookies;
	}
}