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

QueryParser.php

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


 001  <?php
 002  namespace GuzzleHttp;
 003   
 004  /**
 005   * Parses query strings into a Query object.
 006   *
 007   * While parsing, the parser will attempt to determine the most appropriate
 008   * query string aggregator to use when serializing the parsed query string
 009   * object back into a string. The hope is that parsing then serializing a
 010   * query string should be a lossless operation.
 011   *
 012   * @internal Use Query::fromString()
 013   */
 014  class QueryParser
 015  {
 016      private $duplicates;
 017      private $numericIndices;
 018   
 019      /**
 020       * Parse a query string into a Query object.
 021       *
 022       * @param Query       $query       Query object to populate
 023       * @param string      $str         Query string to parse
 024       * @param bool|string $urlEncoding How the query string is encoded
 025       */
 026      public function parseInto(Query $query, $str, $urlEncoding = true)
 027      {
 028          if ($str === '') {
 029              return;
 030          }
 031   
 032          $result = [];
 033          $this->duplicates = false;
 034          $this->numericIndices = true;
 035          $decoder = self::getDecoder($urlEncoding);
 036   
 037          foreach (explode('&', $str) as $kvp) {
 038   
 039              $parts = explode('=', $kvp, 2);
 040              $key = $decoder($parts[0]);
 041              $value = isset($parts[1]) ? $decoder($parts[1]) : null;
 042   
 043              // Special handling needs to be taken for PHP nested array syntax
 044              if (strpos($key, '[') !== false) {
 045                  $this->parsePhpValue($key, $value, $result);
 046                  continue;
 047              }
 048   
 049              if (!isset($result[$key])) {
 050                  $result[$key] = $value;
 051              } else {
 052                  $this->duplicates = true;
 053                  if (!is_array($result[$key])) {
 054                      $result[$key] = [$result[$key]];
 055                  }
 056                  $result[$key][] = $value;
 057              }
 058          }
 059   
 060          $query->replace($result);
 061   
 062          if (!$this->numericIndices) {
 063              $query->setAggregator(Query::phpAggregator(false));
 064          } elseif ($this->duplicates) {
 065              $query->setAggregator(Query::duplicateAggregator());
 066          }
 067      }
 068   
 069      /**
 070       * Returns a callable that is used to URL decode query keys and values.
 071       *
 072       * @param string|bool $type One of true, false, RFC3986, and RFC1738
 073       *
 074       * @return callable|string
 075       */
 076      private static function getDecoder($type)
 077      {
 078          if ($type === true) {
 079              return function ($value) {
 080                  return rawurldecode(str_replace('+', ' ', $value));
 081              };
 082          } elseif ($type == Query::RFC3986) {
 083              return 'rawurldecode';
 084          } elseif ($type == Query::RFC1738) {
 085              return 'urldecode';
 086          } else {
 087              return function ($str) { return $str; };
 088          }
 089      }
 090   
 091      /**
 092       * Parses a PHP style key value pair.
 093       *
 094       * @param string      $key    Key to parse (e.g., "foo[a][b]")
 095       * @param string|null $value  Value to set
 096       * @param array       $result Result to modify by reference
 097       */
 098      private function parsePhpValue($key, $value, array &$result)
 099      {
 100          $node =& $result;
 101          $keyBuffer = '';
 102   
 103          for ($i = 0, $t = strlen($key); $i < $t; $i++) {
 104              switch ($key[$i]) {
 105                  case '[':
 106                      if ($keyBuffer) {
 107                          $this->prepareNode($node, $keyBuffer);
 108                          $node =& $node[$keyBuffer];
 109                          $keyBuffer = '';
 110                      }
 111                      break;
 112                  case ']':
 113                      $k = $this->cleanKey($node, $keyBuffer);
 114                      $this->prepareNode($node, $k);
 115                      $node =& $node[$k];
 116                      $keyBuffer = '';
 117                      break;
 118                  default:
 119                      $keyBuffer .= $key[$i];
 120                      break;
 121              }
 122          }
 123   
 124          if (isset($node)) {
 125              $this->duplicates = true;
 126              $node[] = $value;
 127          } else {
 128              $node = $value;
 129          }
 130      }
 131   
 132      /**
 133       * Prepares a value in the array at the given key.
 134       *
 135       * If the key already exists, the key value is converted into an array.
 136       *
 137       * @param array  $node Result node to modify
 138       * @param string $key  Key to add or modify in the node
 139       */
 140      private function prepareNode(&$node, $key)
 141      {
 142          if (!isset($node[$key])) {
 143              $node[$key] = null;
 144          } elseif (!is_array($node[$key])) {
 145              $node[$key] = [$node[$key]];
 146          }
 147      }
 148   
 149      /**
 150       * Returns the appropriate key based on the node and key.
 151       */
 152      private function cleanKey($node, $key)
 153      {
 154          if ($key === '') {
 155              $key = $node ? (string) count($node) : 0;
 156              // Found a [] key, so track this to ensure that we disable numeric
 157              // indexing of keys in the resolved query aggregator.
 158              $this->numericIndices = false;
 159          }
 160   
 161          return $key;
 162      }
 163  }
 164