Response.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * @final since Symfony 4.3
  15. */
  16. class Response
  17. {
  18. /** @internal */
  19. protected $content;
  20. /** @internal */
  21. protected $status;
  22. /** @internal */
  23. protected $headers;
  24. /**
  25. * The headers array is a set of key/value pairs. If a header is present multiple times
  26. * then the value is an array of all the values.
  27. *
  28. * @param string $content The content of the response
  29. * @param int $status The response status code
  30. * @param array $headers An array of headers
  31. */
  32. public function __construct(string $content = '', int $status = 200, array $headers = [])
  33. {
  34. $this->content = $content;
  35. $this->status = $status;
  36. $this->headers = $headers;
  37. }
  38. /**
  39. * Converts the response object to string containing all headers and the response content.
  40. *
  41. * @return string The response with headers and content
  42. */
  43. public function __toString()
  44. {
  45. $headers = '';
  46. foreach ($this->headers as $name => $value) {
  47. if (\is_string($value)) {
  48. $headers .= sprintf("%s: %s\n", $name, $value);
  49. } else {
  50. foreach ($value as $headerValue) {
  51. $headers .= sprintf("%s: %s\n", $name, $headerValue);
  52. }
  53. }
  54. }
  55. return $headers."\n".$this->content;
  56. }
  57. /**
  58. * Returns the build header line.
  59. *
  60. * @param string $name The header name
  61. * @param string $value The header value
  62. *
  63. * @return string The built header line
  64. *
  65. * @deprecated since Symfony 4.3
  66. */
  67. protected function buildHeader($name, $value)
  68. {
  69. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.3.', __METHOD__), E_USER_DEPRECATED);
  70. return sprintf("%s: %s\n", $name, $value);
  71. }
  72. /**
  73. * Gets the response content.
  74. *
  75. * @return string The response content
  76. */
  77. public function getContent()
  78. {
  79. return $this->content;
  80. }
  81. /**
  82. * Gets the response status code.
  83. *
  84. * @return int The response status code
  85. *
  86. * @deprecated since Symfony 4.3, use getStatusCode() instead
  87. */
  88. public function getStatus()
  89. {
  90. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.3, use getStatusCode() instead.', __METHOD__), E_USER_DEPRECATED);
  91. return $this->status;
  92. }
  93. public function getStatusCode(): int
  94. {
  95. return $this->status;
  96. }
  97. /**
  98. * Gets the response headers.
  99. *
  100. * @return array The response headers
  101. */
  102. public function getHeaders()
  103. {
  104. return $this->headers;
  105. }
  106. /**
  107. * Gets a response header.
  108. *
  109. * @param string $header The header name
  110. * @param bool $first Whether to return the first value or all header values
  111. *
  112. * @return string|array The first header value if $first is true, an array of values otherwise
  113. */
  114. public function getHeader($header, $first = true)
  115. {
  116. $normalizedHeader = str_replace('-', '_', strtolower($header));
  117. foreach ($this->headers as $key => $value) {
  118. if (str_replace('-', '_', strtolower($key)) === $normalizedHeader) {
  119. if ($first) {
  120. return \is_array($value) ? (\count($value) ? $value[0] : '') : $value;
  121. }
  122. return \is_array($value) ? $value : [$value];
  123. }
  124. }
  125. return $first ? null : [];
  126. }
  127. }