HeaderCollection.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use Yii;
  9. use yii\base\BaseObject;
  10. /**
  11. * HeaderCollection is used by [[Response]] to maintain the currently registered HTTP headers.
  12. *
  13. * @property int $count The number of headers in the collection. This property is read-only.
  14. * @property \ArrayIterator $iterator An iterator for traversing the headers in the collection. This property
  15. * is read-only.
  16. *
  17. * @author Qiang Xue <qiang.xue@gmail.com>
  18. * @since 2.0
  19. */
  20. class HeaderCollection extends BaseObject implements \IteratorAggregate, \ArrayAccess, \Countable
  21. {
  22. /**
  23. * @var array the headers in this collection (indexed by the header names)
  24. */
  25. private $_headers = [];
  26. /**
  27. * Returns an iterator for traversing the headers in the collection.
  28. * This method is required by the SPL interface [[\IteratorAggregate]].
  29. * It will be implicitly called when you use `foreach` to traverse the collection.
  30. * @return \ArrayIterator an iterator for traversing the headers in the collection.
  31. */
  32. public function getIterator()
  33. {
  34. return new \ArrayIterator($this->_headers);
  35. }
  36. /**
  37. * Returns the number of headers in the collection.
  38. * This method is required by the SPL `Countable` interface.
  39. * It will be implicitly called when you use `count($collection)`.
  40. * @return int the number of headers in the collection.
  41. */
  42. public function count()
  43. {
  44. return $this->getCount();
  45. }
  46. /**
  47. * Returns the number of headers in the collection.
  48. * @return int the number of headers in the collection.
  49. */
  50. public function getCount()
  51. {
  52. return count($this->_headers);
  53. }
  54. /**
  55. * Returns the named header(s).
  56. * @param string $name the name of the header to return
  57. * @param mixed $default the value to return in case the named header does not exist
  58. * @param bool $first whether to only return the first header of the specified name.
  59. * If false, all headers of the specified name will be returned.
  60. * @return string|array the named header(s). If `$first` is true, a string will be returned;
  61. * If `$first` is false, an array will be returned.
  62. */
  63. public function get($name, $default = null, $first = true)
  64. {
  65. $name = strtolower($name);
  66. if (isset($this->_headers[$name])) {
  67. return $first ? reset($this->_headers[$name]) : $this->_headers[$name];
  68. }
  69. return $default;
  70. }
  71. /**
  72. * Adds a new header.
  73. * If there is already a header with the same name, it will be replaced.
  74. * @param string $name the name of the header
  75. * @param string $value the value of the header
  76. * @return $this the collection object itself
  77. */
  78. public function set($name, $value = '')
  79. {
  80. $name = strtolower($name);
  81. $this->_headers[$name] = (array) $value;
  82. return $this;
  83. }
  84. /**
  85. * Adds a new header.
  86. * If there is already a header with the same name, the new one will
  87. * be appended to it instead of replacing it.
  88. * @param string $name the name of the header
  89. * @param string $value the value of the header
  90. * @return $this the collection object itself
  91. */
  92. public function add($name, $value)
  93. {
  94. $name = strtolower($name);
  95. $this->_headers[$name][] = $value;
  96. return $this;
  97. }
  98. /**
  99. * Sets a new header only if it does not exist yet.
  100. * If there is already a header with the same name, the new one will be ignored.
  101. * @param string $name the name of the header
  102. * @param string $value the value of the header
  103. * @return $this the collection object itself
  104. */
  105. public function setDefault($name, $value)
  106. {
  107. $name = strtolower($name);
  108. if (empty($this->_headers[$name])) {
  109. $this->_headers[$name][] = $value;
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Returns a value indicating whether the named header exists.
  115. * @param string $name the name of the header
  116. * @return bool whether the named header exists
  117. */
  118. public function has($name)
  119. {
  120. $name = strtolower($name);
  121. return isset($this->_headers[$name]);
  122. }
  123. /**
  124. * Removes a header.
  125. * @param string $name the name of the header to be removed.
  126. * @return array the value of the removed header. Null is returned if the header does not exist.
  127. */
  128. public function remove($name)
  129. {
  130. $name = strtolower($name);
  131. if (isset($this->_headers[$name])) {
  132. $value = $this->_headers[$name];
  133. unset($this->_headers[$name]);
  134. return $value;
  135. }
  136. return null;
  137. }
  138. /**
  139. * Removes all headers.
  140. */
  141. public function removeAll()
  142. {
  143. $this->_headers = [];
  144. }
  145. /**
  146. * Returns the collection as a PHP array.
  147. * @return array the array representation of the collection.
  148. * The array keys are header names, and the array values are the corresponding header values.
  149. */
  150. public function toArray()
  151. {
  152. return $this->_headers;
  153. }
  154. /**
  155. * Populates the header collection from an array.
  156. * @param array $array the headers to populate from
  157. * @since 2.0.3
  158. */
  159. public function fromArray(array $array)
  160. {
  161. $this->_headers = $array;
  162. }
  163. /**
  164. * Returns whether there is a header with the specified name.
  165. * This method is required by the SPL interface [[\ArrayAccess]].
  166. * It is implicitly called when you use something like `isset($collection[$name])`.
  167. * @param string $name the header name
  168. * @return bool whether the named header exists
  169. */
  170. public function offsetExists($name)
  171. {
  172. return $this->has($name);
  173. }
  174. /**
  175. * Returns the header with the specified name.
  176. * This method is required by the SPL interface [[\ArrayAccess]].
  177. * It is implicitly called when you use something like `$header = $collection[$name];`.
  178. * This is equivalent to [[get()]].
  179. * @param string $name the header name
  180. * @return string the header value with the specified name, null if the named header does not exist.
  181. */
  182. public function offsetGet($name)
  183. {
  184. return $this->get($name);
  185. }
  186. /**
  187. * Adds the header to the collection.
  188. * This method is required by the SPL interface [[\ArrayAccess]].
  189. * It is implicitly called when you use something like `$collection[$name] = $header;`.
  190. * This is equivalent to [[add()]].
  191. * @param string $name the header name
  192. * @param string $value the header value to be added
  193. */
  194. public function offsetSet($name, $value)
  195. {
  196. $this->set($name, $value);
  197. }
  198. /**
  199. * Removes the named header.
  200. * This method is required by the SPL interface [[\ArrayAccess]].
  201. * It is implicitly called when you use something like `unset($collection[$name])`.
  202. * This is equivalent to [[remove()]].
  203. * @param string $name the header name
  204. */
  205. public function offsetUnset($name)
  206. {
  207. $this->remove($name);
  208. }
  209. }