1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psecio\Invoke; |
4
|
|
|
|
5
|
|
|
class Resource |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Curernt URI location |
9
|
|
|
* @var string |
10
|
|
|
*/ |
11
|
|
|
protected $uri; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Current HTTP method/verb |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $method; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create the resource, set the URL and HTTP method manually if desired |
21
|
|
|
* URI will default to REQUEST_URI |
22
|
|
|
* HTTP method will default to REQUEST_METHOD |
23
|
|
|
* |
24
|
|
|
* @param string $uri URI for the resource |
25
|
|
|
* @param string $httpMethod HTTP method for the resource |
26
|
|
|
*/ |
27
|
|
|
public function __construct($uri = null, $httpMethod = null) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$this->setUri( |
30
|
|
|
($uri !== null) ? $uri : $_SERVER['REQUEST_URI'] |
31
|
|
|
); |
32
|
|
|
$this->setHttpMethod( |
33
|
|
|
($httpMethod !== null) ? $httpMethod : $_SERVER['REQUEST_METHOD'] |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the curren tHTTP method |
39
|
|
|
* |
40
|
|
|
* @param string $method HTTP method (ex: GET, POST) |
41
|
|
|
*/ |
42
|
|
|
public function setHttpMethod($method) |
43
|
|
|
{ |
44
|
|
|
$this->method = strtoupper($method); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the current HTTP method |
49
|
|
|
* |
50
|
|
|
* @return string HTTP method currently set |
51
|
|
|
*/ |
52
|
|
|
public function getHttpMethod() |
53
|
|
|
{ |
54
|
|
|
return $this->method; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set the current URI for the resource |
59
|
|
|
* |
60
|
|
|
* @param string $uri URI path |
61
|
|
|
*/ |
62
|
|
|
public function setUri($uri) |
63
|
|
|
{ |
64
|
|
|
$this->uri = $uri; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the curent URI setting |
69
|
|
|
* |
70
|
|
|
* @return string Current URI setting |
71
|
|
|
*/ |
72
|
|
|
public function getUri($parsed = false) |
73
|
|
|
{ |
74
|
|
|
return ($parsed === true) ? parse_url($this->uri) : $this->uri; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getParams() |
78
|
|
|
{ |
79
|
|
|
$result = []; |
80
|
|
|
$parse = parse_url($this->uri); |
81
|
|
|
|
82
|
|
|
if (isset($parse['query'])) { |
83
|
|
|
$params = explode('&', $parse['query']); |
84
|
|
|
foreach ($params as $param) { |
85
|
|
|
list($key, $value) = explode('=', $param); |
86
|
|
|
$result[$key] = $value; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
} |
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: