* @license http://www.opensource.org/licenses/lgpl-3.0.html LGPL * @link www.onlineabteilung.de */ class YourProjectName_Requests { /** * @var array $requests the requests */ private $requests; /** * @var array $origin the methods */ private $origin; static private $instance = null; static public function getInstance() { if (self::$instance === null) { self::$instance = new self; } return self::$instance; } private function __construct(){} private function __clone(){} /** * collectRequests * collects requests from POST and GET methods * */ public function collectRequests() { // don't allow duplicate keys. POST overrides GET. $this->requests = array_merge($_GET,$_POST); foreach($_GET as $key => $value) $this->origin[$key] = 'get'; foreach($_POST as $key => $value) $this->origin[$key] = 'post'; } /** * getRequests * returns all requests * * @return array All requests */ public function getRequests() { return $this->requests; } /** * isRequests * returns true if the given key is part of the request, false if not * * @param string $key The key to search for in requests * @return boolean true if the given key is part of the request, false if not */ public function isRequest($key) { return isset($this->requests[$key]); } /** * isMethod * checks if the given key is part of the given method * * @param string $key the key to search for * @param string $method the method to search in * @return boolean true if given key is part of given method */ public function isMethod($key,$method) { return $this->isRequest($key) and $this->origin[$key] == $method; } /** * isPost * checks if the given key is part of the POST request * * @param string $key the key to search for * @return boolean true if given key is part of POST */ public function isPost($key) { return $this->isMethod($key,'post'); } /** * isGet * checks if the given key ist part of the GET request * * @param string $key the key to search for * @return boolean true if given key is part of GET */ public function isGet($key) { return $this->isMethod($key,'get'); } /** * get * returns the value of the given key, or null if it doesn't exist * * @param string $key the key to search for * @return mixed either the value as string, or null if the key wasn't found */ public function get($key) { return $this->isRequest($key) ? $this->requests[$key] : null; } /** * getIfMethod * returns the value of the given key, or null if it doesn't exist or isn't part of the given method * * @param string $key the key to search for * @param string $method the method to search in * @return mixed either the value as string, or null if the key wasn't found */ public function getIfMethod($key,$method) { return $this->isMethod($key,$method) ? $this->requests[$key] : null; } /** * getIfPost * returns the value of the given key, or null if it doesn't exist or isn't part of POST * * @param string $key the key to search for * @return mixed either the value as string, or null if the key wasn't found */ public function getIfPost($key) { return $this->getIfMethod($key,'post'); } /** * getIfGet * returns the value of the given key, or null if it doesn't exist or isn't part of GET * * @param string $key the key to search for * @return mixed either the value as string, or null if the key wasn't found */ public function getIfGet($key) { return $this->getIfMethod($key,'get'); } /** * set * sets the given key to the given value * * @param string $key the key to set * @param string $value the value to set the key to * @param string $method the method to apply the changes to. Optional, defaults to 'get' * @param boolean $override wether to override a value if it already exists. Optional, defaults to true * @return boolean true, if setting the variable was successful, false otherwise */ public function set($key, $value, $method = 'get', $override = true) { if(!$override and $this->isRequest($key)) return false; /* check if method is valid. * * NOTE: remove this check if you want to use * the requester to store and retrieve data * from non-existing methods. * E.g. set('key','value','fancyMethod') / getIfMethod('key','fancyMethod') */ $method = strtolower($method); if(strpos(' get post ',' '.$method.' ') === false) return false; $this->requests[$key] = $value; $this->origin[$key] = $method; return true; } /** * getMethod * returns the method of the given key, or null if it's not part of the request * * @param string $key the key to search for * @return mixed the method the key was associated with, or null if t wasn't part of the request */ public function getMethod($key) { if(!$this->isRequest($key)) return null; return $this->origin[$key]; } } ?>