1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Almendra\Http; |
4
|
|
|
|
5
|
|
|
use Almendra\Http\Interfaces\ServerInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A wrapper for the superglobals $_SERVER, $_GET and $_POST. |
9
|
|
|
* |
10
|
|
|
* @package Almendra\PSR7 |
11
|
|
|
* @author Richard Trujillo Torres <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Server implements ServerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Retrieves a value defined in the superglobal $_SERVER. |
17
|
|
|
* |
18
|
|
|
* @param string $value The key's name. |
19
|
|
|
* @return string|mixed |
20
|
|
|
*/ |
21
|
|
|
public static function getValue($value, $default = '') |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
if (array_key_exists($value, $_SERVER)) { |
24
|
|
|
return $_SERVER[$value]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $default; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Retrieves all values defined in the superglobal $_SERVER. |
32
|
|
|
* |
33
|
|
|
* @return string|mixed |
34
|
|
|
*/ |
35
|
|
|
public static function getValues() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
return $_SERVER; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns a value from the $_GET superglobal. |
43
|
|
|
* Null if none exists. |
44
|
|
|
* |
45
|
|
|
* @param string $name The value's name |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public static function get($name) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
if (isset($_GET[$name])) { |
51
|
|
|
return $_GET[$name]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns all values from the $_GET superglobal. |
59
|
|
|
* Null if none exists. |
60
|
|
|
* |
61
|
|
|
* @param string $name |
|
|
|
|
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public static function gets(array $values = null) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$fields = []; |
67
|
|
|
if (isset($values) && null !== $values) { |
68
|
|
|
foreach ($values as $name) { |
69
|
|
|
$fields[] = self::get($name); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $fields; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return isset($_GET) ? $_GET : null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns a value from the $_POST superglobal. |
80
|
|
|
* Null if none exists. |
81
|
|
|
* |
82
|
|
|
* @param string $name The value's name |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public static function post($name) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
if (isset($_POST[$name])) { |
88
|
|
|
return $_POST[$name]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns all values from the $_POST superglobal. |
96
|
|
|
* Null if none exists. |
97
|
|
|
* |
98
|
|
|
* @param string $name |
|
|
|
|
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public static function posts(array $values = null) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$fields = []; |
104
|
|
|
if (isset($values) && null !== $values) { |
105
|
|
|
foreach ($values as $name) { |
106
|
|
|
$fields[] = self::post($name); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $fields; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return isset($_POST) ? $_POST : null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns a value from the $_FILES superglobal. |
117
|
|
|
* Null if none exists. |
118
|
|
|
* |
119
|
|
|
* @param string $name The value's name |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public static function file($name) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
if (isset($_FILES[$name])) { |
125
|
|
|
return $_FILES[$name]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns all values from the $_FILES superglobal. |
133
|
|
|
* Null if none exists. |
134
|
|
|
* |
135
|
|
|
* @param string $name |
|
|
|
|
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public static function files(array $values = null) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$fields = []; |
141
|
|
|
if (isset($values) && null !== $values) { |
142
|
|
|
foreach ($values as $name) { |
143
|
|
|
$fields[] = self::file($name); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $fields; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return isset($_FILES) ? $_FILES : null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
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: