Skip to content

Instantly share code, notes, and snippets.

@isdyy
Created November 23, 2013 17:53
Show Gist options
  • Save isdyy/7617759 to your computer and use it in GitHub Desktop.
Save isdyy/7617759 to your computer and use it in GitHub Desktop.
<?php
/**
* Non PHP like querystring parser / container
*
* param1=foo&param2=bar&param1=baz
* => array([param1] => array(foo, baz), [param2] => array(bar)
*/
class QueryData
{
protected $_data;
public function __construct($querystr = null)
{
if (is_null($querystr)) {
$querystr = file_get_contents('php://input');
}
$data = array();
$params = explode('&', $querystr);
foreach ($params as $pair) {
list ($key, $value) = array_map('urldecode', explode('=', $pair));
if (!isset($data[$key])) {
$data[$key] = array();
}
$data[$key][] = $value;
}
$this->_data = $data;
}
public function get($name, $default='')
{
if (isset($this->_data[$name][0])) {
return $this->_data[$name][0];
}
return $default;
}
public function getlist($name)
{
if (isset($this->_data[$name])) {
return $this->_data[$name];
}
return array();
}
public function getmulti($names)
{
$res = array();
foreach ($names as $name) {
$res[] = $this->get($name);
}
return $res;
}
public function checked($name, $value)
{
return in_array($value, $this->getlist($name));
}
public function all()
{
return $this->_data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment