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