Completed
Push — master ( 61f3b2...32110c )
by Jean-Christophe
02:56
created

URequest::getBoolean()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
use Ubiquity\controllers\Startup;
6
use Ubiquity\utils\base\UString;
7
8
/**
9
 * Http Request utilities
10
 * @author jc
11
 * @version 1.0.0.2
12
 */
13
class URequest {
14
15
	/**
16
	 * Affects member to member the values of the associative array $values to the members of the object $object
17
	 * Used for example to retrieve the variables posted and assign them to the members of an object
18
	 * @param object $object
19
	 * @param associative array $values
20
	 */
21
	public static function setValuesToObject($object, $values=null) {
22
		if (!isset($values))
23
			$values=$_POST;
24
		foreach ( $values as $key => $value ) {
25
			$accessor="set" . ucfirst($key);
26
			if (method_exists($object, $accessor)) {
27
				$object->$accessor($value);
28
				$object->_rest[$key]=$value;
29
			}
30
		}
31
	}
32
33
	/**
34
	 * Affects member to member the values of $_GET to the members of the object $object
35
	 * $object must have accessors to each members
36
	 * @param object $object
37
	 */
38
	public static function setGetValuesToObject($object) {
0 ignored issues
show
Coding Style introduced by
setGetValuesToObject uses the super-global variable $_GET 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...
39
		self::setValuesToObject($object,$_GET);
40
	}
41
42
	/**
43
	 * Affects member to member the values of $_POST to the members of the object $object
44
	 * $object must have accessors to each members
45
	 * @param object $object
46
	 */
47
	public static function setPostValuesToObject($object) {
0 ignored issues
show
Coding Style introduced by
setPostValuesToObject uses the super-global variable $_POST 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...
48
		self::setValuesToObject($object,$_POST);
49
	}
50
51
	/**
52
	 * Call a cleaning function on the post
53
	 * @param string $function the cleaning function, default htmlentities
54
	 * @return array
55
	 */
56
	public static function getPost($function="htmlentities") {
57
		return array_map($function, $_POST);
58
	}
59
60
	/**
61
	 * Returns the query data, for PUT, DELETE PATCH methods
62
	 */
63
	public static function getInput() {
64
		$put=array ();
65
		\parse_str(\file_get_contents('php://input'), $put);
66
		return $put;
67
	}
68
69
	/**
70
	 * Returns the query data, regardless of the method
71
	 * @return array
72
	 */
73
	public static function getDatas() {
74
		$method=\strtolower($_SERVER['REQUEST_METHOD']);
75
		switch($method) {
76
			case 'post':
77
				return $_POST;
78
			case 'get':
79
				return $_GET;
80
			default:
81
				return self::getInput();
82
		}
83
	}
84
85
	/**
86
	 * Returns the request content-type header
87
	 * @return string
88
	 */
89
	public static function getContentType() {
90
		$headers=getallheaders();
91
		if (isset($headers["content-type"])) {
92
			return $headers["content-type"];
93
		}
94
		return null;
95
	}
96
97
	/**
98
	 * Returns true if the request is an Ajax request
99
	 * @return boolean
100
	 */
101
	public static function isAjax() {
102
		return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
103
	}
104
105
	/**
106
	 * Returns true if the request is sent by the POST method
107
	 * @return boolean
108
	 */
109
	public static function isPost() {
110
		return $_SERVER['REQUEST_METHOD'] === 'POST';
111
	}
112
113
	/**
114
	 * Returns true if the request is cross site
115
	 * @return boolean
116
	 */
117
	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...
118
		return stripos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) === FALSE;
119
	}
120
121
	/**
122
	 * Returns true if request contentType is set to json
123
	 * @return boolean
124
	 */
125
	public static function isJSON() {
126
		$contentType=self::getContentType();
127
		return \stripos($contentType, "json") !== false;
128
	}
129
130
	/**
131
	 * Returns the value of the $key variable passed by the get method or $default if the $key variable does not exist
132
	 * @param string $key
133
	 * @param string $default return value by default
134
	 * @return string
135
	 */
136
	public static function get($key, $default=NULL) {
137
		return isset($_GET[$key]) ? $_GET[$key] : $default;
138
	}
139
	
140
	/**
141
	 * Returns a boolean at the key position in request
142
	 *
143
	 * @param string $key
144
	 *        	the key to add or set
145
	 * @return boolean
146
	 */
147
	public static function getBoolean($key) {
1 ignored issue
show
Coding Style introduced by
getBoolean uses the super-global variable $_REQUEST 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...
148
		$ret = false;
149
		if (isset ( $_REQUEST[$key] )) {
150
			$ret = UString::isBooleanTrue ( $_REQUEST[$key] );
151
		}
152
		return $ret;
153
	}
154
155
	/**
156
	 * Returns the value of the $key variable passed by the post method or $default if the $key variable does not exist
157
	 * @param string $key
158
	 * @param string $default return value by default
159
	 * @return string
160
	 */
161
	public static function post($key, $default=NULL) {
162
		return isset($_POST[$key]) ? $_POST[$key] : $default;
163
	}
164
165
	public static function getUrl($url) {
166
		$config=Startup::getConfig();
167
		$siteUrl=\rtrim($config["siteUrl"], '/');
168
		if (UString::startswith($url, "/") === false) {
169
			$url="/" . $url;
170
		}
171
		return $siteUrl . $url;
172
	}
173
174
	public static function getUrlParts() {
175
		return \explode("/", $_GET["c"]);
176
	}
177
178
	/**
179
	 * Returns the http method
180
	 * @return string
181
	 */
182
	public static function getMethod() {
183
		return \strtolower($_SERVER['REQUEST_METHOD']);
184
	}
185
186
	public static function cleanUrl($url){
187
		$url=\str_replace("\\", "/", $url);
188
		return \str_replace("//", "/", $url);
189
	}
190
}
191