CookieInterface.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.5.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Http\Cookie;
  15. /**
  16. * Cookie Interface
  17. *
  18. * @method string|null getSameSite()
  19. * @method static withSameSite($sameSite = null)
  20. */
  21. interface CookieInterface
  22. {
  23. /**
  24. * Expires attribute format.
  25. *
  26. * @var string
  27. */
  28. const EXPIRES_FORMAT = 'D, d-M-Y H:i:s T';
  29. /**
  30. * SameSite attribute value: Lax
  31. *
  32. * @var string
  33. */
  34. const SAMESITE_LAX = 'Lax';
  35. /**
  36. * SameSite attribute value: Strict
  37. *
  38. * @var string
  39. */
  40. const SAMESITE_STRICT = 'Strict';
  41. /**
  42. * SameSite attribute value: None
  43. *
  44. * @var string
  45. */
  46. const SAMESITE_NONE = 'None';
  47. /**
  48. * Valid values for "SameSite" attribute.
  49. *
  50. * @var string[]
  51. */
  52. const SAMESITE_VALUES = [
  53. self::SAMESITE_LAX,
  54. self::SAMESITE_STRICT,
  55. self::SAMESITE_NONE,
  56. ];
  57. /**
  58. * Sets the cookie name
  59. *
  60. * @param string $name Name of the cookie
  61. * @return static
  62. */
  63. public function withName($name);
  64. /**
  65. * Gets the cookie name
  66. *
  67. * @return string
  68. */
  69. public function getName();
  70. /**
  71. * Gets the cookie value
  72. *
  73. * @return string|array
  74. */
  75. public function getValue();
  76. /**
  77. * Gets the cookie value as a string.
  78. *
  79. * This will collapse any complex data in the cookie with json_encode()
  80. *
  81. * @return string
  82. */
  83. public function getStringValue();
  84. /**
  85. * Create a cookie with an updated value.
  86. *
  87. * @param string|array $value Value of the cookie to set
  88. * @return static
  89. */
  90. public function withValue($value);
  91. /**
  92. * Get the id for a cookie
  93. *
  94. * Cookies are unique across name, domain, path tuples.
  95. *
  96. * @return string
  97. */
  98. public function getId();
  99. /**
  100. * Get the path attribute.
  101. *
  102. * @return string
  103. */
  104. public function getPath();
  105. /**
  106. * Create a new cookie with an updated path
  107. *
  108. * @param string $path Sets the path
  109. * @return static
  110. */
  111. public function withPath($path);
  112. /**
  113. * Get the domain attribute.
  114. *
  115. * @return string
  116. */
  117. public function getDomain();
  118. /**
  119. * Create a cookie with an updated domain
  120. *
  121. * @param string $domain Domain to set
  122. * @return static
  123. */
  124. public function withDomain($domain);
  125. /**
  126. * Get the current expiry time
  127. *
  128. * @return \DateTime|\DateTimeImmutable|null Timestamp of expiry or null
  129. */
  130. public function getExpiry();
  131. /**
  132. * Get the timestamp from the expiration time
  133. *
  134. * Timestamps are strings as large timestamps can overflow MAX_INT
  135. * in 32bit systems.
  136. *
  137. * @return string|null The expiry time as a string timestamp.
  138. */
  139. public function getExpiresTimestamp();
  140. /**
  141. * Builds the expiration value part of the header string
  142. *
  143. * @return string
  144. */
  145. public function getFormattedExpires();
  146. /**
  147. * Create a cookie with an updated expiration date
  148. *
  149. * @param \DateTime|\DateTimeImmutable $dateTime Date time object
  150. * @return static
  151. */
  152. public function withExpiry($dateTime);
  153. /**
  154. * Create a new cookie that will virtually never expire.
  155. *
  156. * @return static
  157. */
  158. public function withNeverExpire();
  159. /**
  160. * Create a new cookie that will expire/delete the cookie from the browser.
  161. *
  162. * This is done by setting the expiration time to 1 year ago
  163. *
  164. * @return static
  165. */
  166. public function withExpired();
  167. /**
  168. * Check if a cookie is expired when compared to $time
  169. *
  170. * Cookies without an expiration date always return false.
  171. *
  172. * @param \DateTime|\DateTimeImmutable $time The time to test against. Defaults to 'now' in UTC.
  173. * @return bool
  174. */
  175. public function isExpired($time = null);
  176. /**
  177. * Check if the cookie is HTTP only
  178. *
  179. * @return bool
  180. */
  181. public function isHttpOnly();
  182. /**
  183. * Create a cookie with HTTP Only updated
  184. *
  185. * @param bool $httpOnly HTTP Only
  186. * @return static
  187. */
  188. public function withHttpOnly($httpOnly);
  189. /**
  190. * Check if the cookie is secure
  191. *
  192. * @return bool
  193. */
  194. public function isSecure();
  195. /**
  196. * Create a cookie with Secure updated
  197. *
  198. * @param bool $secure Secure attribute value
  199. * @return static
  200. */
  201. public function withSecure($secure);
  202. /**
  203. * Returns the cookie as header value
  204. *
  205. * @return string
  206. */
  207. public function toHeaderValue();
  208. }