Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/updraftp.../vendor/guzzleht.../guzzle/src
File: UriTemplate.php
<?php
[0] Fix | Delete
namespace GuzzleHttp;
[1] Fix | Delete
[2] Fix | Delete
/**
[3] Fix | Delete
* Expands URI templates. Userland implementation of PECL uri_template.
[4] Fix | Delete
*
[5] Fix | Delete
* @link http://tools.ietf.org/html/rfc6570
[6] Fix | Delete
*/
[7] Fix | Delete
class UriTemplate
[8] Fix | Delete
{
[9] Fix | Delete
/** @var string URI template */
[10] Fix | Delete
private $template;
[11] Fix | Delete
[12] Fix | Delete
/** @var array Variables to use in the template expansion */
[13] Fix | Delete
private $variables;
[14] Fix | Delete
[15] Fix | Delete
/** @var array Hash for quick operator lookups */
[16] Fix | Delete
private static $operatorHash = [
[17] Fix | Delete
'' => ['prefix' => '', 'joiner' => ',', 'query' => false],
[18] Fix | Delete
'+' => ['prefix' => '', 'joiner' => ',', 'query' => false],
[19] Fix | Delete
'#' => ['prefix' => '#', 'joiner' => ',', 'query' => false],
[20] Fix | Delete
'.' => ['prefix' => '.', 'joiner' => '.', 'query' => false],
[21] Fix | Delete
'/' => ['prefix' => '/', 'joiner' => '/', 'query' => false],
[22] Fix | Delete
';' => ['prefix' => ';', 'joiner' => ';', 'query' => true],
[23] Fix | Delete
'?' => ['prefix' => '?', 'joiner' => '&', 'query' => true],
[24] Fix | Delete
'&' => ['prefix' => '&', 'joiner' => '&', 'query' => true]
[25] Fix | Delete
];
[26] Fix | Delete
[27] Fix | Delete
/** @var array Delimiters */
[28] Fix | Delete
private static $delims = [':', '/', '?', '#', '[', ']', '@', '!', '$',
[29] Fix | Delete
'&', '\'', '(', ')', '*', '+', ',', ';', '='];
[30] Fix | Delete
[31] Fix | Delete
/** @var array Percent encoded delimiters */
[32] Fix | Delete
private static $delimsPct = ['%3A', '%2F', '%3F', '%23', '%5B', '%5D',
[33] Fix | Delete
'%40', '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C',
[34] Fix | Delete
'%3B', '%3D'];
[35] Fix | Delete
[36] Fix | Delete
public function expand($template, array $variables)
[37] Fix | Delete
{
[38] Fix | Delete
if (false === strpos($template, '{')) {
[39] Fix | Delete
return $template;
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
$this->template = $template;
[43] Fix | Delete
$this->variables = $variables;
[44] Fix | Delete
[45] Fix | Delete
return preg_replace_callback(
[46] Fix | Delete
'/\{([^\}]+)\}/',
[47] Fix | Delete
[$this, 'expandMatch'],
[48] Fix | Delete
$this->template
[49] Fix | Delete
);
[50] Fix | Delete
}
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* Parse an expression into parts
[54] Fix | Delete
*
[55] Fix | Delete
* @param string $expression Expression to parse
[56] Fix | Delete
*
[57] Fix | Delete
* @return array Returns an associative array of parts
[58] Fix | Delete
*/
[59] Fix | Delete
private function parseExpression($expression)
[60] Fix | Delete
{
[61] Fix | Delete
$result = [];
[62] Fix | Delete
[63] Fix | Delete
if (isset(self::$operatorHash[$expression[0]])) {
[64] Fix | Delete
$result['operator'] = $expression[0];
[65] Fix | Delete
$expression = substr($expression, 1);
[66] Fix | Delete
} else {
[67] Fix | Delete
$result['operator'] = '';
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
foreach (explode(',', $expression) as $value) {
[71] Fix | Delete
$value = trim($value);
[72] Fix | Delete
$varspec = [];
[73] Fix | Delete
if ($colonPos = strpos($value, ':')) {
[74] Fix | Delete
$varspec['value'] = substr($value, 0, $colonPos);
[75] Fix | Delete
$varspec['modifier'] = ':';
[76] Fix | Delete
$varspec['position'] = (int) substr($value, $colonPos + 1);
[77] Fix | Delete
} elseif (substr($value, -1) === '*') {
[78] Fix | Delete
$varspec['modifier'] = '*';
[79] Fix | Delete
$varspec['value'] = substr($value, 0, -1);
[80] Fix | Delete
} else {
[81] Fix | Delete
$varspec['value'] = (string) $value;
[82] Fix | Delete
$varspec['modifier'] = '';
[83] Fix | Delete
}
[84] Fix | Delete
$result['values'][] = $varspec;
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
return $result;
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* Process an expansion
[92] Fix | Delete
*
[93] Fix | Delete
* @param array $matches Matches met in the preg_replace_callback
[94] Fix | Delete
*
[95] Fix | Delete
* @return string Returns the replacement string
[96] Fix | Delete
*/
[97] Fix | Delete
private function expandMatch(array $matches)
[98] Fix | Delete
{
[99] Fix | Delete
static $rfc1738to3986 = ['+' => '%20', '%7e' => '~'];
[100] Fix | Delete
[101] Fix | Delete
$replacements = [];
[102] Fix | Delete
$parsed = self::parseExpression($matches[1]);
[103] Fix | Delete
$prefix = self::$operatorHash[$parsed['operator']]['prefix'];
[104] Fix | Delete
$joiner = self::$operatorHash[$parsed['operator']]['joiner'];
[105] Fix | Delete
$useQuery = self::$operatorHash[$parsed['operator']]['query'];
[106] Fix | Delete
[107] Fix | Delete
foreach ($parsed['values'] as $value) {
[108] Fix | Delete
if (!isset($this->variables[$value['value']])) {
[109] Fix | Delete
continue;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
$variable = $this->variables[$value['value']];
[113] Fix | Delete
$actuallyUseQuery = $useQuery;
[114] Fix | Delete
$expanded = '';
[115] Fix | Delete
[116] Fix | Delete
if (is_array($variable)) {
[117] Fix | Delete
$isAssoc = $this->isAssoc($variable);
[118] Fix | Delete
$kvp = [];
[119] Fix | Delete
foreach ($variable as $key => $var) {
[120] Fix | Delete
if ($isAssoc) {
[121] Fix | Delete
$key = rawurlencode($key);
[122] Fix | Delete
$isNestedArray = is_array($var);
[123] Fix | Delete
} else {
[124] Fix | Delete
$isNestedArray = false;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
if (!$isNestedArray) {
[128] Fix | Delete
$var = rawurlencode($var);
[129] Fix | Delete
if ($parsed['operator'] === '+' ||
[130] Fix | Delete
$parsed['operator'] === '#'
[131] Fix | Delete
) {
[132] Fix | Delete
$var = $this->decodeReserved($var);
[133] Fix | Delete
}
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
if ($value['modifier'] === '*') {
[137] Fix | Delete
if ($isAssoc) {
[138] Fix | Delete
if ($isNestedArray) {
[139] Fix | Delete
// Nested arrays must allow for deeply nested
[140] Fix | Delete
// structures.
[141] Fix | Delete
$var = strtr(
[142] Fix | Delete
http_build_query([$key => $var]),
[143] Fix | Delete
$rfc1738to3986
[144] Fix | Delete
);
[145] Fix | Delete
} else {
[146] Fix | Delete
$var = $key . '=' . $var;
[147] Fix | Delete
}
[148] Fix | Delete
} elseif ($key > 0 && $actuallyUseQuery) {
[149] Fix | Delete
$var = $value['value'] . '=' . $var;
[150] Fix | Delete
}
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
$kvp[$key] = $var;
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
if (empty($variable)) {
[157] Fix | Delete
$actuallyUseQuery = false;
[158] Fix | Delete
} elseif ($value['modifier'] === '*') {
[159] Fix | Delete
$expanded = implode($joiner, $kvp);
[160] Fix | Delete
if ($isAssoc) {
[161] Fix | Delete
// Don't prepend the value name when using the explode
[162] Fix | Delete
// modifier with an associative array.
[163] Fix | Delete
$actuallyUseQuery = false;
[164] Fix | Delete
}
[165] Fix | Delete
} else {
[166] Fix | Delete
if ($isAssoc) {
[167] Fix | Delete
// When an associative array is encountered and the
[168] Fix | Delete
// explode modifier is not set, then the result must be
[169] Fix | Delete
// a comma separated list of keys followed by their
[170] Fix | Delete
// respective values.
[171] Fix | Delete
foreach ($kvp as $k => &$v) {
[172] Fix | Delete
$v = $k . ',' . $v;
[173] Fix | Delete
}
[174] Fix | Delete
}
[175] Fix | Delete
$expanded = implode(',', $kvp);
[176] Fix | Delete
}
[177] Fix | Delete
} else {
[178] Fix | Delete
if ($value['modifier'] === ':') {
[179] Fix | Delete
$variable = substr($variable, 0, $value['position']);
[180] Fix | Delete
}
[181] Fix | Delete
$expanded = rawurlencode($variable);
[182] Fix | Delete
if ($parsed['operator'] === '+' || $parsed['operator'] === '#') {
[183] Fix | Delete
$expanded = $this->decodeReserved($expanded);
[184] Fix | Delete
}
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
if ($actuallyUseQuery) {
[188] Fix | Delete
if (!$expanded && $joiner !== '&') {
[189] Fix | Delete
$expanded = $value['value'];
[190] Fix | Delete
} else {
[191] Fix | Delete
$expanded = $value['value'] . '=' . $expanded;
[192] Fix | Delete
}
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
$replacements[] = $expanded;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
$ret = implode($joiner, $replacements);
[199] Fix | Delete
if ($ret && $prefix) {
[200] Fix | Delete
return $prefix . $ret;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
return $ret;
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
/**
[207] Fix | Delete
* Determines if an array is associative.
[208] Fix | Delete
*
[209] Fix | Delete
* This makes the assumption that input arrays are sequences or hashes.
[210] Fix | Delete
* This assumption is a tradeoff for accuracy in favor of speed, but it
[211] Fix | Delete
* should work in almost every case where input is supplied for a URI
[212] Fix | Delete
* template.
[213] Fix | Delete
*
[214] Fix | Delete
* @param array $array Array to check
[215] Fix | Delete
*
[216] Fix | Delete
* @return bool
[217] Fix | Delete
*/
[218] Fix | Delete
private function isAssoc(array $array)
[219] Fix | Delete
{
[220] Fix | Delete
return $array && array_keys($array)[0] !== 0;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/**
[224] Fix | Delete
* Removes percent encoding on reserved characters (used with + and #
[225] Fix | Delete
* modifiers).
[226] Fix | Delete
*
[227] Fix | Delete
* @param string $string String to fix
[228] Fix | Delete
*
[229] Fix | Delete
* @return string
[230] Fix | Delete
*/
[231] Fix | Delete
private function decodeReserved($string)
[232] Fix | Delete
{
[233] Fix | Delete
return str_replace(self::$delimsPct, self::$delims, $string);
[234] Fix | Delete
}
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function