PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Guzzle/Http/Cookie.php

http://github.com/guzzle/guzzle
PHP | 440 lines | 173 code | 41 blank | 226 comment | 9 complexity | 819ada5995e636bfb47bcf8ce5cac79f MD5 | raw file
  1. <?php
  2. namespace Guzzle\Http;
  3. /**
  4. * Set-Cookie object
  5. */
  6. class Cookie
  7. {
  8. /**
  9. * @var array Cookie data
  10. */
  11. protected $data;
  12. /**
  13. * @param array $data Array of cookie data provided by a Cookie parser
  14. */
  15. public function __construct(array $data = array())
  16. {
  17. static $defaults = array(
  18. 'name' => '',
  19. 'value' => '',
  20. 'domain' => '',
  21. 'path' => '/',
  22. 'expires' => null,
  23. 'max_age' => 0,
  24. 'comment' => null,
  25. 'comment_url' => null,
  26. 'port' => array(),
  27. 'version' => null,
  28. 'secure' => false,
  29. 'discard' => false,
  30. 'http_only' => false
  31. );
  32. $this->data = array_merge($defaults, $data);
  33. // Extract the expires value and turn it into a UNIX timestamp if needed
  34. if (!$this->getExpires() && $this->getMaxAge()) {
  35. // Calculate the expires date
  36. $this->setExpires(time() + (int) $this->getMaxAge());
  37. } elseif ($this->getExpires() && !is_numeric($this->getExpires())) {
  38. $this->setExpires(strtotime($this->getExpires()));
  39. }
  40. }
  41. /**
  42. * Get the cookie as an array
  43. *
  44. * @return array
  45. */
  46. public function toArray()
  47. {
  48. return $this->data;
  49. }
  50. /**
  51. * Get the cookie name
  52. *
  53. * @return string
  54. */
  55. public function getName()
  56. {
  57. return $this->data['name'];
  58. }
  59. /**
  60. * Set the cookie name
  61. *
  62. * @param string $name Cookie name
  63. *
  64. * @return Cookie
  65. */
  66. public function setName($name)
  67. {
  68. return $this->setData('name', $name);
  69. }
  70. /**
  71. * Get the cookie value
  72. *
  73. * @return string
  74. */
  75. public function getValue()
  76. {
  77. return $this->data['value'];
  78. }
  79. /**
  80. * Set the cookie value
  81. *
  82. * @param string $value Cookie value
  83. *
  84. * @return Cookie
  85. */
  86. public function setValue($value)
  87. {
  88. return $this->setData('value', $value);
  89. }
  90. /**
  91. * Get the domain
  92. *
  93. * @return string|null
  94. */
  95. public function getDomain()
  96. {
  97. return $this->data['domain'];
  98. }
  99. /**
  100. * Set the domain of the cookie
  101. *
  102. * @param string $domain
  103. *
  104. * @return Cookie
  105. */
  106. public function setDomain($domain)
  107. {
  108. return $this->setData('domain', $domain);
  109. }
  110. /**
  111. * Get the path
  112. *
  113. * @return string
  114. */
  115. public function getPath()
  116. {
  117. return $this->data['path'];
  118. }
  119. /**
  120. * Set the path of the cookie
  121. *
  122. * @param string $path Path of the cookie
  123. *
  124. * @return Cookie
  125. */
  126. public function setPath($path)
  127. {
  128. return $this->setData('path', $path);
  129. }
  130. /**
  131. * Maximum lifetime of the cookie in seconds
  132. *
  133. * @return int|null
  134. */
  135. public function getMaxAge()
  136. {
  137. return $this->data['max_age'];
  138. }
  139. /**
  140. * Set the max-age of the cookie
  141. *
  142. * @param int $maxAge Max age of the cookie in seconds
  143. *
  144. * @return Cookie
  145. */
  146. public function setMaxAge($maxAge)
  147. {
  148. return $this->setData('max_age', $maxAge);
  149. }
  150. /**
  151. * The UNIX timestamp when the cookie expires
  152. *
  153. * @return mixed
  154. */
  155. public function getExpires()
  156. {
  157. return $this->data['expires'];
  158. }
  159. /**
  160. * Set the unix timestamp for which the cookie will expire
  161. *
  162. * @param int $timestamp Unix timestamp
  163. *
  164. * @return Cookie
  165. */
  166. public function setExpires($timestamp)
  167. {
  168. return $this->setData('expires', $timestamp);
  169. }
  170. /**
  171. * Version of the cookie specification. RFC 2965 is 1
  172. *
  173. * @return mixed
  174. */
  175. public function getVersion()
  176. {
  177. return $this->data['version'];
  178. }
  179. /**
  180. * Set the cookie version
  181. *
  182. * @param string|int $version Version to set
  183. *
  184. * @return Cookie
  185. */
  186. public function setVersion($version)
  187. {
  188. return $this->setData('version', $version);
  189. }
  190. /**
  191. * Get whether or not this is a secure cookie
  192. *
  193. * @return null|bool
  194. */
  195. public function getSecure()
  196. {
  197. return $this->data['secure'];
  198. }
  199. /**
  200. * Set whether or not the cookie is secure
  201. *
  202. * @param bool $secure Set to true or false if secure
  203. *
  204. * @return Cookie
  205. */
  206. public function setSecure($secure)
  207. {
  208. return $this->setData('secure', (bool) $secure);
  209. }
  210. /**
  211. * Get whether or not this is a session cookie
  212. *
  213. * @return null|bool
  214. */
  215. public function getDiscard()
  216. {
  217. return $this->data['discard'];
  218. }
  219. /**
  220. * Set whether or not this is a session cookie
  221. *
  222. * @param bool $discard Set to true or false if this is a session cookie
  223. *
  224. * @return Cookie
  225. */
  226. public function setDiscard($discard)
  227. {
  228. return $this->setData('discard', $discard);
  229. }
  230. /**
  231. * Get the comment
  232. *
  233. * @return string|null
  234. */
  235. public function getComment()
  236. {
  237. return $this->data['comment'];
  238. }
  239. /**
  240. * Set the comment of the cookie
  241. *
  242. * @param string $comment Cookie comment
  243. *
  244. * @return Cookie
  245. */
  246. public function setComment($comment)
  247. {
  248. return $this->setData('comment', $comment);
  249. }
  250. /**
  251. * Get the comment URL of the cookie
  252. *
  253. * @return string|null
  254. */
  255. public function getCommentUrl()
  256. {
  257. return $this->data['comment_url'];
  258. }
  259. /**
  260. * Set the comment URL of the cookie
  261. *
  262. * @param string $commentUrl Cookie comment URL for more information
  263. *
  264. * @return Cookie
  265. */
  266. public function setCommentUrl($commentUrl)
  267. {
  268. return $this->setData('comment_url', $commentUrl);
  269. }
  270. /**
  271. * Get an array of acceptable ports this cookie can be used with
  272. *
  273. * @return array
  274. */
  275. public function getPorts()
  276. {
  277. return $this->data['port'];
  278. }
  279. /**
  280. * Set a list of acceptable ports this cookie can be used with
  281. *
  282. * @param array $ports Array of acceptable ports
  283. *
  284. * @return Cookie
  285. */
  286. public function setPorts(array $ports)
  287. {
  288. return $this->setData('port', $ports);
  289. }
  290. /**
  291. * Get whether or not this is an HTTP only cookie
  292. *
  293. * @return bool
  294. */
  295. public function getHttpOnly()
  296. {
  297. return $this->data['http_only'];
  298. }
  299. /**
  300. * Set whether or not this is an HTTP only cookie
  301. *
  302. * @param bool $httpOnly Set to true or false if this is HTTP only
  303. *
  304. * @return Cookie
  305. */
  306. public function setHttpOnly($httpOnly)
  307. {
  308. return $this->setData('http_only', $httpOnly);
  309. }
  310. /**
  311. * Get an array of extra cookie data
  312. *
  313. * @return array
  314. */
  315. public function getAttributes()
  316. {
  317. return $this->data['data'];
  318. }
  319. /**
  320. * Get a specific data point from the extra cookie data
  321. *
  322. * @param $name Name of the data point to retrieve
  323. *
  324. * @return null|string
  325. */
  326. public function getAttribute($name)
  327. {
  328. return array_key_exists($name, $this->data['data']) ? $this->data['data'][$name] : null;
  329. }
  330. /**
  331. * Set a cookie data attribute
  332. *
  333. * @param string $name Name of the attribute to set
  334. * @param string $value Value to set
  335. *
  336. * @return Cookie
  337. */
  338. public function setAttribute($name, $value)
  339. {
  340. $this->data['data'][$name] = $value;
  341. return $this;
  342. }
  343. /**
  344. * Check if the cookie matches a path value
  345. *
  346. * @param string $path Path to check against
  347. *
  348. * @return bool
  349. */
  350. public function matchesPath($path)
  351. {
  352. return !$this->getPath() || 0 === stripos($path, $this->getPath());
  353. }
  354. /**
  355. * Check if the cookie matches a domain value
  356. *
  357. * @param string $domain Domain to check against
  358. *
  359. * @return bool
  360. */
  361. public function matchesDomain($domain)
  362. {
  363. return !$this->getDomain() || !strcasecmp($domain, $this->getDomain()) ||
  364. (strpos($this->getDomain(), '.') === 0 && preg_match('/' . preg_quote($this->getDomain()) . '$/i', $domain));
  365. }
  366. /**
  367. * Check if the cookie is compatible with a specific port
  368. *
  369. * @param int $port Port to check
  370. *
  371. * @return bool
  372. */
  373. public function matchesPort($port)
  374. {
  375. return count($this->getPorts()) == 0 || in_array($port, $this->getPorts());
  376. }
  377. /**
  378. * Check if the cookie is expired
  379. *
  380. * @return bool
  381. */
  382. public function isExpired()
  383. {
  384. return $this->getExpires() && time() > $this->getExpires();
  385. }
  386. /**
  387. * Set a value and return the cookie object
  388. *
  389. * @param string $key Key to set
  390. * @param string $value Value to set
  391. *
  392. * @return Cookie
  393. */
  394. private function setData($key, $value)
  395. {
  396. $this->data[$key] = $value;
  397. return $this;
  398. }
  399. }