1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-10 21:24:33 +01:00
OwncloudContactsOfficial/lib/request.php

214 lines
5.7 KiB
PHP
Raw Normal View History

2013-03-26 12:33:44 +01:00
<?php
/**
* ownCloud - Request
*
* @author Thomas Tanghus
* @copyright 2013 Thomas Tanghus (thomas@tanghus.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Contacts;
/**
* Class for accessing variables in the request.
* This class provides an immutable object with request variables.
*/
class Request implements \ArrayAccess, \Countable {
2013-03-26 12:33:44 +01:00
protected $items = array();
protected $varnames = array('get', 'post', 'files', 'server', 'env', 'session', 'cookies', 'urlParams', 'params', 'parameters', 'method');
2013-03-26 12:33:44 +01:00
/**
* @param array $vars And associative array with the following optional values:
* @param array 'params' the parsed json array
2013-03-26 12:33:44 +01:00
* @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 = array()) {
2013-03-26 12:33:44 +01:00
foreach($this->varnames as $name) {
switch($name) {
2013-03-26 12:33:44 +01:00
case 'get':
$this->items[$name] = isset($vars[$name]) ? $vars[$name] : $_GET;
2013-03-26 12:33:44 +01:00
break;
case 'post':
$this->items[$name] = isset($vars[$name]) ? $vars[$name] : $_POST;
2013-03-26 12:33:44 +01:00
break;
case 'files':
$this->items[$name] = isset($vars[$name]) ? $vars[$name] : $_FILES;
2013-03-26 12:33:44 +01:00
break;
case 'server':
$this->items[$name] = isset($vars[$name]) ? $vars[$name] : $_SERVER;
2013-03-26 12:33:44 +01:00
break;
case 'env':
$this->items[$name] = isset($vars[$name]) ? $vars[$name] : $_ENV;
2013-03-26 12:33:44 +01:00
break;
case 'session':
$this->items[$name] = isset($vars[$name])
? $vars[$name]
: (isset($_SESSION) ? $_SESSION : array());
2013-03-26 12:33:44 +01:00
break;
case 'cookies':
$this->items[$name] = isset($vars[$name]) ? $vars[$name] : $_COOKIE;
break;
case 'method':
$this->items[$name] = isset($vars[$name])
? $vars[$name]
: (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : array());
2013-03-26 12:33:44 +01:00
break;
case 'urlParams':
case 'params':
$this->items[$name] = isset($vars[$name]) ? $vars[$name] : array();
2013-03-26 12:33:44 +01:00
break;
}
}
$this->items['parameters'] = array_merge(
$this->items['params'],
$this->items['get'],
$this->items['post'],
$this->items['urlParams']
2013-03-26 12:33:44 +01:00
);
}
// Countable method.
public function count() {
return count(array_keys($this->items['parameters']));
2013-03-26 12:33:44 +01:00
}
/**
* 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 \RuntimeException('You cannot change the contents of the request object');
2013-03-26 12:33:44 +01:00
}
/**
* @see offsetExists
*/
public function offsetUnset($offset) {
throw new \RuntimeException('You cannot change the contents of the request object');
2013-03-26 12:33:44 +01:00
}
/**
* 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'
* @param string $name
* @param string $default
2013-03-26 12:33:44 +01:00
*/
public function getVar($vars, $name, $default = null) {
return isset($this->{$vars}[$name]) ? $this->{$vars}[$name] : $default;
2013-03-26 12:33:44 +01:00
}
// Magic property accessors
public function __set($name, $value) {
throw new \RuntimeException('You cannot change the contents of the request object');
2013-03-26 12:33:44 +01:00
}
/**
* 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($name) {
2013-03-26 12:33:44 +01:00
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->items['method'];
2013-03-26 12:33:44 +01:00
break;
default;
return isset($this[$name]) ? $this[$name] : null;
2013-03-26 12:33:44 +01:00
break;
}
}
public function __isset($name) {
return isset($this->items['parameters'][$name]);
}
public function __unset($id) {
throw new \RunTimeException('You cannot change the contents of the request object');
2013-03-26 12:33:44 +01:00
}
}