Cookie.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // Copyright 2004-present Facebook. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. namespace Facebook\WebDriver;
  16. use InvalidArgumentException;
  17. /**
  18. * Set values of an cookie.
  19. *
  20. * Implements ArrayAccess for backwards compatibility.
  21. *
  22. * @see https://w3c.github.io/webdriver/webdriver-spec.html#cookies
  23. */
  24. class Cookie implements \ArrayAccess
  25. {
  26. /** @var array */
  27. protected $cookie = [];
  28. /**
  29. * @param string $name The name of the cookie; may not be null or an empty string.
  30. * @param string $value The cookie value; may not be null.
  31. */
  32. public function __construct($name, $value)
  33. {
  34. $this->validateCookieName($name);
  35. $this->validateCookieValue($value);
  36. $this->cookie['name'] = $name;
  37. $this->cookie['value'] = $value;
  38. }
  39. /**
  40. * @param array $cookieArray The cookie fields; must contain name and value.
  41. * @return Cookie
  42. */
  43. public static function createFromArray(array $cookieArray)
  44. {
  45. if (!isset($cookieArray['name'])) {
  46. throw new InvalidArgumentException('Cookie name should be set');
  47. }
  48. if (!isset($cookieArray['value'])) {
  49. throw new InvalidArgumentException('Cookie value should be set');
  50. }
  51. $cookie = new self($cookieArray['name'], $cookieArray['value']);
  52. if (isset($cookieArray['path'])) {
  53. $cookie->setPath($cookieArray['path']);
  54. }
  55. if (isset($cookieArray['domain'])) {
  56. $cookie->setDomain($cookieArray['domain']);
  57. }
  58. if (isset($cookieArray['expiry'])) {
  59. $cookie->setExpiry($cookieArray['expiry']);
  60. }
  61. if (isset($cookieArray['secure'])) {
  62. $cookie->setSecure($cookieArray['secure']);
  63. }
  64. if (isset($cookieArray['httpOnly'])) {
  65. $cookie->setHttpOnly($cookieArray['httpOnly']);
  66. }
  67. return $cookie;
  68. }
  69. /**
  70. * @return string
  71. */
  72. public function getName()
  73. {
  74. return $this->offsetGet('name');
  75. }
  76. /**
  77. * @return string
  78. */
  79. public function getValue()
  80. {
  81. return $this->offsetGet('value');
  82. }
  83. /**
  84. * The path the cookie is visible to. Defaults to "/" if omitted.
  85. *
  86. * @param string $path
  87. */
  88. public function setPath($path)
  89. {
  90. $this->offsetSet('path', $path);
  91. }
  92. /**
  93. * @return string|null
  94. */
  95. public function getPath()
  96. {
  97. return $this->offsetGet('path');
  98. }
  99. /**
  100. * The domain the cookie is visible to. Defaults to the current browsing context's document's URL domain if omitted.
  101. *
  102. * @param string $domain
  103. */
  104. public function setDomain($domain)
  105. {
  106. if (mb_strpos($domain, ':') !== false) {
  107. throw new InvalidArgumentException(sprintf('Cookie domain "%s" should not contain a port', $domain));
  108. }
  109. $this->offsetSet('domain', $domain);
  110. }
  111. /**
  112. * @return string|null
  113. */
  114. public function getDomain()
  115. {
  116. return $this->offsetGet('domain');
  117. }
  118. /**
  119. * The cookie's expiration date, specified in seconds since Unix Epoch.
  120. *
  121. * @param int $expiry
  122. */
  123. public function setExpiry($expiry)
  124. {
  125. $this->offsetSet('expiry', (int) $expiry);
  126. }
  127. /**
  128. * @return int|null
  129. */
  130. public function getExpiry()
  131. {
  132. return $this->offsetGet('expiry');
  133. }
  134. /**
  135. * Whether this cookie requires a secure connection (https). Defaults to false if omitted.
  136. *
  137. * @param bool $secure
  138. */
  139. public function setSecure($secure)
  140. {
  141. $this->offsetSet('secure', $secure);
  142. }
  143. /**
  144. * @return bool|null
  145. */
  146. public function isSecure()
  147. {
  148. return $this->offsetGet('secure');
  149. }
  150. /**
  151. * Whether the cookie is an HTTP only cookie. Defaults to false if omitted.
  152. *
  153. * @param bool $httpOnly
  154. */
  155. public function setHttpOnly($httpOnly)
  156. {
  157. $this->offsetSet('httpOnly', $httpOnly);
  158. }
  159. /**
  160. * @return bool|null
  161. */
  162. public function isHttpOnly()
  163. {
  164. return $this->offsetGet('httpOnly');
  165. }
  166. /**
  167. * @return array
  168. */
  169. public function toArray()
  170. {
  171. return $this->cookie;
  172. }
  173. public function offsetExists($offset)
  174. {
  175. return isset($this->cookie[$offset]);
  176. }
  177. public function offsetGet($offset)
  178. {
  179. return $this->offsetExists($offset) ? $this->cookie[$offset] : null;
  180. }
  181. public function offsetSet($offset, $value)
  182. {
  183. if ($value === null) {
  184. unset($this->cookie[$offset]);
  185. } else {
  186. $this->cookie[$offset] = $value;
  187. }
  188. }
  189. public function offsetUnset($offset)
  190. {
  191. unset($this->cookie[$offset]);
  192. }
  193. /**
  194. * @param string $name
  195. */
  196. protected function validateCookieName($name)
  197. {
  198. if ($name === null || $name === '') {
  199. throw new InvalidArgumentException('Cookie name should be non-empty');
  200. }
  201. if (mb_strpos($name, ';') !== false) {
  202. throw new InvalidArgumentException('Cookie name should not contain a ";"');
  203. }
  204. }
  205. /**
  206. * @param string $value
  207. */
  208. protected function validateCookieValue($value)
  209. {
  210. if ($value === null) {
  211. throw new InvalidArgumentException('Cookie value is required when setting a cookie');
  212. }
  213. }
  214. }