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