Example #1
0
 /**
  * Parse a query string into a Query object.
  *
  * @param puzzle_Query $query       Query object to populate
  * @param string       $str         Query string to parse
  * @param bool|string  $urlEncoding How the query string is encoded
  */
 public function parseInto(puzzle_Query $query, $str, $urlEncoding = true)
 {
     if ($str === '') {
         return;
     }
     $result = array();
     $this->duplicates = false;
     $this->numericIndices = true;
     $decoder = self::getDecoder($urlEncoding);
     foreach (explode('&', $str) as $kvp) {
         $parts = explode('=', $kvp, 2);
         $key = call_user_func($decoder, $parts[0]);
         $value = isset($parts[1]) ? call_user_func($decoder, $parts[1]) : null;
         // Special handling needs to be taken for PHP nested array syntax
         if (strpos($key, '[') !== false) {
             $this->parsePhpValue($key, $value, $result);
             continue;
         }
         if (!isset($result[$key])) {
             $result[$key] = $value;
         } else {
             $this->duplicates = true;
             if (!is_array($result[$key])) {
                 $result[$key] = array($result[$key]);
             }
             $result[$key][] = $value;
         }
     }
     $query->replace($result);
     if (!$this->numericIndices) {
         $query->setAggregator(puzzle_Query::phpAggregator(false));
     } elseif ($this->duplicates) {
         $query->setAggregator(puzzle_Query::duplicateAggregator());
     }
 }
Example #2
0
 public function testAllowsMultipleValuesPerKey()
 {
     $q = new puzzle_Query();
     $q->add('facet', 'size');
     $q->add('facet', 'width');
     $q->add('facet.field', 'foo');
     // Use the duplicate aggregator
     $q->setAggregator($q->duplicateAggregator());
     $this->assertEquals('facet=size&facet=width&facet.field=foo', (string) $q);
 }