Response.php 3.3 KB

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