PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/cookie/Cookie.php

https://gitlab.com/igorbabko/mindk
PHP | 213 lines | 120 code | 19 blank | 74 comment | 12 complexity | 311833359bf87eb1f4c20585eaf03796 MD5 | raw file
  1. <?php
  2. /**
  3. * File /framework/cookie/Cookie.php contains Cookie class to easy manipulate with cookies.
  4. *
  5. * PHP version 5
  6. *
  7. * @package Framework\Cookie
  8. * @author Igor Babko <i.i.babko@gmail.com>
  9. */
  10. namespace Framework\Cookie;
  11. use Framework\Exception\CookieException;
  12. /**
  13. * Class cookie represents objects to work with cookies.
  14. * Default implementation of {@link CookieInterface}.
  15. *
  16. * @package Framework\Cookie
  17. * @author Igor Babko <i.i.babko@gmail.com>
  18. */
  19. class Cookie implements CookieInterface
  20. {
  21. /**
  22. * @static
  23. * @var Cookie|null $_instance Cookie instance
  24. */
  25. private static $_instance = null;
  26. /**
  27. * @const null SESSION Set cookie for a session.
  28. */
  29. const SESSION = null;
  30. /**
  31. * @const int DAY Set cookie for a day.
  32. */
  33. const DAY = 86400;
  34. /**
  35. * @const int WEEK Set cookie for a week.
  36. */
  37. const WEEK = 604800;
  38. /**
  39. * @const int MONTH Set cookie for a month.
  40. */
  41. const MONTH = 2592000;
  42. /**
  43. * @const int SIX_MONTHS Set cookie for six months.
  44. */
  45. const SIX_MONTHS = 15811200;
  46. /**
  47. * @const int YEAR Set cookie for a year.
  48. */
  49. const YEAR = 31536000;
  50. /**
  51. * @const int FOREVER Set cookie forever.
  52. */
  53. const FOREVER = -1;
  54. /**
  55. * @var array $_cookies Cookies to be sent
  56. */
  57. private $_cookies = array();
  58. /**
  59. * Cookie constructor.
  60. *
  61. * @return \Framework\Cookie\Cookie Cookie object.
  62. */
  63. private function __construct()
  64. {
  65. }
  66. /**
  67. * Method to clone objects of its class.
  68. *
  69. * @return \Framework\Cookie\Cookie Cookie instance.
  70. */
  71. private function __clone()
  72. {
  73. }
  74. /**
  75. * Method returns Cookie instance creating it if it's not been instantiated before
  76. * otherwise existed Cookie object will be returned.
  77. *
  78. * @return \Framework\Cookie\Cookie Cookie instance.
  79. */
  80. public static function getInstance()
  81. {
  82. if (null === self::$_instance) {
  83. self::$_instance = new self();
  84. }
  85. return self::$_instance;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getCookies()
  91. {
  92. return $this->_cookies;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function exists($name)
  98. {
  99. if (is_string($name)) {
  100. return isset($_COOKIE[$name]);
  101. } else {
  102. $parameterType = gettype($name);
  103. throw new CookieException(
  104. 500, "<strong>Internal server error:</strong> parameter for Cookie::exists method must be 'string', '$parameterType' is given"
  105. );
  106. }
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function send()
  112. {
  113. if (!headers_sent()) {
  114. foreach ($this->_cookies as $cookieName => $cookieInfo) {
  115. $returnValue = setcookie(
  116. $cookieName,
  117. $cookieInfo['value'],
  118. $cookieInfo['expiry'],
  119. $cookieInfo['path'],
  120. $cookieInfo['domain']
  121. );
  122. if ($returnValue) {
  123. $_COOKIE[$cookieName] = $cookieInfo['value'];
  124. } else {
  125. throw new CookieException(
  126. 500, "<strong>Internal server error:</strong> cookie '$cookieName' has not be set successfully."
  127. );
  128. }
  129. }
  130. } else {
  131. throw new CookieException(500, "<strong>Internal server error:</strong> headers has already been sent.");
  132. }
  133. return true;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function add($name, $value, $expiry = self::YEAR, $path = '/', $domain = false)
  139. {
  140. if ($expiry === -1) {
  141. $expiry = 1893456000;
  142. } elseif (is_numeric($expiry)) {
  143. $expiry += time();
  144. } else {
  145. $expiry = strtotime($expiry);
  146. }
  147. $this->_cookies[$name] = array(
  148. 'value' => $value,
  149. 'expiry' => $expiry,
  150. 'path' => $path,
  151. 'domain' => $domain?$_SERVER['HTTP_HOST']:$domain
  152. );
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function isEmpty($name)
  158. {
  159. if ($this->exists($name)) {
  160. return empty($this->_cookies[$name]);
  161. } else {
  162. throw new CookieException(500, "<strong>Internal server error:</strong> cookie '$name' doesn't exists.");
  163. }
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function get($name, $default = '')
  169. {
  170. return isset($_COOKIE[$name])?$_COOKIE[$name]:$default;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function remove($name, $path = '/', $domain = false, $global = true)
  176. {
  177. if (!headers_sent()) {
  178. if ($domain === false) {
  179. $domain = $_SERVER['HTTP_HOST'];
  180. }
  181. $returnValue = setcookie($name, '', time() - 3600, $path, $domain);
  182. if ($global) {
  183. unset($_COOKIE[$name]);
  184. }
  185. return $returnValue;
  186. } else {
  187. throw new CookieException(500, "<strong>Internal server error:</strong> headers has been sent already");
  188. }
  189. }
  190. }