PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Widget/Header.php

https://github.com/putersham/widget
PHP | 105 lines | 36 code | 10 blank | 59 comment | 3 complexity | e68c82f47490a32e7605f4ec36f9d4f4 MD5 | raw file
  1. <?php
  2. /**
  3. * Widget Framework
  4. *
  5. * @copyright Copyright (c) 2008-2013 Twin Huang
  6. * @license http://opensource.org/licenses/mit-license.php MIT License
  7. */
  8. namespace Widget;
  9. /**
  10. * A widget that handles the HTTP response headers
  11. *
  12. * @author Twin Huang <twinhuang@qq.com>
  13. * @property Response $response A widget that handles the HTTP response data
  14. */
  15. class Header extends AbstractWidget
  16. {
  17. /**
  18. * The variable to store array
  19. *
  20. * @var array
  21. */
  22. protected $data = array();
  23. /**
  24. * Constructor
  25. *
  26. * @param array $options
  27. */
  28. public function __construct(array $options = array())
  29. {
  30. parent::__construct($options);
  31. $this->data = &$this->response->getHeaderReference();
  32. }
  33. /**
  34. * Get or set HTTP header
  35. *
  36. * @param string|array $name The header name or an associative array
  37. * that the key is header name and the value
  38. * is header value
  39. * @param string|array $values The header values, for set method only
  40. * @param bool $replace Whether replace the exists values or not, for set method only
  41. * @return mixed
  42. */
  43. public function __invoke($name, $values = null, $replace = true)
  44. {
  45. if (1 == func_num_args()) {
  46. return $this->get($name);
  47. } else {
  48. return $this->set($name, $values, $replace);
  49. }
  50. }
  51. /**
  52. * Set the header string
  53. *
  54. * @param string $name The header name
  55. * @param string|array $values The header values
  56. * @param bool $replace Whether replace the exists values or not
  57. * @return Header
  58. */
  59. public function set($name, $values = null, $replace = true)
  60. {
  61. return $this->response->setHeader($name, $values, $replace);
  62. }
  63. /**
  64. * Get the header string
  65. *
  66. * @param string $name The header name
  67. * @param mixed $default The default value
  68. * @param bool $first return the first element or the whole header values
  69. * @return mixed
  70. */
  71. public function get($name, $default = null, $first = true)
  72. {
  73. return $this->response->getHeader($name, $default, $first);
  74. }
  75. /**
  76. * Remove header by specified name
  77. *
  78. * @param string $name The header name
  79. * @return Header
  80. */
  81. public function remove($name)
  82. {
  83. $this->response->removeHeader($name);
  84. return $this;
  85. }
  86. /**
  87. * Returns response header as string
  88. *
  89. * @return string
  90. */
  91. public function __toString()
  92. {
  93. return $this->response->getHeaderString();
  94. }
  95. }