Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

237 rindas
7.1KB

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