Drupal investigation

Response.php 3.2KB

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