1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Mvc5\Response\Config; |
7
|
|
|
|
8
|
|
|
use Mvc5\Arg; |
9
|
|
|
use Mvc5\Cookie\Cookies; |
10
|
|
|
use Mvc5\Cookie\HttpCookies; |
11
|
|
|
use Mvc5\Http\Headers; |
12
|
|
|
use Mvc5\Http\HttpHeaders; |
13
|
|
|
use Mvc5\Response\Response; |
14
|
|
|
|
15
|
|
|
trait HttpResponse |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
use \Mvc5\Http\Config\Response; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $config = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed $body |
29
|
|
|
* @param int|null $status |
30
|
|
|
* @param array|Headers $headers |
31
|
|
|
* @param array $config |
32
|
|
|
*/ |
33
|
25 |
|
function __construct($body = null, int $status = null, $headers = [], array $config = []) |
34
|
|
|
{ |
35
|
25 |
|
!isset($config[Arg::COOKIES]) && |
36
|
23 |
|
$config[Arg::COOKIES] = new HttpCookies; |
37
|
|
|
|
38
|
25 |
|
!($config[Arg::COOKIES] instanceof Cookies) && |
39
|
2 |
|
$config[Arg::COOKIES] = new HttpCookies($config[Arg::COOKIES]); |
40
|
|
|
|
41
|
25 |
|
$config[Arg::HEADERS] = $headers instanceof Headers ? $headers : new HttpHeaders($headers); |
42
|
25 |
|
$config[Arg::STATUS] = $status; |
43
|
25 |
|
$config[Arg::BODY] = $body; |
44
|
|
|
|
45
|
25 |
|
$this->config = $config; |
46
|
25 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Cookies |
50
|
|
|
*/ |
51
|
4 |
|
function cookies() : Cookies |
|
|
|
|
52
|
|
|
{ |
53
|
4 |
|
return $this[Arg::COOKIES]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array|string $name |
58
|
|
|
* @return array|string |
59
|
|
|
*/ |
60
|
|
|
function header($name) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if (is_string($name)) { |
63
|
|
|
return implode(', ', (array) ($this->headers()[$name] ?? '')); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$matched = []; |
67
|
|
|
|
68
|
|
|
foreach($name as $key) { |
69
|
1 |
|
$matched[$key] = implode(', ', (array) ($this->headers()[$key] ?? '')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $matched; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
2 |
|
* @param string $name |
77
|
|
|
* @param string|null $value |
78
|
2 |
|
* @param int|null $expire |
79
|
|
|
* @param string|null $path |
80
|
|
|
* @param string|null $domain |
81
|
|
|
* @param bool|null $secure |
82
|
|
|
* @param bool|null $httponly |
83
|
|
|
* @return self|mixed |
84
|
|
|
*/ |
85
|
|
|
function withCookie($name, $value = null, $expire = null, |
|
|
|
|
86
|
1 |
|
string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : Response |
87
|
|
|
{ |
88
|
1 |
|
return $this->withCookies($this->cookies()->with($name, $value, $expire, $path, $domain, $secure, $httponly)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array|Cookies $cookies |
93
|
|
|
* @return self|mixed |
94
|
|
|
*/ |
95
|
1 |
|
function withCookies($cookies) : Response |
|
|
|
|
96
|
|
|
{ |
97
|
1 |
|
return $this->with(Arg::COOKIES, $cookies instanceof Cookies ? $cookies : new HttpCookies($cookies)); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $name |
102
|
|
|
* @param string $value |
103
|
|
|
* @return self|mixed |
104
|
|
|
*/ |
105
|
1 |
|
function withHeader($name, $value) : Response |
|
|
|
|
106
|
|
|
{ |
107
|
1 |
|
return $this->with(Arg::HEADERS, $this->headers()->with((string) $name, $value)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array|Headers $headers |
112
|
|
|
* @return self|mixed |
113
|
|
|
*/ |
114
|
1 |
|
function withHeaders($headers) : Response |
|
|
|
|
115
|
|
|
{ |
116
|
1 |
|
return $this->with(Arg::HEADERS, $headers instanceof Headers ? $headers : new HttpHeaders($headers)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param int $status |
121
|
|
|
* @param string $reason |
122
|
|
|
* @return self|mixed |
123
|
|
|
*/ |
124
|
|
|
function withStatus($status, $reason = '') : Response |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
return $this->with([Arg::STATUS => (int) $status, Arg::REASON => (string) $reason]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $version |
131
|
|
|
* @return self|mixed |
132
|
|
|
*/ |
133
|
|
|
function withVersion(string $version) : Response |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
return $this->with(Arg::VERSION, $version); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.