Cookie.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 'name' => null,
  29. 'value' => null,
  30. 'path' => null,
  31. 'domain' => null,
  32. 'expiry' => null,
  33. 'secure' => null,
  34. 'httpOnly' => null,
  35. ];
  36. /**
  37. * @param string $name The name of the cookie; may not be null or an empty string.
  38. * @param string $value The cookie value; may not be null.
  39. */
  40. public function __construct($name, $value)
  41. {
  42. $this->validateCookieName($name);
  43. $this->validateCookieValue($value);
  44. $this->cookie['name'] = $name;
  45. $this->cookie['value'] = $value;
  46. }
  47. /**
  48. * @param array $cookieArray
  49. * @return Cookie
  50. */
  51. public static function createFromArray(array $cookieArray)
  52. {
  53. $cookie = new self($cookieArray['name'], $cookieArray['value']);
  54. if (isset($cookieArray['path'])) {
  55. $cookie->setPath($cookieArray['path']);
  56. }
  57. if (isset($cookieArray['domain'])) {
  58. $cookie->setDomain($cookieArray['domain']);
  59. }
  60. if (isset($cookieArray['expiry'])) {
  61. $cookie->setExpiry($cookieArray['expiry']);
  62. }
  63. if (isset($cookieArray['secure'])) {
  64. $cookie->setSecure($cookieArray['secure']);
  65. }
  66. if (isset($cookieArray['httpOnly'])) {
  67. $cookie->setHttpOnly($cookieArray['httpOnly']);
  68. }
  69. return $cookie;
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function getName()
  75. {
  76. return $this->cookie['name'];
  77. }
  78. /**
  79. * @return string
  80. */
  81. public function getValue()
  82. {
  83. return $this->cookie['value'];
  84. }
  85. /**
  86. * The path the cookie is visible to. Defaults to "/" if omitted.
  87. *
  88. * @param string $path
  89. */
  90. public function setPath($path)
  91. {
  92. $this->cookie['path'] = $path;
  93. }
  94. /**
  95. * @return string|null
  96. */
  97. public function getPath()
  98. {
  99. return $this->cookie['path'];
  100. }
  101. /**
  102. * The domain the cookie is visible to. Defaults to the current browsing context's document's URL domain if omitted.
  103. *
  104. * @param string $domain
  105. */
  106. public function setDomain($domain)
  107. {
  108. if (mb_strpos($domain, ':') !== false) {
  109. throw new InvalidArgumentException(sprintf('Cookie domain "%s" should not contain a port', $domain));
  110. }
  111. $this->cookie['domain'] = $domain;
  112. }
  113. /**
  114. * @return string|null
  115. */
  116. public function getDomain()
  117. {
  118. return $this->cookie['domain'];
  119. }
  120. /**
  121. * The cookie's expiration date, specified in seconds since Unix Epoch.
  122. *
  123. * @param int $expiry
  124. */
  125. public function setExpiry($expiry)
  126. {
  127. $this->cookie['expiry'] = (int) $expiry;
  128. }
  129. /**
  130. * @return int|null
  131. */
  132. public function getExpiry()
  133. {
  134. return $this->cookie['expiry'];
  135. }
  136. /**
  137. * Whether this cookie requires a secure connection (https). Defaults to false if omitted.
  138. *
  139. * @param bool $secure
  140. */
  141. public function setSecure($secure)
  142. {
  143. $this->cookie['secure'] = $secure;
  144. }
  145. /**
  146. * @return bool|null
  147. */
  148. public function isSecure()
  149. {
  150. return $this->cookie['secure'];
  151. }
  152. /**
  153. * Whether the cookie is an HTTP only cookie. Defaults to false if omitted.
  154. *
  155. * @param bool $httpOnly
  156. */
  157. public function setHttpOnly($httpOnly)
  158. {
  159. $this->cookie['httpOnly'] = $httpOnly;
  160. }
  161. /**
  162. * @return bool|null
  163. */
  164. public function isHttpOnly()
  165. {
  166. return $this->cookie['httpOnly'];
  167. }
  168. /**
  169. * @return array
  170. */
  171. public function toArray()
  172. {
  173. return $this->cookie;
  174. }
  175. public function offsetExists($offset)
  176. {
  177. return isset($this->cookie[$offset]);
  178. }
  179. public function offsetGet($offset)
  180. {
  181. return $this->cookie[$offset];
  182. }
  183. public function offsetSet($offset, $value)
  184. {
  185. $this->cookie[$offset] = $value;
  186. }
  187. public function offsetUnset($offset)
  188. {
  189. unset($this->cookie[$offset]);
  190. }
  191. /**
  192. * @param string $name
  193. */
  194. protected function validateCookieName($name)
  195. {
  196. if ($name === null || $name === '') {
  197. throw new InvalidArgumentException('Cookie name should be non-empty');
  198. }
  199. if (mb_strpos($name, ';') !== false) {
  200. throw new InvalidArgumentException('Cookie name should not contain a ";"');
  201. }
  202. }
  203. /**
  204. * @param string $value
  205. */
  206. protected function validateCookieValue($value)
  207. {
  208. if ($value === null) {
  209. throw new InvalidArgumentException('Cookie value is required when setting a cookie');
  210. }
  211. }
  212. }