Completed
Push — master ( 04c30e...76fb55 )
by Jean-Christophe
01:50
created

RequestUtils::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ubiquity\utils;
4
5
use Ubiquity\controllers\Startup;
6
7
/**
8
 * Request utilities
9
 * @author jc
10
 * @version 1.0.0.1
11
 */
12
class RequestUtils {
13
14
	/**
15
	 * Affects member to member the values of the associative array $values to the members of the object $object
16
	 * Used for example to retrieve the variables posted and assign them to the members of an object
17
	 * @param object $object
18
	 * @param associative array $values
19
	 */
20
	public static function setValuesToObject($object, $values=null) {
21
		if (!isset($values))
22
			$values=$_POST;
23
		foreach ( $values as $key => $value ) {
24
			$accessor="set" . ucfirst($key);
25
			if (method_exists($object, $accessor)) {
26
				$object->$accessor($value);
27
				$object->_rest[$key]=$value;
28
			}
29
		}
30
	}
31
32
	/**
33
	 * Call a cleaning function on the post
34
	 * @param string $function the cleaning function, default htmlentities
35
	 * @return array
36
	 */
37
	public static function getPost($function="htmlentities") {
38
		return array_map($function, $_POST);
39
	}
40
41
	/**
42
	 * Returns the query data, for PUT, DELETE PATCH methods
43
	 */
44
	public static function getInput(){
45
		$put = array();
46
		\parse_str(\file_get_contents('php://input'), $put);
47
		return $put;
48
	}
49
50
	/**
51
	 * Returns the query data, regardless of the method
52
	 * @return array
53
	 */
54
	public static function getDatas(){
55
		$method=\strtolower($_SERVER['REQUEST_METHOD']);
56
		switch ($method) {
57
			case 'post':
58
				return $_POST;
59
			case 'get':
60
				return $_GET;
61
			default:
62
				return self::getInput();
63
		}
64
	}
65
66
	/**
67
	 * Returns the request content-type header
68
	 * @return string
69
	 */
70
	public static function getContentType(){
71
		$headers=getallheaders();
72
		if(isset($headers["content-type"])){
73
			return $headers["content-type"];
74
		}
75
		return null;
76
	}
77
78
	/**
79
	 * Returns true if the request is an Ajax request
80
	 * @return boolean
81
	 */
82
	public static function isAjax() {
83
		return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
84
	}
85
86
	/**
87
	 * Returns true if the request is sent by the POST method
88
	 * @return boolean
89
	 */
90
	public static function isPost() {
91
		return $_SERVER['REQUEST_METHOD'] === 'POST';
92
	}
93
94
	/**
95
	 * Returns true if the request is cross site
96
	 * @return boolean
97
	 */
98
	public static function isCrossSite(){
0 ignored issues
show
Coding Style introduced by
isCrossSite uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
99
		return stripos($_SERVER['HTTP_REFERER'],$_SERVER['SERVER_NAME'])===FALSE;
100
	}
101
102
	/**
103
	 * Returns true if request contentType is set to json
104
	 * @return boolean
105
	 */
106
	public static function isJSON(){
107
		$contentType=self::getContentType();
108
		return \stripos($contentType, "json")!==false;
109
	}
110
111
	/**
112
	 * Returns the value of the $key variable passed by the get method or $default if the $key variable does not exist
113
	 * @param string $key
114
	 * @param string $default return value by default
115
	 * @return string
116
	 */
117
	public static function get($key, $default=NULL) {
118
		return isset($_GET[$key]) ? $_GET[$key] : $default;
119
	}
120
121
	/**
122
	 * Returns the value of the $key variable passed by the post method or $default if the $key variable does not exist
123
	 * @param string $key
124
	 * @param string $default return value by default
125
	 * @return string
126
	 */
127
	public static function post($key, $default=NULL) {
128
		return isset($_POST[$key]) ? $_POST[$key] : $default;
129
	}
130
131
	public static function getUrl($url) {
132
		$config=Startup::getConfig();
133
		if (StrUtils::startswith($url, "/") === false) {
134
			$url="/" . $url;
135
		}
136
		return $config["siteUrl"] . $url;
137
	}
138
139
	public static function getUrlParts(){
140
		return \explode("/", $_GET["c"]);
141
	}
142
143
	/**
144
	 * Returns the http method
145
	 * @return string
146
	 */
147
	public static function getMethod() {
148
		return \strtolower($_SERVER['REQUEST_METHOD']);
149
	}
150
}
151