Passed
Push — master ( 682a54...fa63c5 )
by
unknown
01:55
created

Request   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 38.1%

Importance

Changes 0
Metric Value
dl 0
loc 181
ccs 16
cts 42
cp 0.381
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A fromGlobals() 0 3 1
A input() 0 11 2
A get() 0 3 1
A post() 0 3 1
A cookie() 0 3 1
A files() 0 3 1
A server() 0 3 1
A headers() 0 3 1
A getMethod() 0 10 3
A getUrl() 0 10 2
1
<?php
2
3
namespace CarbonFramework;
4
5
use CarbonFramework\Support\Arr;
6
7
/**
8
 * A server request representation
9
 */
10
class Request {
11
	/**
12
	 * GET parameters
13
	 *
14
	 * @var array
15
	 */
16
	protected $get = [];
17
18
	/**
19
	 * POST parameters
20
	 *
21
	 * @var array
22
	 */
23
	protected $post = [];
24
25
	/**
26
	 * COOKIE parameters
27
	 *
28
	 * @var array
29
	 */
30
	protected $cookie = [];
31
32
	/**
33
	 * FILES parameters
34
	 *
35
	 * @var array
36
	 */
37
	protected $files = [];
38
39
	/**
40
	 * SERVER parameters
41
	 *
42
	 * @var array
43
	 */
44
	protected $server = [];
45
46
	/**
47
	 * Headers
48
	 *
49
	 * @var array
50
	 */
51
	protected $headers = [];
52
53
	/**
54
	 * Create a new instance from php superglobals
55
	 *
56
	 * @return Request
57
	 */
58 1
	public static function fromGlobals() {
59 1
		return new static( stripslashes_deep( $_GET ), stripslashes_deep( $_POST ), $_COOKIE, $_FILES, $_SERVER, getallheaders() );
60
	}
61
62
	/**
63
	 * Constructor
64
	 *
65
	 * @param array $get
66
	 * @param array $post
67
	 * @param array $cookie
68
	 * @param array $files
69
	 * @param array $server
70
	 * @param array $headers
71
	 */
72
	public function __construct( $get, $post, $cookie, $files, $server, $headers ) {
73
		$this->get = $get;
74
		$this->post = $post;
75
		$this->cookie = $cookie;
76
		$this->files = $files;
77
		$this->server = $server;
78
		$this->headers = $headers;
79
	}
80
81
	/**
82
	 * Return the request method
83
	 *
84
	 * @return string
85
	 */
86 1
	public function getMethod() {
87 1
		$method = (string) Arr::get( $this->server, 'REQUEST_METHOD', 'GET' );
88
89 1
		$override = (string) Arr::get( $this->headers, 'X-HTTP-METHOD-OVERRIDE' );
90 1
		if ( $method === 'POST' && $override ) {
91
			$method = $override;
92
		}
93
94 1
		return strtoupper( $method );
95
	}
96
97
	/**
98
	 * Return the request url
99
	 *
100
	 * @return string
101
	 */
102 3
	public function getUrl() {
103 3
		$https = Arr::get( $this->server, 'HTTPS' );
104
105 3
		$protocol = $https ? 'https' : 'http';
106 3
		$host = (string) Arr::get( $this->server, 'HTTP_HOST', '' );
107 3
		$uri = (string) Arr::get( $this->server, 'REQUEST_URI', '' );
108 3
		$uri = Url::addLeadingSlash( $uri );
109
110 3
		return $protocol . '://' . $host . $uri;
111
	}
112
113
	/**
114
	 * Return a value from any of the request parameters
115
	 *
116
	 * @see  \CarbonFramework\Support\Arr
117
	 * @return mixed
118
	 */
119
	protected function input() {
120
		$args = func_get_args();
121
		$source = $this->{$args[0]};
122
123
		if ( count( $args ) === 1 ) {
124
			return $source;
125
		}
126
127
		$args[0] = $source;
128
		return call_user_func_array( [Arr::class, 'get'], $args );
129
	}
130
131
	/**
132
	 * Return a value from the GET parameters
133
	 *
134
	 * @see  \CarbonFramework\Support\Arr
135
	 * @return mixed
136
	 */
137 3
	public function get() {
138 3
		return call_user_func_array( [$this, 'input'], array_merge( ['get'], func_get_args() ) );
139
	}
140
141
	/**
142
	 * Return a value from the POST parameters
143
	 *
144
	 * @see  \CarbonFramework\Support\Arr
145
	 * @return mixed
146
	 */
147
	public function post() {
148
		return call_user_func_array( [$this, 'input'], array_merge( ['post'], func_get_args() ) );
149
	}
150
151
	/**
152
	 * Return a value from the COOKIE parameters
153
	 *
154
	 * @see  \CarbonFramework\Support\Arr
155
	 * @return mixed
156
	 */
157
	public function cookie() {
158
		return call_user_func_array( [$this, 'input'], array_merge( ['cookie'], func_get_args() ) );
159
	}
160
161
	/**
162
	 * Return a value from the FILES parameters
163
	 *
164
	 * @see  \CarbonFramework\Support\Arr
165
	 * @return mixed
166
	 */
167
	public function files() {
168
		return call_user_func_array( [$this, 'input'], array_merge( ['files'], func_get_args() ) );
169
	}
170
171
	/**
172
	 * Return a value from the SERVER parameters
173
	 *
174
	 * @see  \CarbonFramework\Support\Arr
175
	 * @return mixed
176
	 */
177
	public function server() {
178
		return call_user_func_array( [$this, 'input'], array_merge( ['server'], func_get_args() ) );
179
	}
180
181
	/**
182
	 * Return a value from the headers
183
	 *
184
	 * @see  \CarbonFramework\Support\Arr
185
	 * @return mixed
186
	 */
187
	public function headers() {
188
		return call_user_func_array( [$this, 'input'], array_merge( ['headers'], func_get_args() ) );
189
	}
190
}
191