Changeset 52328 for trunk/src/wp-includes/Requests/Cookie/Jar.php
- Timestamp:
- 12/06/2021 09:29:00 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Cookie/Jar.php
r52244 r52328 3 3 * Cookie holder object 4 4 * 5 * @package Requests\Cookies 5 * @package Requests 6 * @subpackage Cookies 6 7 */ 7 8 namespace WpOrg\Requests\Cookie;9 10 use ArrayAccess;11 use ArrayIterator;12 use IteratorAggregate;13 use ReturnTypeWillChange;14 use WpOrg\Requests\Cookie;15 use WpOrg\Requests\Exception;16 use WpOrg\Requests\Exception\InvalidArgument;17 use WpOrg\Requests\HookManager;18 use WpOrg\Requests\Iri;19 use WpOrg\Requests\Response;20 8 21 9 /** 22 10 * Cookie holder object 23 11 * 24 * @package Requests\Cookies 12 * @package Requests 13 * @subpackage Cookies 25 14 */ 26 class Jar implements ArrayAccess, IteratorAggregate {15 class Requests_Cookie_Jar implements ArrayAccess, IteratorAggregate { 27 16 /** 28 17 * Actual item data … … 30 19 * @var array 31 20 */ 32 protected $cookies = [];21 protected $cookies = array(); 33 22 34 23 /** … … 36 25 * 37 26 * @param array $cookies Existing cookie values 38 *39 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array.40 27 */ 41 public function __construct($cookies = []) { 42 if (is_array($cookies) === false) { 43 throw InvalidArgument::create(1, '$cookies', 'array', gettype($cookies)); 44 } 45 28 public function __construct($cookies = array()) { 46 29 $this->cookies = $cookies; 47 30 } 48 31 49 32 /** 50 * Normalise cookie data into a \WpOrg\Requests\Cookie33 * Normalise cookie data into a Requests_Cookie 51 34 * 52 * @param string| \WpOrg\Requests\Cookie $cookie53 * @return \WpOrg\Requests\Cookie35 * @param string|Requests_Cookie $cookie 36 * @return Requests_Cookie 54 37 */ 55 public function normalize_cookie($cookie, $key = '') {56 if ($cookie instanceof Cookie) {38 public function normalize_cookie($cookie, $key = null) { 39 if ($cookie instanceof Requests_Cookie) { 57 40 return $cookie; 58 41 } 59 42 60 return Cookie::parse($cookie, $key); 43 return Requests_Cookie::parse($cookie, $key); 44 } 45 46 /** 47 * Normalise cookie data into a Requests_Cookie 48 * 49 * @codeCoverageIgnore 50 * @deprecated Use {@see Requests_Cookie_Jar::normalize_cookie} 51 * @return Requests_Cookie 52 */ 53 public function normalizeCookie($cookie, $key = null) { 54 return $this->normalize_cookie($cookie, $key); 61 55 } 62 56 … … 64 58 * Check if the given item exists 65 59 * 66 * @param string $ offsetItem key60 * @param string $key Item key 67 61 * @return boolean Does the item exist? 68 62 */ 69 #[ReturnTypeWillChange] 70 public function offsetExists($offset) { 71 return isset($this->cookies[$offset]); 63 public function offsetExists($key) { 64 return isset($this->cookies[$key]); 72 65 } 73 66 … … 75 68 * Get the value for the item 76 69 * 77 * @param string $ offsetItem key70 * @param string $key Item key 78 71 * @return string|null Item value (null if offsetExists is false) 79 72 */ 80 #[ReturnTypeWillChange] 81 public function offsetGet($offset) { 82 if (!isset($this->cookies[$offset])) { 73 public function offsetGet($key) { 74 if (!isset($this->cookies[$key])) { 83 75 return null; 84 76 } 85 77 86 return $this->cookies[$ offset];78 return $this->cookies[$key]; 87 79 } 88 80 … … 90 82 * Set the given item 91 83 * 92 * @param string $offset Item name 84 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) 85 * 86 * @param string $key Item name 93 87 * @param string $value Item value 94 *95 * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`)96 88 */ 97 #[ReturnTypeWillChange] 98 public function offsetSet($offset, $value) { 99 if ($offset === null) { 100 throw new Exception('Object is a dictionary, not a list', 'invalidset'); 89 public function offsetSet($key, $value) { 90 if ($key === null) { 91 throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset'); 101 92 } 102 93 103 $this->cookies[$ offset] = $value;94 $this->cookies[$key] = $value; 104 95 } 105 96 … … 107 98 * Unset the given header 108 99 * 109 * @param string $ offset100 * @param string $key 110 101 */ 111 #[ReturnTypeWillChange] 112 public function offsetUnset($offset) { 113 unset($this->cookies[$offset]); 102 public function offsetUnset($key) { 103 unset($this->cookies[$key]); 114 104 } 115 105 … … 117 107 * Get an iterator for the data 118 108 * 119 * @return \ArrayIterator109 * @return ArrayIterator 120 110 */ 121 #[ReturnTypeWillChange]122 111 public function getIterator() { 123 112 return new ArrayIterator($this->cookies); … … 127 116 * Register the cookie handler with the request's hooking system 128 117 * 129 * @param \WpOrg\Requests\HookManager $hooks Hooking system118 * @param Requests_Hooker $hooks Hooking system 130 119 */ 131 public function register( HookManager $hooks) {132 $hooks->register('requests.before_request', [$this, 'before_request']);133 $hooks->register('requests.before_redirect_check', [$this, 'before_redirect_check']);120 public function register(Requests_Hooker $hooks) { 121 $hooks->register('requests.before_request', array($this, 'before_request')); 122 $hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check')); 134 123 } 135 124 … … 146 135 */ 147 136 public function before_request($url, &$headers, &$data, &$type, &$options) { 148 if (!$url instanceof Iri) {149 $url = new Iri($url);137 if (!$url instanceof Requests_IRI) { 138 $url = new Requests_IRI($url); 150 139 } 151 140 152 141 if (!empty($this->cookies)) { 153 $cookies = [];142 $cookies = array(); 154 143 foreach ($this->cookies as $key => $cookie) { 155 144 $cookie = $this->normalize_cookie($cookie, $key); … … 172 161 * Parse all cookies from a response and attach them to the response 173 162 * 174 * @ param \WpOrg\Requests\Response $response163 * @var Requests_Response $response 175 164 */ 176 public function before_redirect_check(Re sponse $response) {177 $url = $re sponse->url;178 if (!$url instanceof Iri) {179 $url = new Iri($url);165 public function before_redirect_check(Requests_Response $return) { 166 $url = $return->url; 167 if (!$url instanceof Requests_IRI) { 168 $url = new Requests_IRI($url); 180 169 } 181 170 182 $cookies = Cookie::parse_from_headers($response->headers, $url);183 $this->cookies 184 $re sponse->cookies = $this;171 $cookies = Requests_Cookie::parse_from_headers($return->headers, $url); 172 $this->cookies = array_merge($this->cookies, $cookies); 173 $return->cookies = $this; 185 174 } 186 175 }
Note: See TracChangeset
for help on using the changeset viewer.