CookieJarInterface.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace GuzzleHttp\Cookie;
  3. use Psr\Http\Message\RequestInterface;
  4. use Psr\Http\Message\ResponseInterface;
  5. /**
  6. * Stores HTTP cookies.
  7. *
  8. * It extracts cookies from HTTP requests, and returns them in HTTP responses.
  9. * CookieJarInterface instances automatically expire contained cookies when
  10. * necessary. Subclasses are also responsible for storing and retrieving
  11. * cookies from a file, database, etc.
  12. *
  13. * @see https://docs.python.org/2/library/cookielib.html Inspiration
  14. * @extends \IteratorAggregate<SetCookie>
  15. */
  16. interface CookieJarInterface extends \Countable, \IteratorAggregate
  17. {
  18. /**
  19. * Create a request with added cookie headers.
  20. *
  21. * If no matching cookies are found in the cookie jar, then no Cookie
  22. * header is added to the request and the same request is returned.
  23. *
  24. * @param RequestInterface $request Request object to modify.
  25. *
  26. * @return RequestInterface returns the modified request.
  27. */
  28. public function withCookieHeader(RequestInterface $request): RequestInterface;
  29. /**
  30. * Extract cookies from an HTTP response and store them in the CookieJar.
  31. *
  32. * @param RequestInterface $request Request that was sent
  33. * @param ResponseInterface $response Response that was received
  34. */
  35. public function extractCookies(RequestInterface $request, ResponseInterface $response): void;
  36. /**
  37. * Sets a cookie in the cookie jar.
  38. *
  39. * @param SetCookie $cookie Cookie to set.
  40. *
  41. * @return bool Returns true on success or false on failure
  42. */
  43. public function setCookie(SetCookie $cookie): bool;
  44. /**
  45. * Remove cookies currently held in the cookie jar.
  46. *
  47. * Invoking this method without arguments will empty the whole cookie jar.
  48. * If given a $domain argument only cookies belonging to that domain will
  49. * be removed. If given a $domain and $path argument, cookies belonging to
  50. * the specified path within that domain are removed. If given all three
  51. * arguments, then the cookie with the specified name, path and domain is
  52. * removed.
  53. *
  54. * @param string|null $domain Clears cookies matching a domain
  55. * @param string|null $path Clears cookies matching a domain and path
  56. * @param string|null $name Clears cookies matching a domain, path, and name
  57. */
  58. public function clear(?string $domain = null, ?string $path = null, ?string $name = null): void;
  59. /**
  60. * Discard all sessions cookies.
  61. *
  62. * Removes cookies that don't have an expire field or a have a discard
  63. * field set to true. To be called when the user agent shuts down according
  64. * to RFC 2965.
  65. */
  66. public function clearSessionCookies(): void;
  67. /**
  68. * Converts the cookie jar to an array.
  69. */
  70. public function toArray(): array;
  71. }