/laravel/vendor/Symfony/Component/HttpFoundation/Cookie.php

https://bitbucket.org/Maron1/taqman · PHP · 208 lines · 93 code · 24 blank · 91 comment · 12 complexity · 0f2824b38de8a1f7f8e2831ad785ef80 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. /**
  12. * Represents a cookie
  13. *
  14. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  15. *
  16. * @api
  17. */
  18. class Cookie
  19. {
  20. protected $name;
  21. protected $value;
  22. protected $domain;
  23. protected $expire;
  24. protected $path;
  25. protected $secure;
  26. protected $httpOnly;
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $name The name of the cookie
  31. * @param string $value The value of the cookie
  32. * @param integer|string|\DateTime $expire The time the cookie expires
  33. * @param string $path The path on the server in which the cookie will be available on
  34. * @param string $domain The domain that the cookie is available to
  35. * @param Boolean $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
  36. * @param Boolean $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
  37. *
  38. * @api
  39. */
  40. public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
  41. {
  42. // from PHP source code
  43. if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
  44. throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
  45. }
  46. if (empty($name)) {
  47. throw new \InvalidArgumentException('The cookie name cannot be empty.');
  48. }
  49. // convert expiration time to a Unix timestamp
  50. if ($expire instanceof \DateTime) {
  51. $expire = $expire->format('U');
  52. } elseif (!is_numeric($expire)) {
  53. $expire = strtotime($expire);
  54. if (false === $expire || -1 === $expire) {
  55. throw new \InvalidArgumentException('The cookie expiration time is not valid.');
  56. }
  57. }
  58. $this->name = $name;
  59. $this->value = $value;
  60. $this->domain = $domain;
  61. $this->expire = $expire;
  62. $this->path = empty($path) ? '/' : $path;
  63. $this->secure = (Boolean) $secure;
  64. $this->httpOnly = (Boolean) $httpOnly;
  65. }
  66. /**
  67. * Returns the cookie as a string.
  68. *
  69. * @return string The cookie
  70. */
  71. public function __toString()
  72. {
  73. $str = urlencode($this->getName()).'=';
  74. if ('' === (string) $this->getValue()) {
  75. $str .= 'deleted; expires='.gmdate("D, d-M-Y H:i:s T", time() - 31536001);
  76. } else {
  77. $str .= urlencode($this->getValue());
  78. if ($this->getExpiresTime() !== 0) {
  79. $str .= '; expires='.gmdate("D, d-M-Y H:i:s T", $this->getExpiresTime());
  80. }
  81. }
  82. if ('/' !== $this->path) {
  83. $str .= '; path='.$this->path;
  84. }
  85. if (null !== $this->getDomain()) {
  86. $str .= '; domain='.$this->getDomain();
  87. }
  88. if (true === $this->isSecure()) {
  89. $str .= '; secure';
  90. }
  91. if (true === $this->isHttpOnly()) {
  92. $str .= '; httponly';
  93. }
  94. return $str;
  95. }
  96. /**
  97. * Gets the name of the cookie.
  98. *
  99. * @return string
  100. *
  101. * @api
  102. */
  103. public function getName()
  104. {
  105. return $this->name;
  106. }
  107. /**
  108. * Gets the value of the cookie.
  109. *
  110. * @return string
  111. *
  112. * @api
  113. */
  114. public function getValue()
  115. {
  116. return $this->value;
  117. }
  118. /**
  119. * Gets the domain that the cookie is available to.
  120. *
  121. * @return string
  122. *
  123. * @api
  124. */
  125. public function getDomain()
  126. {
  127. return $this->domain;
  128. }
  129. /**
  130. * Gets the time the cookie expires.
  131. *
  132. * @return integer
  133. *
  134. * @api
  135. */
  136. public function getExpiresTime()
  137. {
  138. return $this->expire;
  139. }
  140. /**
  141. * Gets the path on the server in which the cookie will be available on.
  142. *
  143. * @return string
  144. *
  145. * @api
  146. */
  147. public function getPath()
  148. {
  149. return $this->path;
  150. }
  151. /**
  152. * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
  153. *
  154. * @return Boolean
  155. *
  156. * @api
  157. */
  158. public function isSecure()
  159. {
  160. return $this->secure;
  161. }
  162. /**
  163. * Checks whether the cookie will be made accessible only through the HTTP protocol.
  164. *
  165. * @return Boolean
  166. *
  167. * @api
  168. */
  169. public function isHttpOnly()
  170. {
  171. return $this->httpOnly;
  172. }
  173. /**
  174. * Whether this cookie is about to be cleared
  175. *
  176. * @return Boolean
  177. *
  178. * @api
  179. */
  180. public function isCleared()
  181. {
  182. return $this->expire < time();
  183. }
  184. }