2010-04-19 13:12:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-24 00:22:16 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-19 13:12:42 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-04-19 13:12:42 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\BrowserKit;
|
|
|
|
|
2010-04-19 13:12:42 +01:00
|
|
|
/**
|
|
|
|
* Response object.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-04-19 13:12:42 +01:00
|
|
|
*/
|
|
|
|
class Response
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $content;
|
|
|
|
protected $status;
|
|
|
|
protected $headers;
|
2010-04-19 13:12:42 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2010-06-23 15:24:24 +01:00
|
|
|
* The headers array is a set of key/value pairs. If a header is present multiple times
|
|
|
|
* then the value is an array of all the values.
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @param string $content The content of the response
|
|
|
|
* @param int $status The response status code
|
|
|
|
* @param array $headers An array of headers
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-06-23 15:24:24 +01:00
|
|
|
public function __construct($content = '', $status = 200, array $headers = array())
|
2010-04-21 12:28:59 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->content = $content;
|
2014-10-22 19:27:13 +01:00
|
|
|
$this->status = $status;
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->headers = $headers;
|
2010-04-21 12:28:59 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-02-06 21:10:40 +00:00
|
|
|
/**
|
2011-02-06 23:58:54 +00:00
|
|
|
* Converts the response object to string containing all headers and the response content.
|
2011-02-06 21:10:40 +00:00
|
|
|
*
|
|
|
|
* @return string The response with headers and content
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function __toString()
|
2010-04-21 12:28:59 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$headers = '';
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->headers as $name => $value) {
|
2011-02-11 18:11:48 +00:00
|
|
|
if (is_string($value)) {
|
|
|
|
$headers .= $this->buildHeader($name, $value);
|
|
|
|
} else {
|
|
|
|
foreach ($value as $headerValue) {
|
|
|
|
$headers .= $this->buildHeader($name, $headerValue);
|
|
|
|
}
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-04-21 12:28:59 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $headers."\n".$this->content;
|
|
|
|
}
|
2010-04-21 12:28:59 +01:00
|
|
|
|
2011-02-11 18:11:48 +00:00
|
|
|
/**
|
|
|
|
* Returns the build header line.
|
|
|
|
*
|
|
|
|
* @param string $name The header name
|
|
|
|
* @param string $value The header value
|
|
|
|
*
|
2011-02-11 18:16:01 +00:00
|
|
|
* @return string The built header line
|
2011-02-11 18:11:48 +00:00
|
|
|
*/
|
|
|
|
protected function buildHeader($name, $value)
|
|
|
|
{
|
|
|
|
return sprintf("%s: %s\n", $name, $value);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the response content.
|
|
|
|
*
|
|
|
|
* @return string The response content
|
|
|
|
*/
|
|
|
|
public function getContent()
|
|
|
|
{
|
|
|
|
return $this->content;
|
|
|
|
}
|
2010-04-19 13:12:42 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the response status code.
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @return int The response status code
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getStatus()
|
|
|
|
{
|
|
|
|
return $this->status;
|
|
|
|
}
|
2010-04-19 13:12:42 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the response headers.
|
|
|
|
*
|
|
|
|
* @return array The response headers
|
|
|
|
*/
|
|
|
|
public function getHeaders()
|
|
|
|
{
|
|
|
|
return $this->headers;
|
|
|
|
}
|
2010-04-19 13:12:42 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets a response header.
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @param string $header The header name
|
|
|
|
* @param bool $first Whether to return the first value or all header values
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-06-23 15:24:24 +01:00
|
|
|
* @return string|array The first header value if $first is true, an array of values otherwise
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-06-23 15:24:24 +01:00
|
|
|
public function getHeader($header, $first = true)
|
2010-04-19 13:12:42 +01:00
|
|
|
{
|
2015-03-11 20:36:41 +00:00
|
|
|
$normalizedHeader = str_replace('-', '_', strtolower($header));
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->headers as $key => $value) {
|
2015-03-11 20:36:41 +00:00
|
|
|
if (str_replace('-', '_', strtolower($key)) === $normalizedHeader) {
|
2010-06-23 15:24:24 +01:00
|
|
|
if ($first) {
|
|
|
|
return is_array($value) ? (count($value) ? $value[0] : '') : $value;
|
|
|
|
}
|
2011-02-27 16:47:10 +00:00
|
|
|
|
|
|
|
return is_array($value) ? $value : array($value);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-04-19 13:12:42 +01:00
|
|
|
|
2010-06-23 15:24:24 +01:00
|
|
|
return $first ? null : array();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2011-03-24 08:25:46 +00:00
|
|
|
}
|