Created
November 23, 2013 17:53
-
-
Save isdyy/7617759 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Non PHP like querystring parser / container | |
* | |
* param1=foo¶m2=bar¶m1=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