You are here

private function UriTemplate::expandMatch in Auth0 Single Sign On 8.2

Process an expansion

Parameters

array $matches Matches met in the preg_replace_callback:

Return value

string Returns the replacement string

File

vendor/guzzlehttp/guzzle/src/UriTemplate.php, line 99

Class

UriTemplate
Expands URI templates. Userland implementation of PECL uri_template.

Namespace

GuzzleHttp

Code

private function expandMatch(array $matches) {
  static $rfc1738to3986 = [
    '+' => '%20',
    '%7e' => '~',
  ];
  $replacements = [];
  $parsed = self::parseExpression($matches[1]);
  $prefix = self::$operatorHash[$parsed['operator']]['prefix'];
  $joiner = self::$operatorHash[$parsed['operator']]['joiner'];
  $useQuery = self::$operatorHash[$parsed['operator']]['query'];
  foreach ($parsed['values'] as $value) {
    if (!isset($this->variables[$value['value']])) {
      continue;
    }
    $variable = $this->variables[$value['value']];
    $actuallyUseQuery = $useQuery;
    $expanded = '';
    if (is_array($variable)) {
      $isAssoc = $this
        ->isAssoc($variable);
      $kvp = [];
      foreach ($variable as $key => $var) {
        if ($isAssoc) {
          $key = rawurlencode($key);
          $isNestedArray = is_array($var);
        }
        else {
          $isNestedArray = false;
        }
        if (!$isNestedArray) {
          $var = rawurlencode($var);
          if ($parsed['operator'] === '+' || $parsed['operator'] === '#') {
            $var = $this
              ->decodeReserved($var);
          }
        }
        if ($value['modifier'] === '*') {
          if ($isAssoc) {
            if ($isNestedArray) {

              // Nested arrays must allow for deeply nested
              // structures.
              $var = strtr(http_build_query([
                $key => $var,
              ]), $rfc1738to3986);
            }
            else {
              $var = $key . '=' . $var;
            }
          }
          elseif ($key > 0 && $actuallyUseQuery) {
            $var = $value['value'] . '=' . $var;
          }
        }
        $kvp[$key] = $var;
      }
      if (empty($variable)) {
        $actuallyUseQuery = false;
      }
      elseif ($value['modifier'] === '*') {
        $expanded = implode($joiner, $kvp);
        if ($isAssoc) {

          // Don't prepend the value name when using the explode
          // modifier with an associative array.
          $actuallyUseQuery = false;
        }
      }
      else {
        if ($isAssoc) {

          // When an associative array is encountered and the
          // explode modifier is not set, then the result must be
          // a comma separated list of keys followed by their
          // respective values.
          foreach ($kvp as $k => &$v) {
            $v = $k . ',' . $v;
          }
        }
        $expanded = implode(',', $kvp);
      }
    }
    else {
      if ($value['modifier'] === ':') {
        $variable = substr($variable, 0, $value['position']);
      }
      $expanded = rawurlencode($variable);
      if ($parsed['operator'] === '+' || $parsed['operator'] === '#') {
        $expanded = $this
          ->decodeReserved($expanded);
      }
    }
    if ($actuallyUseQuery) {
      if (!$expanded && $joiner !== '&') {
        $expanded = $value['value'];
      }
      else {
        $expanded = $value['value'] . '=' . $expanded;
      }
    }
    $replacements[] = $expanded;
  }
  $ret = implode($joiner, $replacements);
  if ($ret && $prefix) {
    return $prefix . $ret;
  }
  return $ret;
}