1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nymfonya\Component\Http; |
6
|
|
|
|
7
|
|
|
use Nymfonya\Component\Http\Interfaces\ResponseInterface; |
8
|
|
|
use Nymfonya\Component\Http\Interfaces\HeadersInterface; |
9
|
|
|
use Nymfonya\Component\Http\Headers; |
10
|
|
|
|
11
|
|
|
class Response implements ResponseInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* response content |
16
|
|
|
* |
17
|
|
|
* @var mixed |
18
|
|
|
*/ |
19
|
|
|
protected $content; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* http status code |
23
|
|
|
* |
24
|
|
|
* @var Integer |
25
|
|
|
*/ |
26
|
|
|
protected $code; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* header manager |
30
|
|
|
* |
31
|
|
|
* @var Headers |
32
|
|
|
*/ |
33
|
|
|
protected $headerManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* is cli |
37
|
|
|
* |
38
|
|
|
* @var Boolean |
39
|
|
|
*/ |
40
|
|
|
protected $isCli; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* instanciate |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
9 |
|
public function __construct() |
47
|
|
|
{ |
48
|
9 |
|
$this->headerManager = new Headers(); |
49
|
9 |
|
$this->code = self::HTTP_NOT_FOUND; |
50
|
9 |
|
$this->content = ''; |
51
|
9 |
|
$sapiName = php_sapi_name(); |
52
|
9 |
|
$this->setIsCli($sapiName == self::_CLI || $sapiName == self::_CLID); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* returns header manager |
57
|
|
|
* |
58
|
|
|
* @return HeadersInterface |
59
|
|
|
*/ |
60
|
1 |
|
public function getHeaderManager(): HeadersInterface |
61
|
|
|
{ |
62
|
1 |
|
return $this->headerManager; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* set response content |
67
|
|
|
* |
68
|
|
|
* @param mixed $content |
69
|
|
|
* @return ResponseInterface |
70
|
|
|
*/ |
71
|
1 |
|
public function setContent($content): ResponseInterface |
72
|
|
|
{ |
73
|
1 |
|
$this->content = (is_string($content)) |
74
|
1 |
|
? $content |
75
|
1 |
|
: json_encode($content); |
76
|
1 |
|
$this->headerManager->add( |
77
|
1 |
|
Headers::CONTENT_LENGTH, |
78
|
1 |
|
(string) strlen($this->content) |
79
|
|
|
); |
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* return content string |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
1 |
|
public function getContent(): string |
89
|
|
|
{ |
90
|
1 |
|
return $this->content; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* set http code response |
95
|
|
|
* |
96
|
|
|
* @param integer $code |
97
|
|
|
* @return ResponseInterface |
98
|
|
|
*/ |
99
|
1 |
|
public function setCode(int $code): ResponseInterface |
100
|
|
|
{ |
101
|
1 |
|
$this->code = $code; |
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* return http code response |
107
|
|
|
* |
108
|
|
|
* @return integer |
109
|
|
|
*/ |
110
|
1 |
|
public function getCode(): int |
111
|
|
|
{ |
112
|
1 |
|
return $this->code; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* send response content to output |
117
|
|
|
* |
118
|
|
|
* @return ResponseInterface |
119
|
|
|
*/ |
120
|
1 |
|
public function send(): ResponseInterface |
121
|
|
|
{ |
122
|
1 |
|
if ($this->isCli) { |
123
|
1 |
|
echo $this->content; |
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
1 |
|
$this->headerManager->send(); |
127
|
1 |
|
http_response_code($this->code); |
128
|
1 |
|
echo $this->content; |
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* set true if we are running from cli |
134
|
|
|
* essentially for testing purposes |
135
|
|
|
* |
136
|
|
|
* @param boolean $isCli |
137
|
|
|
* @return ResponseInterface |
138
|
|
|
*/ |
139
|
1 |
|
protected function setIsCli(bool $isCli): ResponseInterface |
140
|
|
|
{ |
141
|
1 |
|
$this->isCli = $isCli; |
142
|
1 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|