4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\BrowserKit;
17 * @author Fabien Potencier <fabien@symfony.com>
28 * The headers array is a set of key/value pairs. If a header is present multiple times
29 * then the value is an array of all the values.
31 * @param string $content The content of the response
32 * @param int $status The response status code
33 * @param array $headers An array of headers
35 public function __construct($content = '', $status = 200, array $headers = array())
37 $this->content = $content;
38 $this->status = $status;
39 $this->headers = $headers;
43 * Converts the response object to string containing all headers and the response content.
45 * @return string The response with headers and content
47 public function __toString()
50 foreach ($this->headers as $name => $value) {
51 if (is_string($value)) {
52 $headers .= $this->buildHeader($name, $value);
54 foreach ($value as $headerValue) {
55 $headers .= $this->buildHeader($name, $headerValue);
60 return $headers."\n".$this->content;
64 * Returns the build header line.
66 * @param string $name The header name
67 * @param string $value The header value
69 * @return string The built header line
71 protected function buildHeader($name, $value)
73 return sprintf("%s: %s\n", $name, $value);
77 * Gets the response content.
79 * @return string The response content
81 public function getContent()
83 return $this->content;
87 * Gets the response status code.
89 * @return int The response status code
91 public function getStatus()
97 * Gets the response headers.
99 * @return array The response headers
101 public function getHeaders()
103 return $this->headers;
107 * Gets a response header.
109 * @param string $header The header name
110 * @param bool $first Whether to return the first value or all header values
112 * @return string|array The first header value if $first is true, an array of values otherwise
114 public function getHeader($header, $first = true)
116 $normalizedHeader = str_replace('-', '_', strtolower($header));
117 foreach ($this->headers as $key => $value) {
118 if (str_replace('-', '_', strtolower($key)) === $normalizedHeader) {
120 return is_array($value) ? (count($value) ? $value[0] : '') : $value;
123 return is_array($value) ? $value : array($value);
127 return $first ? null : array();