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
 /**
  * Get the aggregator used to join multi-valued field parameters
  *
  * @return callable
  */
 protected final function getAggregator()
 {
     if (!$this->aggregator) {
         $this->aggregator = puzzle_Query::phpAggregator();
     }
     return $this->aggregator;
 }
Example #3
0
 public function testPhpEncodesNoNumericIndices()
 {
     $agg = puzzle_Query::phpAggregator(false);
     $result = call_user_func($agg, $this->encodeData);
     $this->assertEquals(array('t[v1][]' => array('a', '1'), 't[v2]' => array('b'), 't[v3][v4]' => array('c'), 't[v3][v5]' => array('d')), $result);
 }