1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Almendra\Http\Psr\Messages; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface as RequestInterface; |
6
|
|
|
use Almendra\Http\Psr\Messages\Response; |
7
|
|
|
|
8
|
|
|
use Almendra\Http\Helpers\URI as URIHelper; |
9
|
|
|
use Almendra\Http\Server; |
10
|
|
|
|
11
|
|
|
class ServerRequest extends Response implements RequestInterface |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
protected $_cookies = []; |
14
|
|
|
protected $_serverParams = []; |
15
|
|
|
|
16
|
|
|
// protocol ver |
17
|
|
|
// headers |
18
|
|
|
// body |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
public function __construct($cookies = null, $serverParams = null) { |
|
|
|
|
22
|
|
|
if (isset($cookies) && null !== $cookies) { |
23
|
|
|
$this -> setCookies($cookies); |
24
|
|
|
} else { |
25
|
|
|
$this -> setCookies($this -> getCookieParams()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Retrieve server parameters. |
32
|
|
|
* |
33
|
|
|
* Retrieves data related to the incoming request environment, |
34
|
|
|
* typically derived from PHP's $_SERVER superglobal. The data IS NOT |
35
|
|
|
* REQUIRED to originate from $_SERVER. |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function getServerParams() { |
|
|
|
|
40
|
|
|
if (isset($_SERVER) && null !== $_SERVER) { |
41
|
|
|
$this -> _serverParams = $_SERVER; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this -> _serverParams; // attempt to retrieve them |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Retrieve cookies. |
50
|
|
|
* |
51
|
|
|
* Retrieves cookies sent by the client to the server. |
52
|
|
|
* |
53
|
|
|
* The data MUST be compatible with the structure of the $_COOKIE |
54
|
|
|
* superglobal. |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function getCookieParams() |
59
|
|
|
{ |
60
|
|
|
return $this -> _cookies; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function withCookieParams(array $cookies) |
64
|
|
|
{ |
65
|
|
|
$clone = clone $this; |
66
|
|
|
$clone -> setCookies($cookies); |
67
|
|
|
|
68
|
|
|
return $clone; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setCookies(array $cookies) |
72
|
|
|
{ |
73
|
|
|
foreach ($cookies as $name => $value) { |
74
|
|
|
$this -> setCookie($name, $value); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function setCookie($name, $value) |
79
|
|
|
{ |
80
|
|
|
$this -> _cookies[$name] = $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieve query string arguments. |
85
|
|
|
* |
86
|
|
|
* Retrieves the deserialized query string arguments, if any. |
87
|
|
|
* |
88
|
|
|
* Note: the query params might not be in sync with the URI or server |
89
|
|
|
* params. If you need to ensure you are only getting the original |
90
|
|
|
* values, you may need to parse the query string from `getUri()->getQuery()` |
91
|
|
|
* or from the `QUERY_STRING` server param. |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getQueryParams() |
96
|
|
|
{ |
97
|
|
|
return URIHelper::getQueryParams($this -> getUri(), false); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|