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

144 lines
3.7 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
/**
* @author Fabien Potencier <fabien@symfony.com>
2019-01-14 14:42:11 +00:00
*
* @final since Symfony 4.3
2010-04-19 13:12:42 +01:00
*/
class Response
{
2019-01-14 14:42:11 +00:00
/** @internal */
protected $content;
2019-01-14 14:42:11 +00:00
/** @internal */
protected $status;
2019-01-14 14:42:11 +00:00
/** @internal */
protected $headers;
2010-04-19 13:12:42 +01:00
/**
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
*/
2019-01-16 18:24:45 +00:00
public function __construct(string $content = '', int $status = 200, array $headers = [])
{
$this->content = $content;
2014-10-22 19:27:13 +01:00
$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 .= sprintf("%s: %s\n", $name, $value);
} else {
foreach ($value as $headerValue) {
$headers .= sprintf("%s: %s\n", $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
*
* @deprecated since Symfony 4.3
*/
protected function buildHeader($name, $value)
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.3.', __METHOD__), E_USER_DEPRECATED);
return sprintf("%s: %s\n", $name, $value);
}
/**
* Gets the response content.
*
* @return string The response content
*/
public function getContent()
{
return $this->content;
}
2010-04-19 13:12:42 +01:00
/**
* Gets the response status code.
*
2014-11-30 13:33:44 +00:00
* @return int The response status code
*
* @deprecated since Symfony 4.3, use getStatusCode() instead
*/
public function getStatus()
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.3, use getStatusCode() instead.', __METHOD__), E_USER_DEPRECATED);
return $this->status;
}
public function getStatusCode(): int
{
return $this->status;
}
2010-04-19 13:12:42 +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
/**
* 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-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
{
$normalizedHeader = str_replace('-', '_', strtolower($header));
foreach ($this->headers as $key => $value) {
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;
2010-06-23 15:24:24 +01:00
}
2011-02-27 16:47:10 +00:00
2019-01-16 18:24:45 +00:00
return \is_array($value) ? $value : [$value];
}
}
2010-04-19 13:12:42 +01:00
2019-01-16 18:24:45 +00:00
return $first ? null : [];
}
}