1 | <?php |
||
15 | class Response { |
||
16 | use |
||
17 | Singleton, |
||
18 | Psr7; |
||
19 | /** |
||
20 | * Protocol, for instance: `HTTP/1.0`, `HTTP/1.1` (default), HTTP/2.0 |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | public $protocol; |
||
25 | /** |
||
26 | * HTTP status code |
||
27 | * |
||
28 | * @var int |
||
29 | */ |
||
30 | public $code; |
||
31 | /** |
||
32 | * Headers are normalized to lowercase keys with hyphen as separator, for instance: `connection`, `referer`, `content-type`, `accept-language` |
||
33 | * |
||
34 | * Values might be strings in case of single value or array of strings in case of multiple values with the same field name |
||
35 | * |
||
36 | * @var string[][] |
||
37 | */ |
||
38 | public $headers; |
||
39 | /** |
||
40 | * String body (is used instead of `$this->body_stream` in most cases, ignored if `$this->body_stream` is present) |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | public $body; |
||
45 | /** |
||
46 | * Body in form of stream (might be used instead of `$this->body` in some cases, if present, `$this->body` is ignored) |
||
47 | * |
||
48 | * Stream is read/write |
||
49 | * |
||
50 | * @var resource |
||
51 | */ |
||
52 | public $body_stream; |
||
53 | /** |
||
54 | * Initialize response object with specified data |
||
55 | * |
||
56 | * @param string $body |
||
57 | * @param null|resource|string $body_stream String, like `php://temp` or resource, like `fopen('php://temp', 'a+b')`, if present, `$body` is ignored |
||
58 | * @param string[]|string[][] $headers Headers are normalized to lowercase keys with hyphen as separator, for instance: `connection`, `referer`, |
||
59 | * `content-type`, `accept-language`; Values might be strings in case of single value or array of strings in case |
||
60 | * of multiple values with the same field name |
||
61 | * @param int $code HTTP status code |
||
62 | * @param string $protocol Protocol, for instance: `HTTP/1.0`, `HTTP/1.1` (default), HTTP/2.0 |
||
63 | * |
||
64 | * @return Response |
||
65 | */ |
||
66 | 10 | function init ($body = '', $body_stream = null, $headers = [], $code = 200, $protocol = 'HTTP/1.1') { |
|
77 | /** |
||
78 | * Initialize with typical default settings (headers `Content-Type`, `Vary` and `X-UA-Compatible`, protocol taken from `cs\Request::$protocol`) |
||
79 | * |
||
80 | * @return Response |
||
81 | */ |
||
82 | 10 | function init_with_typical_default_settings () { |
|
95 | /** |
||
96 | * Set raw HTTP header |
||
97 | * |
||
98 | * @param string $field Field |
||
99 | * @param string $value Value, empty string will cause header removal |
||
100 | * @param bool $replace The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header |
||
101 | * of the same type. By default it will replace |
||
102 | * |
||
103 | * @return Response |
||
104 | */ |
||
105 | 58 | function header ($field, $value, $replace = true) { |
|
116 | /** |
||
117 | * Make redirect to specified location |
||
118 | * |
||
119 | * @param string $location |
||
120 | * @param int $code |
||
121 | * |
||
122 | * @return Response |
||
123 | */ |
||
124 | 2 | function redirect ($location, $code = 302) { |
|
130 | /** |
||
131 | * Function for setting cookies, taking into account cookies prefix. Parameters like in system `setcookie()` function, but `$path`, `$domain` and `$secure` |
||
132 | * are skipped, they are detected automatically |
||
133 | * |
||
134 | * This function have side effect of setting cookie on `cs\Request` object |
||
135 | * |
||
136 | * @param string $name |
||
137 | * @param string $value |
||
138 | * @param int $expire Unix timestamp in seconds |
||
139 | * @param bool $httponly |
||
140 | * |
||
141 | * @return Response |
||
142 | */ |
||
143 | 24 | function cookie ($name, $value, $expire = 0, $httponly = false) { |
|
178 | /** |
||
179 | * Provides default output for all the response data using `header()`, `http_response_code()` and `echo` or `php://output` |
||
180 | */ |
||
181 | 2 | function output_default () { |
|
221 | } |
||
222 |