From ebca7c662d891d7dc1457faf15b3cb442777345e Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Tue, 26 Mar 2013 12:33:44 +0100 Subject: [PATCH] Contacts: PoC request. --- appinfo/app.php | 1 + lib/request.php | 195 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 lib/request.php diff --git a/appinfo/app.php b/appinfo/app.php index 57478936..bbfb5453 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -8,6 +8,7 @@ OC::$CLASSPATH['OCA\Contacts\PIMCollectionAbstract'] = 'contacts/lib/abstractpim OC::$CLASSPATH['OCA\Contacts\PIMObjectAbstract'] = 'contacts/lib/abstractpimobject.php'; OC::$CLASSPATH['OCA\Contacts\VCard'] = 'contacts/lib/vcard.php'; OC::$CLASSPATH['OCA\Contacts\Hooks'] = 'contacts/lib/hooks.php'; +OC::$CLASSPATH['OCA\Contacts\Request'] = 'contacts/lib/request.php'; OC::$CLASSPATH['OCA\Contacts\Utils\JSONSerializer'] = 'contacts/lib/utils/jsonserializer.php'; OC::$CLASSPATH['OCA\Contacts\Utils\Properties'] = 'contacts/lib/utils/properties.php'; OC::$CLASSPATH['OCA\Contacts\Backend\AbstractBackend'] = 'contacts/lib/backend/abstractbackend.php'; diff --git a/lib/request.php b/lib/request.php new file mode 100644 index 00000000..25a3ce20 --- /dev/null +++ b/lib/request.php @@ -0,0 +1,195 @@ +. + * + */ + +namespace OCA\Contacts; + +/** + * Class for accessing variables in the request. + * This class provides an immutable object with request variables. + */ + +class Request extends OC_Request implements \ArrayAccess { + + protected $items = array(); + + /** + * @param array $vars And associative array with the following optional values: + * @param array 'urlParams' the parameters which were matched from the URL + * @param array 'get' the $_GET array + * @param array 'post' the $_POST array + * @param array 'files' the $_FILES array + * @param array 'server' the $_SERVER array + * @param array 'env' the $_ENV array + * @param array 'session' the $_SESSION array + * @param array 'cookies' the $_COOKIE array + * @see http://www.php.net/manual/en/reserved.variables.php + */ + public function __construct(array $vars) { + + foreach($vars as $name => $var) { + switch(strtolower($name)) { + case 'get': + $this->items[$name] = $var ? $var : $_GET; + break; + case 'post': + $this->items[$name] = $var ? $var : $_POST; + break; + case 'files': + $this->items[$name] = $var ? $var : $_FILES; + break; + case 'server': + $this->items[$name] = $var ? $var : $_SERVER; + break; + case 'env': + $this->items[$name] = $var ? $var : $_ENV; + break; + case 'session': + $this->items[$name] = $var ? $var : $_SESSION; + break; + case 'cookies': + $this->items[$name] = $var ? $var : $_COOKIE; + break; + case 'urlParams': + $this->items[$name] = $var ? $var : array(); + break; + } + } + + $this->items['parameters'] = array_merge( + $this->items['urlParams'], + $_REQUEST + ); + } + + /** + * ArrayAccess methods + * + * Gives access to the combined GET, POST and urlParams arrays + * + * Examples: + * + * $var = $request['myvar']; + * + * or + * + * if(!isset($request['myvar']) { + * // Do something + * } + * + * $request['myvar'] = 'something'; // This throws an exception. + * + * @param string offset The key to lookup + * @return string|null + */ + public function offsetExists($offset) { + return isset($this->items['parameters'][$offset]); + } + + /** + * @see offsetExists + */ + public function offsetGet($offset) { + return isset($this->items['parameters'][$offset]) + ? $this->items['parameters'][$offset] + : null; + } + + /** + * @see offsetExists + */ + public function offsetSet($offset, $value) { + throw new \Exception('You cannot change the contents of the request object'); + } + + /** + * @see offsetExists + */ + public function offsetUnset($offset) { + throw new \Exception('You cannot change the contents of the request object'); + } + + /** + * Get a value from a request variable or the default value e.g: + * $request->get('post', 'some_key', 'default value'); + * + * @param string $vars Which variables to look in e.g. 'get', 'post, 'session' + */ + public function get($vars, $name, $default = null) { + if(isset($this->{$vars}) && isset($this->{$vars}[$name])) { + return $this->{$vars}[$name]; + } + return $default; + } + + // Magic property accessors + public function __set($name, $value) { + throw new \Exception('You cannot change the contents of the request object'); + } + + /** + * Access request variables by method and name. + * Examples: + * + * $request->post['myvar']; // Only look for POST variables + * $request->myvar; or $request->{'myvar'}; or $request->{$myvar} + * Looks in the combined GET, POST and urlParams array. + * + * if($request->method !== 'POST') { + * throw new Exception('This function can only be invoked using POST'); + * } + * + * @param string $name The key to look for. + * @return mixed|null + */ + public function __get($name) { + switch(strtolower($name)) { + case 'get': + case 'post': + case 'files': + case 'server': + case 'env': + case 'session': + case 'cookies': + case 'parameters': + case 'urlParams': + return isset($this->items[$name]) + ? $this->items[$name] + : null; + break; + case 'method': + return $this->server['REQUEST_METHOD']; + break; + default; + return $this->parameters[$name]; + break; + } + } + + public function __isset($name) { + return isset($this->items['parameters'][$name]); + } + + public function __unset($id) { + throw new \Exception('You cannot change the contents of the request object'); + } + +}