Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\BrowserKit; Chris@0: Chris@0: /** Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class Response Chris@0: { Chris@0: protected $content; Chris@0: protected $status; Chris@0: protected $headers; Chris@0: Chris@0: /** Chris@0: * The headers array is a set of key/value pairs. If a header is present multiple times Chris@0: * then the value is an array of all the values. Chris@0: * Chris@0: * @param string $content The content of the response Chris@0: * @param int $status The response status code Chris@0: * @param array $headers An array of headers Chris@0: */ Chris@17: public function __construct($content = '', $status = 200, array $headers = []) Chris@0: { Chris@0: $this->content = $content; Chris@0: $this->status = $status; Chris@0: $this->headers = $headers; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Converts the response object to string containing all headers and the response content. Chris@0: * Chris@0: * @return string The response with headers and content Chris@0: */ Chris@0: public function __toString() Chris@0: { Chris@0: $headers = ''; Chris@0: foreach ($this->headers as $name => $value) { Chris@17: if (\is_string($value)) { Chris@0: $headers .= $this->buildHeader($name, $value); Chris@0: } else { Chris@0: foreach ($value as $headerValue) { Chris@0: $headers .= $this->buildHeader($name, $headerValue); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $headers."\n".$this->content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the build header line. Chris@0: * Chris@0: * @param string $name The header name Chris@0: * @param string $value The header value Chris@0: * Chris@0: * @return string The built header line Chris@0: */ Chris@0: protected function buildHeader($name, $value) Chris@0: { Chris@0: return sprintf("%s: %s\n", $name, $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the response content. Chris@0: * Chris@0: * @return string The response content Chris@0: */ Chris@0: public function getContent() Chris@0: { Chris@0: return $this->content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the response status code. Chris@0: * Chris@0: * @return int The response status code Chris@0: */ Chris@0: public function getStatus() Chris@0: { Chris@0: return $this->status; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the response headers. Chris@0: * Chris@0: * @return array The response headers Chris@0: */ Chris@0: public function getHeaders() Chris@0: { Chris@0: return $this->headers; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets a response header. Chris@0: * Chris@0: * @param string $header The header name Chris@0: * @param bool $first Whether to return the first value or all header values Chris@0: * Chris@0: * @return string|array The first header value if $first is true, an array of values otherwise Chris@0: */ Chris@0: public function getHeader($header, $first = true) Chris@0: { Chris@0: $normalizedHeader = str_replace('-', '_', strtolower($header)); Chris@0: foreach ($this->headers as $key => $value) { Chris@0: if (str_replace('-', '_', strtolower($key)) === $normalizedHeader) { Chris@0: if ($first) { Chris@17: return \is_array($value) ? (\count($value) ? $value[0] : '') : $value; Chris@0: } Chris@0: Chris@17: return \is_array($value) ? $value : [$value]; Chris@0: } Chris@0: } Chris@0: Chris@17: return $first ? null : []; Chris@0: } Chris@0: }