|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Http; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* HttpResponse |
|
29
|
|
|
* |
|
30
|
|
|
* Implements a http response. |
|
31
|
|
|
* @author Celestino Diaz <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class HttpResponse { |
|
34
|
|
|
|
|
35
|
|
|
/** @var \Brickoo\Component\Http\HttpStatus */ |
|
36
|
|
|
protected $status; |
|
37
|
|
|
|
|
38
|
|
|
/** @var \Brickoo\Component\Http\HttpVersion */ |
|
39
|
|
|
protected $version; |
|
40
|
|
|
|
|
41
|
|
|
/** @var \Brickoo\Component\Http\HttpMessage */ |
|
42
|
|
|
protected $message; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Class constructor. |
|
46
|
|
|
* @param \Brickoo\Component\Http\HttpVersion $version |
|
47
|
|
|
* @param \Brickoo\Component\Http\HttpStatus $status |
|
48
|
|
|
* @param \Brickoo\Component\Http\HttpMessage $message |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function __construct(HttpVersion $version, HttpStatus $status, HttpMessage $message) { |
|
51
|
1 |
|
$this->version = $version; |
|
52
|
1 |
|
$this->status = $status; |
|
53
|
1 |
|
$this->message = $message; |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the response status. |
|
58
|
|
|
* @return \Brickoo\Component\Http\HttpStatus |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function getStatus() { |
|
61
|
1 |
|
return $this->status; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns the response http version. |
|
66
|
|
|
* @return \Brickoo\Component\Http\HttpVersion |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getVersion() { |
|
69
|
1 |
|
return $this->version; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the response message. |
|
74
|
|
|
* @return \Brickoo\Component\Http\HttpMessage |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getMessage() { |
|
77
|
1 |
|
return $this->message; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the response message header. |
|
82
|
|
|
* @return \Brickoo\Component\Http\HttpMessageHeader |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function getHeader() { |
|
85
|
1 |
|
return $this->message->getHeader(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the response message body. |
|
90
|
|
|
* @return \Brickoo\Component\Http\HttpMessageBody |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getBody() { |
|
93
|
1 |
|
return $this->message->getBody(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Inject the dependencies from an other response. |
|
98
|
|
|
* @param HttpResponse $httpResponse |
|
99
|
|
|
* @return \Brickoo\Component\Http\HttpResponse |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function inject(HttpResponse $httpResponse) { |
|
102
|
1 |
|
$this->version = $httpResponse->getVersion(); |
|
103
|
1 |
|
$this->status = $httpResponse->getStatus(); |
|
104
|
1 |
|
$this->message = $httpResponse->getMessage(); |
|
105
|
1 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns a string response representation. |
|
110
|
|
|
* @return string the response representation |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function toString() { |
|
113
|
1 |
|
$response = sprintf("%s %s\r\n", $this->getVersion()->toString(), $this->getStatus()->toString()); |
|
114
|
1 |
|
$response .= $this->getHeader()->toString(); |
|
115
|
|
|
|
|
116
|
1 |
|
$statusCode = $this->getStatus()->getCode(); |
|
117
|
1 |
|
if (($statusCode > 199) && ($statusCode != 204) && ($statusCode != 304)) { |
|
118
|
1 |
|
$response .= "\r\n".$this->getBody()->getContent(); |
|
119
|
1 |
|
} |
|
120
|
1 |
|
return $response; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|