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
|
|
|
* Builds a http response with |
29
|
|
|
* default or configured dependencies. |
30
|
|
|
*/ |
31
|
|
|
class HttpResponseBuilder { |
32
|
|
|
|
33
|
|
|
/** @var \Brickoo\Component\Http\HttpVersion */ |
34
|
|
|
private $httpVersion; |
35
|
|
|
|
36
|
|
|
/** @var \Brickoo\Component\Http\HttpStatus */ |
37
|
|
|
private $httpStatus; |
38
|
|
|
|
39
|
|
|
/** @var \Brickoo\Component\Http\HttpMessage */ |
40
|
|
|
private $httpMessage; |
41
|
|
|
|
42
|
|
|
/** @var \Brickoo\Component\Http\HttpMessageHeader */ |
43
|
|
|
private $httpMessageHeader; |
44
|
|
|
|
45
|
|
|
/** @var \Brickoo\Component\Http\HttpMessageBody */ |
46
|
|
|
private $httpMessageBody; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Build the configured http response. |
50
|
|
|
* @return \Brickoo\Component\Http\HttpResponse |
51
|
|
|
*/ |
52
|
1 |
|
public function build() { |
53
|
1 |
|
return new HttpResponse( |
54
|
1 |
|
$this->getHttpVersion(), |
55
|
1 |
|
$this->getHttpStatus(), |
56
|
1 |
|
$this->getHttpMessage() |
57
|
1 |
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set the http version dependency. |
62
|
|
|
* @param HttpVersion|null $httpVersion |
63
|
|
|
* @return \Brickoo\Component\Http\HttpResponseBuilder |
64
|
|
|
*/ |
65
|
1 |
|
public function setHttpVersion(HttpVersion $httpVersion = null) { |
66
|
1 |
|
$this->httpVersion = $httpVersion; |
67
|
1 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return the http version dependency. |
72
|
|
|
* @return HttpVersion |
73
|
|
|
*/ |
74
|
1 |
|
public function getHttpVersion() { |
75
|
1 |
|
if (!$this->httpVersion instanceof HttpVersion) { |
76
|
1 |
|
$this->httpVersion = new HttpVersion(HttpVersion::HTTP_1_1); |
77
|
1 |
|
} |
78
|
1 |
|
return $this->httpVersion; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the http status dependency. |
83
|
|
|
* @param HttpStatus|null $httpStatus |
84
|
|
|
* @return \Brickoo\Component\Http\HttpResponseBuilder |
85
|
|
|
*/ |
86
|
1 |
|
public function setHttpStatus(HttpStatus $httpStatus = null) { |
87
|
1 |
|
$this->httpStatus = $httpStatus; |
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return the http status dependency. |
93
|
|
|
* @return HttpStatus |
94
|
|
|
*/ |
95
|
1 |
|
public function getHttpStatus() { |
96
|
1 |
|
if (!$this->httpStatus instanceof HttpStatus) { |
97
|
1 |
|
$this->httpStatus = new HttpStatus(HttpStatus::CODE_OK); |
98
|
1 |
|
} |
99
|
1 |
|
return $this->httpStatus; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return the http message header dependency. |
104
|
|
|
* @return HttpMessageHeader |
105
|
|
|
*/ |
106
|
1 |
|
public function getHttpMessageHeader() { |
107
|
1 |
|
if ($this->httpMessageHeader === null) { |
108
|
1 |
|
$this->httpMessageHeader = new HttpMessageHeader(); |
109
|
1 |
|
} |
110
|
1 |
|
return $this->httpMessageHeader; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Add a http header to the response message. |
115
|
|
|
* @param HttpHeaderField $httpHeader |
116
|
|
|
* @return \Brickoo\Component\Http\HttpResponseBuilder |
117
|
|
|
*/ |
118
|
1 |
|
public function addHttpHeader(HttpHeaderField $httpHeader) { |
119
|
1 |
|
$this->getHttpMessageHeader()->addField($httpHeader); |
120
|
1 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set the http message header dependency. |
125
|
|
|
* @param HttpMessageHeader|null $httpMessageHeader |
126
|
|
|
* @return \Brickoo\Component\Http\HttpResponseBuilder |
127
|
|
|
*/ |
128
|
1 |
|
public function setHttpMessageHeader(HttpMessageHeader $httpMessageHeader = null) { |
129
|
1 |
|
$this->httpMessageHeader = $httpMessageHeader; |
130
|
1 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return the http message body dependency. |
135
|
|
|
* @return HttpMessageBody |
136
|
|
|
*/ |
137
|
1 |
|
public function getHttpMessageBody() { |
138
|
1 |
|
if ($this->httpMessageBody === null) { |
139
|
1 |
|
$this->httpMessageBody = new HttpMessageBody(); |
140
|
1 |
|
} |
141
|
1 |
|
return $this->httpMessageBody; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return the http message dependency. |
146
|
|
|
* @return HttpMessage |
147
|
|
|
*/ |
148
|
1 |
|
public function getHttpMessage() { |
149
|
1 |
|
if (!$this->httpMessage instanceof HttpMessage) { |
150
|
1 |
|
$this->httpMessage = new HttpMessage( |
151
|
1 |
|
$this->getHttpMessageHeader(), |
152
|
1 |
|
$this->getHttpMessageBody() |
153
|
1 |
|
); |
154
|
1 |
|
} |
155
|
1 |
|
return $this->httpMessage; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set the response http message dependency. |
160
|
|
|
* @param HttpMessage|null $httpMessage |
161
|
|
|
* @return \Brickoo\Component\Http\HttpResponseBuilder |
162
|
|
|
*/ |
163
|
1 |
|
public function setHttpMessage(HttpMessage $httpMessage = null) { |
164
|
1 |
|
$this->httpMessage = $httpMessage; |
165
|
1 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|