Beispiel #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());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function __toString()
 {
     return $this->_delegate->__toString();
 }
Beispiel #3
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;
 }
 public function testCanChangeUrlEncodingDecodingToRfc3986()
 {
     $q = new tubepress_url_impl_puzzle_PuzzleBasedQuery(puzzle_Query::fromString('foo=bar%20baz', tubepress_url_impl_puzzle_PuzzleBasedQuery::RFC3986_ENCODING));
     $this->assertEquals('bar baz', $q->get('foo'));
     $this->assertEquals('foo=bar%20baz', (string) $q);
 }
Beispiel #5
0
 /**
  * {@inheritdoc}
  */
 public function setQuery($query)
 {
     $this->_assertNotFrozen();
     if ($query === null) {
         $this->_query = null;
         return $this;
     }
     if ($query instanceof tubepress_api_url_QueryInterface) {
         $this->_query = $query;
         return $this;
     }
     if (is_string($query)) {
         $puzzleQuery = puzzle_Query::fromString($query);
     } else {
         $puzzleQuery = new puzzle_Query($query);
     }
     $this->_query = new tubepress_url_impl_puzzle_PuzzleBasedQuery($puzzleQuery);
     return $this;
 }
Beispiel #6
0
 public function testConvertsPlusSymbolsToSpacesByDefault()
 {
     $query = puzzle_Query::fromString('var=foo+bar', true);
     $this->assertEquals('foo bar', $query->get('var'));
 }
 /**
  * @ticket https://github.com/guzzle/guzzle/issues/706
  */
 public function testDoesNotApplyPostBodyRightAway()
 {
     $factory = new puzzle_message_MessageFactory();
     $request = $factory->createRequest('POST', 'http://f.cn', array('body' => array('foo' => array('bar', 'baz'))));
     $this->assertEquals('', $request->getHeader('Content-Type'));
     $this->assertEquals('', $request->getHeader('Content-Length'));
     $request->getBody()->setAggregator(puzzle_Query::duplicateAggregator());
     $request->getBody()->applyRequestHeaders($request);
     $this->assertEquals('foo=bar&foo=baz', $request->getBody());
 }
Beispiel #8
0
 public function testCanSpecifyQueryAggregator()
 {
     $b = new puzzle_post_PostBody();
     $b->setField('foo', array('baz', 'bar'));
     $this->assertEquals('foo%5B0%5D=baz&foo%5B1%5D=bar', (string) $b);
     $b = new puzzle_post_PostBody();
     $b->setField('foo', array('baz', 'bar'));
     $agg = puzzle_Query::duplicateAggregator();
     $b->setAggregator($agg);
     $this->assertEquals('foo=baz&foo=bar', (string) $b);
 }
Beispiel #9
0
 public function testCanChangeUrlEncodingDecodingToRfc3986()
 {
     $q = puzzle_Query::fromString('foo=bar%20baz', puzzle_Query::RFC3986);
     $this->assertEquals('bar baz', $q['foo']);
     $this->assertEquals('foo=bar%20baz', (string) $q);
 }
Beispiel #10
0
 public function aggregate(array $data)
 {
     return puzzle_Query::walkQuery($data, '', array($this, '_walker'));
 }
Beispiel #11
0
 /**
  * Set the query part of the URL
  *
  * @param puzzle_Query|string|array $query Query string value to set. Can
  *     be a string that will be parsed into a puzzle_Query object, an array
  *     of key value pairs, or a puzzle_Query object.
  *
  * @return puzzle_Url
  * @throws InvalidArgumentException
  */
 public function setQuery($query)
 {
     if ($query instanceof puzzle_Query) {
         $this->query = $query;
     } elseif (is_string($query)) {
         $this->query = puzzle_Query::fromString($query);
     } elseif (is_array($query)) {
         $this->query = new puzzle_Query($query);
     } else {
         throw new InvalidArgumentException('Query must be a ' . 'QueryInterface, array, or string');
     }
     return $this;
 }