Response.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\BrowserKit;
  11. /**
  12. * @author Fabien Potencier <fabien@symfony.com>
  13. */
  14. class Response
  15. {
  16. protected $content;
  17. protected $status;
  18. protected $headers;
  19. /**
  20. * The headers array is a set of key/value pairs. If a header is present multiple times
  21. * then the value is an array of all the values.
  22. *
  23. * @param string $content The content of the response
  24. * @param int $status The response status code
  25. * @param array $headers An array of headers
  26. */
  27. public function __construct(string $content = '', int $status = 200, array $headers = [])
  28. {
  29. $this->content = $content;
  30. $this->status = $status;
  31. $this->headers = $headers;
  32. }
  33. /**
  34. * Converts the response object to string containing all headers and the response content.
  35. *
  36. * @return string The response with headers and content
  37. */
  38. public function __toString()
  39. {
  40. $headers = '';
  41. foreach ($this->headers as $name => $value) {
  42. if (\is_string($value)) {
  43. $headers .= $this->buildHeader($name, $value);
  44. } else {
  45. foreach ($value as $headerValue) {
  46. $headers .= $this->buildHeader($name, $headerValue);
  47. }
  48. }
  49. }
  50. return $headers."\n".$this->content;
  51. }
  52. /**
  53. * Returns the build header line.
  54. *
  55. * @param string $name The header name
  56. * @param string $value The header value
  57. *
  58. * @return string The built header line
  59. */
  60. protected function buildHeader($name, $value)
  61. {
  62. return sprintf("%s: %s\n", $name, $value);
  63. }
  64. /**
  65. * Gets the response content.
  66. *
  67. * @return string The response content
  68. */
  69. public function getContent()
  70. {
  71. return $this->content;
  72. }
  73. /**
  74. * Gets the response status code.
  75. *
  76. * @return int The response status code
  77. */
  78. public function getStatus()
  79. {
  80. return $this->status;
  81. }
  82. /**
  83. * Gets the response headers.
  84. *
  85. * @return array The response headers
  86. */
  87. public function getHeaders()
  88. {
  89. return $this->headers;
  90. }
  91. /**
  92. * Gets a response header.
  93. *
  94. * @param string $header The header name
  95. * @param bool $first Whether to return the first value or all header values
  96. *
  97. * @return string|array The first header value if $first is true, an array of values otherwise
  98. */
  99. public function getHeader($header, $first = true)
  100. {
  101. $normalizedHeader = str_replace('-', '_', strtolower($header));
  102. foreach ($this->headers as $key => $value) {
  103. if (str_replace('-', '_', strtolower($key)) === $normalizedHeader) {
  104. if ($first) {
  105. return \is_array($value) ? (\count($value) ? $value[0] : '') : $value;
  106. }
  107. return \is_array($value) ? $value : [$value];
  108. }
  109. }
  110. return $first ? null : [];
  111. }
  112. }