|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\BrowserKit; |
|
13 |
|
14 /** |
|
15 * Response object. |
|
16 * |
|
17 * @author Fabien Potencier <fabien@symfony.com> |
|
18 * |
|
19 * @api |
|
20 */ |
|
21 class Response |
|
22 { |
|
23 protected $content; |
|
24 protected $status; |
|
25 protected $headers; |
|
26 |
|
27 /** |
|
28 * Constructor. |
|
29 * |
|
30 * The headers array is a set of key/value pairs. If a header is present multiple times |
|
31 * then the value is an array of all the values. |
|
32 * |
|
33 * @param string $content The content of the response |
|
34 * @param integer $status The response status code |
|
35 * @param array $headers An array of headers |
|
36 * |
|
37 * @api |
|
38 */ |
|
39 public function __construct($content = '', $status = 200, array $headers = array()) |
|
40 { |
|
41 $this->content = $content; |
|
42 $this->status = $status; |
|
43 $this->headers = $headers; |
|
44 } |
|
45 |
|
46 /** |
|
47 * Converts the response object to string containing all headers and the response content. |
|
48 * |
|
49 * @return string The response with headers and content |
|
50 */ |
|
51 public function __toString() |
|
52 { |
|
53 $headers = ''; |
|
54 foreach ($this->headers as $name => $value) { |
|
55 if (is_string($value)) { |
|
56 $headers .= $this->buildHeader($name, $value); |
|
57 } else { |
|
58 foreach ($value as $headerValue) { |
|
59 $headers .= $this->buildHeader($name, $headerValue); |
|
60 } |
|
61 } |
|
62 } |
|
63 |
|
64 return $headers."\n".$this->content; |
|
65 } |
|
66 |
|
67 /** |
|
68 * Returns the build header line. |
|
69 * |
|
70 * @param string $name The header name |
|
71 * @param string $value The header value |
|
72 * |
|
73 * @return string The built header line |
|
74 */ |
|
75 protected function buildHeader($name, $value) |
|
76 { |
|
77 return sprintf("%s: %s\n", $name, $value); |
|
78 } |
|
79 |
|
80 /** |
|
81 * Gets the response content. |
|
82 * |
|
83 * @return string The response content |
|
84 * |
|
85 * @api |
|
86 */ |
|
87 public function getContent() |
|
88 { |
|
89 return $this->content; |
|
90 } |
|
91 |
|
92 /** |
|
93 * Gets the response status code. |
|
94 * |
|
95 * @return integer The response status code |
|
96 * |
|
97 * @api |
|
98 */ |
|
99 public function getStatus() |
|
100 { |
|
101 return $this->status; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Gets the response headers. |
|
106 * |
|
107 * @return array The response headers |
|
108 * |
|
109 * @api |
|
110 */ |
|
111 public function getHeaders() |
|
112 { |
|
113 return $this->headers; |
|
114 } |
|
115 |
|
116 /** |
|
117 * Gets a response header. |
|
118 * |
|
119 * @param string $header The header name |
|
120 * @param Boolean $first Whether to return the first value or all header values |
|
121 * |
|
122 * @return string|array The first header value if $first is true, an array of values otherwise |
|
123 */ |
|
124 public function getHeader($header, $first = true) |
|
125 { |
|
126 foreach ($this->headers as $key => $value) { |
|
127 if (str_replace('-', '_', strtolower($key)) == str_replace('-', '_', strtolower($header))) { |
|
128 if ($first) { |
|
129 return is_array($value) ? (count($value) ? $value[0] : '') : $value; |
|
130 } |
|
131 |
|
132 return is_array($value) ? $value : array($value); |
|
133 } |
|
134 } |
|
135 |
|
136 return $first ? null : array(); |
|
137 } |
|
138 } |