This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/BrowserKit/Response.php

139 lines
3.3 KiB
PHP
Raw Normal View History

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
*
* (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.
*/
namespace Symfony\Component\BrowserKit;
2010-04-19 13:12:42 +01:00
/**
* Response object.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
2010-04-19 13:12:42 +01:00
*/
class Response
{
protected $content;
protected $status;
protected $headers;
2010-04-19 13:12:42 +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.
*
* @param string $content The content of the response
* @param int $status The response status code
* @param array $headers An array of headers
*
* @api
*/
2010-06-23 15:24:24 +01:00
public function __construct($content = '', $status = 200, array $headers = array())
{
$this->content = $content;
$this->status = $status;
$this->headers = $headers;
}
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
*/
public function __toString()
{
$headers = '';
foreach ($this->headers as $name => $value) {
if (is_string($value)) {
$headers .= $this->buildHeader($name, $value);
} else {
foreach ($value as $headerValue) {
$headers .= $this->buildHeader($name, $headerValue);
}
}
}
return $headers."\n".$this->content;
}
/**
* 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
*/
protected function buildHeader($name, $value)
{
return sprintf("%s: %s\n", $name, $value);
}
/**
* Gets the response content.
*
* @return string The response content
*
* @api
*/
public function getContent()
{
return $this->content;
}
2010-04-19 13:12:42 +01:00
/**
* Gets the response status code.
*
2014-04-16 11:30:19 +01:00
* @return int The response status code
*
* @api
*/
public function getStatus()
{
return $this->status;
}
2010-04-19 13:12:42 +01:00
/**
* Gets the response headers.
*
* @return array The response headers
*
* @api
*/
public function getHeaders()
{
return $this->headers;
}
2010-04-19 13:12:42 +01:00
/**
* Gets a response header.
*
2010-06-23 15:24:24 +01:00
* @param string $header The header name
* @param bool $first Whether to return the first value or all header values
*
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-06-23 15:24:24 +01:00
public function getHeader($header, $first = true)
2010-04-19 13:12:42 +01:00
{
foreach ($this->headers as $key => $value) {
if (str_replace('-', '_', strtolower($key)) == str_replace('-', '_', strtolower($header))) {
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-04-19 13:12:42 +01:00
2010-06-23 15:24:24 +01:00
return $first ? null : array();
}
}