Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Query.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 5.93 KiB


 001  <?php
 002  namespace GuzzleHttp;
 003   
 004  /**
 005   * Manages query string variables and can aggregate them into a string
 006   */
 007  class Query extends Collection
 008  {
 009      const RFC3986 = 'RFC3986';
 010      const RFC1738 = 'RFC1738';
 011   
 012      /** @var callable Encoding function */
 013      private $encoding = 'rawurlencode';
 014      /** @var callable */
 015      private $aggregator;
 016   
 017      /**
 018       * Parse a query string into a Query object
 019       *
 020       * $urlEncoding is used to control how the query string is parsed and how
 021       * it is ultimately serialized. The value can be set to one of the
 022       * following:
 023       *
 024       * - true: (default) Parse query strings using RFC 3986 while still
 025       *   converting "+" to " ".
 026       * - false: Disables URL decoding of the input string and URL encoding when
 027       *   the query string is serialized.
 028       * - 'RFC3986': Use RFC 3986 URL encoding/decoding
 029       * - 'RFC1738': Use RFC 1738 URL encoding/decoding
 030       *
 031       * @param string      $query       Query string to parse
 032       * @param bool|string $urlEncoding Controls how the input string is decoded
 033       *                                 and encoded.
 034       * @return self
 035       */
 036      public static function fromString($query, $urlEncoding = true)
 037      {
 038          static $qp;
 039          if (!$qp) {
 040              $qp = new QueryParser();
 041          }
 042   
 043          $q = new static();
 044   
 045          if ($urlEncoding !== true) {
 046              $q->setEncodingType($urlEncoding);
 047          }
 048   
 049          $qp->parseInto($q, $query, $urlEncoding);
 050   
 051          return $q;
 052      }
 053   
 054      /**
 055       * Convert the query string parameters to a query string string
 056       *
 057       * @return string
 058       */
 059      public function __toString()
 060      {
 061          if (!$this->data) {
 062              return '';
 063          }
 064   
 065          // The default aggregator is statically cached
 066          static $defaultAggregator;
 067   
 068          if (!$this->aggregator) {
 069              if (!$defaultAggregator) {
 070                  $defaultAggregator = self::phpAggregator();
 071              }
 072              $this->aggregator = $defaultAggregator;
 073          }
 074   
 075          $result = '';
 076          $aggregator = $this->aggregator;
 077          $encoder = $this->encoding;
 078   
 079          foreach ($aggregator($this->data) as $key => $values) {
 080              foreach ($values as $value) {
 081                  if ($result) {
 082                      $result .= '&';
 083                  }
 084                  $result .= $encoder($key);
 085                  if ($value !== null) {
 086                      $result .= '=' . $encoder($value);
 087                  }
 088              }
 089          }
 090   
 091          return $result;
 092      }
 093   
 094      /**
 095       * Controls how multi-valued query string parameters are aggregated into a
 096       * string.
 097       *
 098       *     $query->setAggregator($query::duplicateAggregator());
 099       *
 100       * @param callable $aggregator Callable used to convert a deeply nested
 101       *     array of query string variables into a flattened array of key value
 102       *     pairs. The callable accepts an array of query data and returns a
 103       *     flattened array of key value pairs where each value is an array of
 104       *     strings.
 105       */
 106      public function setAggregator(callable $aggregator)
 107      {
 108          $this->aggregator = $aggregator;
 109      }
 110   
 111      /**
 112       * Specify how values are URL encoded
 113       *
 114       * @param string|bool $type One of 'RFC1738', 'RFC3986', or false to disable encoding
 115       *
 116       * @throws \InvalidArgumentException
 117       */
 118      public function setEncodingType($type)
 119      {
 120          switch ($type) {
 121              case self::RFC3986:
 122                  $this->encoding = 'rawurlencode';
 123                  break;
 124              case self::RFC1738:
 125                  $this->encoding = 'urlencode';
 126                  break;
 127              case false:
 128                  $this->encoding = function ($v) { return $v; };
 129                  break;
 130              default:
 131                  throw new \InvalidArgumentException('Invalid URL encoding type');
 132          }
 133      }
 134   
 135      /**
 136       * Query string aggregator that does not aggregate nested query string
 137       * values and allows duplicates in the resulting array.
 138       *
 139       * Example: http://test.com?q=1&q=2
 140       *
 141       * @return callable
 142       */
 143      public static function duplicateAggregator()
 144      {
 145          return function (array $data) {
 146              return self::walkQuery($data, '', function ($key, $prefix) {
 147                  return is_int($key) ? $prefix : "{$prefix}[{$key}]";
 148              });
 149          };
 150      }
 151   
 152      /**
 153       * Aggregates nested query string variables using the same technique as
 154       * ``http_build_query()``.
 155       *
 156       * @param bool $numericIndices Pass false to not include numeric indices
 157       *     when multi-values query string parameters are present.
 158       *
 159       * @return callable
 160       */
 161      public static function phpAggregator($numericIndices = true)
 162      {
 163          return function (array $data) use ($numericIndices) {
 164              return self::walkQuery(
 165                  $data,
 166                  '',
 167                  function ($key, $prefix) use ($numericIndices) {
 168                      return !$numericIndices && is_int($key)
 169                          ? "{$prefix}[]"
 170                          : "{$prefix}[{$key}]";
 171                  }
 172              );
 173          };
 174      }
 175   
 176      /**
 177       * Easily create query aggregation functions by providing a key prefix
 178       * function to this query string array walker.
 179       *
 180       * @param array    $query     Query string to walk
 181       * @param string   $keyPrefix Key prefix (start with '')
 182       * @param callable $prefixer  Function used to create a key prefix
 183       *
 184       * @return array
 185       */
 186      public static function walkQuery(array $query, $keyPrefix, callable $prefixer)
 187      {
 188          $result = [];
 189          foreach ($query as $key => $value) {
 190              if ($keyPrefix) {
 191                  $key = $prefixer($key, $keyPrefix);
 192              }
 193              if (is_array($value)) {
 194                  $result += self::walkQuery($value, $key, $prefixer);
 195              } elseif (isset($result[$key])) {
 196                  $result[$key][] = $value;
 197              } else {
 198                  $result[$key] = array($value);
 199              }
 200          }
 201   
 202          return $result;
 203      }
 204  }
 205