Authentication
6 months ago
Builder
6 months ago
Decorator
6 months ago
Encoding
6 months ago
Exception
3 years ago
Formatter
6 months ago
MessageFactory
6 months ago
RequestMatcher
6 months ago
Stream
6 months ago
StreamFactory
6 months ago
UriFactory
6 months ago
Authentication.php
6 months ago
Cookie.php
3 years ago
CookieJar.php
3 years ago
CookieUtil.php
3 years ago
Exception.php
3 years ago
Formatter.php
6 months ago
RequestMatcher.php
6 months ago
filters.php
7 years ago
CookieJar.php
221 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaHttp\Message; |
| 4 | |
| 5 | /** |
| 6 | * Cookie Jar holds a set of Cookies. |
| 7 | * |
| 8 | * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> |
| 9 | */ |
| 10 | final class CookieJar implements \Countable, \IteratorAggregate |
| 11 | { |
| 12 | /** |
| 13 | * @var \SplObjectStorage |
| 14 | */ |
| 15 | protected $cookies; |
| 16 | |
| 17 | public function __construct() |
| 18 | { |
| 19 | $this->cookies = new \SplObjectStorage(); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Checks if there is a cookie. |
| 24 | * |
| 25 | * @param Cookie $cookie |
| 26 | * |
| 27 | * @return bool |
| 28 | */ |
| 29 | public function hasCookie(Cookie $cookie) |
| 30 | { |
| 31 | return $this->cookies->contains($cookie); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Adds a cookie. |
| 36 | * |
| 37 | * @param Cookie $cookie |
| 38 | */ |
| 39 | public function addCookie(Cookie $cookie) |
| 40 | { |
| 41 | if (!$this->hasCookie($cookie)) { |
| 42 | $cookies = $this->getMatchingCookies($cookie); |
| 43 | |
| 44 | foreach ($cookies as $matchingCookie) { |
| 45 | if ($cookie->getValue() !== $matchingCookie->getValue() || $cookie->getMaxAge() > $matchingCookie->getMaxAge()) { |
| 46 | $this->removeCookie($matchingCookie); |
| 47 | |
| 48 | continue; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if ($cookie->hasValue()) { |
| 53 | $this->cookies->attach($cookie); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Removes a cookie. |
| 60 | * |
| 61 | * @param Cookie $cookie |
| 62 | */ |
| 63 | public function removeCookie(Cookie $cookie) |
| 64 | { |
| 65 | $this->cookies->detach($cookie); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the cookies. |
| 70 | * |
| 71 | * @return Cookie[] |
| 72 | */ |
| 73 | public function getCookies() |
| 74 | { |
| 75 | $match = function ($matchCookie) { |
| 76 | return true; |
| 77 | }; |
| 78 | |
| 79 | return $this->findMatchingCookies($match); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns all matching cookies. |
| 84 | * |
| 85 | * @param Cookie $cookie |
| 86 | * |
| 87 | * @return Cookie[] |
| 88 | */ |
| 89 | public function getMatchingCookies(Cookie $cookie) |
| 90 | { |
| 91 | $match = function ($matchCookie) use ($cookie) { |
| 92 | return $matchCookie->match($cookie); |
| 93 | }; |
| 94 | |
| 95 | return $this->findMatchingCookies($match); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Finds matching cookies based on a callable. |
| 100 | * |
| 101 | * @param callable $match |
| 102 | * |
| 103 | * @return Cookie[] |
| 104 | */ |
| 105 | protected function findMatchingCookies(callable $match) |
| 106 | { |
| 107 | $cookies = []; |
| 108 | |
| 109 | foreach ($this->cookies as $cookie) { |
| 110 | if ($match($cookie)) { |
| 111 | $cookies[] = $cookie; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return $cookies; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Checks if there are cookies. |
| 120 | * |
| 121 | * @return bool |
| 122 | */ |
| 123 | public function hasCookies() |
| 124 | { |
| 125 | return $this->cookies->count() > 0; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Sets the cookies and removes any previous one. |
| 130 | * |
| 131 | * @param Cookie[] $cookies |
| 132 | */ |
| 133 | public function setCookies(array $cookies) |
| 134 | { |
| 135 | $this->clear(); |
| 136 | $this->addCookies($cookies); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Adds some cookies. |
| 141 | * |
| 142 | * @param Cookie[] $cookies |
| 143 | */ |
| 144 | public function addCookies(array $cookies) |
| 145 | { |
| 146 | foreach ($cookies as $cookie) { |
| 147 | $this->addCookie($cookie); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Removes some cookies. |
| 153 | * |
| 154 | * @param Cookie[] $cookies |
| 155 | */ |
| 156 | public function removeCookies(array $cookies) |
| 157 | { |
| 158 | foreach ($cookies as $cookie) { |
| 159 | $this->removeCookie($cookie); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Removes cookies which match the given parameters. |
| 165 | * |
| 166 | * Null means that parameter should not be matched |
| 167 | * |
| 168 | * @param string|null $name |
| 169 | * @param string|null $domain |
| 170 | * @param string|null $path |
| 171 | */ |
| 172 | public function removeMatchingCookies($name = null, $domain = null, $path = null) |
| 173 | { |
| 174 | $match = function ($cookie) use ($name, $domain, $path) { |
| 175 | $match = true; |
| 176 | |
| 177 | if (isset($name)) { |
| 178 | $match = $match && ($cookie->getName() === $name); |
| 179 | } |
| 180 | |
| 181 | if (isset($domain)) { |
| 182 | $match = $match && $cookie->matchDomain($domain); |
| 183 | } |
| 184 | |
| 185 | if (isset($path)) { |
| 186 | $match = $match && $cookie->matchPath($path); |
| 187 | } |
| 188 | |
| 189 | return $match; |
| 190 | }; |
| 191 | |
| 192 | $cookies = $this->findMatchingCookies($match); |
| 193 | |
| 194 | $this->removeCookies($cookies); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Removes all cookies. |
| 199 | */ |
| 200 | public function clear() |
| 201 | { |
| 202 | $this->cookies = new \SplObjectStorage(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * {@inheritdoc} |
| 207 | */ |
| 208 | public function count() |
| 209 | { |
| 210 | return $this->cookies->count(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * {@inheritdoc} |
| 215 | */ |
| 216 | public function getIterator() |
| 217 | { |
| 218 | return clone $this->cookies; |
| 219 | } |
| 220 | } |
| 221 |