Headers.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <[email protected]>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 2.2.0
  10. * @package Slim
  11. *
  12. * MIT LICENSE
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining
  15. * a copy of this software and associated documentation files (the
  16. * "Software"), to deal in the Software without restriction, including
  17. * without limitation the rights to use, copy, modify, merge, publish,
  18. * distribute, sublicense, and/or sell copies of the Software, and to
  19. * permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. namespace Slim\Http;
  34. /**
  35. * HTTP Headers
  36. *
  37. * This class is an abstraction of the HTTP response headers and
  38. * provides array access to the header list while automatically
  39. * stores and retrieves headers with lowercase canonical keys regardless
  40. * of the input format.
  41. *
  42. * This class also implements the `Iterator` and `Countable`
  43. * interfaces for even more convenient usage.
  44. *
  45. * @package Slim
  46. * @author Josh Lockhart
  47. * @since 1.6.0
  48. */
  49. class Headers implements \ArrayAccess, \Iterator, \Countable
  50. {
  51. /**
  52. * @var array HTTP headers
  53. */
  54. protected $headers;
  55. /**
  56. * @var array Map canonical header name to original header name
  57. */
  58. protected $map;
  59. /**
  60. * Constructor
  61. * @param array $headers
  62. */
  63. public function __construct($headers = array())
  64. {
  65. $this->merge($headers);
  66. }
  67. /**
  68. * Merge Headers
  69. * @param array $headers
  70. */
  71. public function merge($headers)
  72. {
  73. foreach ($headers as $name => $value) {
  74. $this[$name] = $value;
  75. }
  76. }
  77. /**
  78. * Transform header name into canonical form
  79. * @param string $name
  80. * @return string
  81. */
  82. protected function canonical($name)
  83. {
  84. return strtolower(trim($name));
  85. }
  86. /**
  87. * Array Access: Offset Exists
  88. */
  89. public function offsetExists($offset)
  90. {
  91. return isset($this->headers[$this->canonical($offset)]);
  92. }
  93. /**
  94. * Array Access: Offset Get
  95. */
  96. public function offsetGet($offset)
  97. {
  98. $canonical = $this->canonical($offset);
  99. if (isset($this->headers[$canonical])) {
  100. return $this->headers[$canonical];
  101. } else {
  102. return null;
  103. }
  104. }
  105. /**
  106. * Array Access: Offset Set
  107. */
  108. public function offsetSet($offset, $value)
  109. {
  110. $canonical = $this->canonical($offset);
  111. $this->headers[$canonical] = $value;
  112. $this->map[$canonical] = $offset;
  113. }
  114. /**
  115. * Array Access: Offset Unset
  116. */
  117. public function offsetUnset($offset)
  118. {
  119. $canonical = $this->canonical($offset);
  120. unset($this->headers[$canonical], $this->map[$canonical]);
  121. }
  122. /**
  123. * Countable: Count
  124. */
  125. public function count()
  126. {
  127. return count($this->headers);
  128. }
  129. /**
  130. * Iterator: Rewind
  131. */
  132. public function rewind()
  133. {
  134. reset($this->headers);
  135. }
  136. /**
  137. * Iterator: Current
  138. */
  139. public function current()
  140. {
  141. return current($this->headers);
  142. }
  143. /**
  144. * Iterator: Key
  145. */
  146. public function key()
  147. {
  148. $key = key($this->headers);
  149. return $this->map[$key];
  150. }
  151. /**
  152. * Iterator: Next
  153. */
  154. public function next()
  155. {
  156. return next($this->headers);
  157. }
  158. /**
  159. * Iterator: Valid
  160. */
  161. public function valid()
  162. {
  163. return current($this->headers) !== false;
  164. }
  165. }