HeaderCollection.php 8.2 KB

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