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;
15 * @author Fabien Potencier <fabien@symfony.com>
24 * The headers array is a set of key/value pairs. If a header is present multiple times
25 * then the value is an array of all the values.
27 * @param string $content The content of the response
28 * @param int $status The response status code
29 * @param array $headers An array of headers
31 public function __construct($content = '', $status = 200, array $headers = array())
33 $this->content = $content;
34 $this->status = $status;
35 $this->headers = $headers;
39 * Converts the response object to string containing all headers and the response content.
41 * @return string The response with headers and content
43 public function __toString()
46 foreach ($this->headers as $name => $value) {
47 if (\is_string($value)) {
48 $headers .= $this->buildHeader($name, $value);
50 foreach ($value as $headerValue) {
51 $headers .= $this->buildHeader($name, $headerValue);
56 return $headers."\n".$this->content;
60 * Returns the build header line.
62 * @param string $name The header name
63 * @param string $value The header value
65 * @return string The built header line
67 protected function buildHeader($name, $value)
69 return sprintf("%s: %s\n", $name, $value);
73 * Gets the response content.
75 * @return string The response content
77 public function getContent()
79 return $this->content;
83 * Gets the response status code.
85 * @return int The response status code
87 public function getStatus()
93 * Gets the response headers.
95 * @return array The response headers
97 public function getHeaders()
99 return $this->headers;
103 * Gets a response header.
105 * @param string $header The header name
106 * @param bool $first Whether to return the first value or all header values
108 * @return string|array The first header value if $first is true, an array of values otherwise
110 public function getHeader($header, $first = true)
112 $normalizedHeader = str_replace('-', '_', strtolower($header));
113 foreach ($this->headers as $key => $value) {
114 if (str_replace('-', '_', strtolower($key)) === $normalizedHeader) {
116 return \is_array($value) ? (\count($value) ? $value[0] : '') : $value;
119 return \is_array($value) ? $value : array($value);
123 return $first ? null : array();