wp/wp-includes/Requests/Response/Headers.php
author ymh <ymh.work@gmail.com>
Thu, 29 Sep 2022 08:06:27 +0200
changeset 20 7b1b88e27a20
parent 18 be944660c56a
permissions -rw-r--r--
Modified Readme
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Case-insensitive dictionary, suitable for HTTP headers
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package Requests
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * Case-insensitive dictionary, suitable for HTTP headers
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * @package Requests
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
	 * Get the given header
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
	 * Unlike {@see self::getValues()}, this returns a string. If there are
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	 * multiple values, it concatenates them with a comma as per RFC2616.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
	 * Avoid using this where commas may be used unquoted in values, such as
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
	 * Set-Cookie headers.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
	 * @param string $key
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    24
	 * @return string|null Header value
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	public function offsetGet($key) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
		$key = strtolower($key);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
		if (!isset($this->data[$key])) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
			return null;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		return $this->flatten($this->data[$key]);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	 * Set the given item
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
	 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	 * @param string $key Item name
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
	 * @param string $value Item value
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	public function offsetSet($key, $value) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		if ($key === null) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
			throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		$key = strtolower($key);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		if (!isset($this->data[$key])) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
			$this->data[$key] = array();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		$this->data[$key][] = $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
	 * Get all values for a given header
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	 * @param string $key
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    61
	 * @return array|null Header values
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	public function getValues($key) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		$key = strtolower($key);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		if (!isset($this->data[$key])) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
			return null;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
		return $this->data[$key];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	 * Flattens a value into a string
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	 * Converts an array into a string by imploding values with a comma, as per
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	 * RFC2616's rules for folding headers.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	 * @param string|array $value Value to flatten
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
	 * @return string Flattened value
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
	public function flatten($value) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		if (is_array($value)) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			$value = implode(',', $value);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
		return $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
	 * Get an iterator for the data
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
	 * Converts the internal
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
	 * @return ArrayIterator
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
	public function getIterator() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten'));
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
}