-
Notifications
You must be signed in to change notification settings - Fork 0
/
Request.php
211 lines (204 loc) · 5.43 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* @author iann
* this class work with parameters from Request = GET,POST
*
*/
namespace BlueSeed;
class Request
{
/**
* The data received in $_GET parsed by processURLParam
* in format where any value is independent
* @var ARRAY
* @access protected
*/
protected $GET;
/**
* The data received in $_GET parsed by processURLParam
* in format [N] = N+1
* @var Array
* @access protected
*/
protected $PAIRGET;
/**
* The data received in $_POST
* @var Array
* @access protected
*/
protected $POST;
function __construct (Array $GET, Array $POST)
{
$this->processURLParam((isset($GET['bsurl']))?$GET['bsurl']:null);
$this->POST = $POST;
}
/**
* Method where the friendly URL are parsed into Controller /
* and Action /Data1/Data2/DataN
* @return void
* @access private
*/
private function processURLParam($GET){
if (strlen($GET)==0) {
$this->setDefaultControllerAction($GET);
return true;
}
$GET = explode ('/', $GET);
if ($GET[count($GET)-1]=='') {
array_pop($GET);
}
if ($this->setDefaultControllerAction($GET)) {
return true;
}
foreach ($GET as $idx => $each){
if ($idx < 1)
continue;
$this->GET[$idx] = $each;//, FILTER_SANITIZE_URL );
if (!($idx % 2)) {
$this->PAIRGET[$each] = (($idx+1) < count($GET))
?$GET[($idx+1)] //, FILTER_SANITIZE_URL )
:NULL;
}
}
}
/**
* Set the default parameters to Controller and Action if not informed
* with URL
* @param array $GET the Request URL
*
* @return boolean
* @access private
*/
private function setDefaultControllerAction($GET)
{
if (!is_array($GET))
$GET = strlen($GET)==0?Array():explode('/',$GET);
if (count($GET)==0){
$this->PAIRGET['controller'] = $this->GET[0] = "IndexController";
$this->PAIRGET['action'] = $this->GET[1] = "Index";
return true;
} else {
$this->PAIRGET['controller'] = $this->GET[0] =
str_replace("-","",ucfirst($GET[0]))."Controller";
if (count($GET)==1)
$GET[1] = 'Index';
$this->PAIRGET['action'] = $this->GET[1] =
str_replace("-","",ucfirst($GET[1]));
return false;
}
}
public function setController($controller)
{
$this->PAIRGET['controller'] = str_replace("-","",ucfirst($controller))."Controller";
$this->GET[0] = $this->PAIRGET['controller'];
}
public function setAction($action)
{
$this->PAIRGET['action'] = str_replace("-","",ucfirst($action));
$this->GET[1] = $this->PAIRGET['action'];
}
public function getAction()
{
return $this->PAIRGET['action'];
}
public function getController()
{
return $this->PAIRGET['controller'];
}
/**
* inform if the Request is a GET Request
* @return boolean
* @access public
*/
public function isGet()
{
return !$this->isPOST();
}
/**
* inform if the Request is a POST Request
* @return boolean
* @access public
*/
public function isPost()
{
return (boolean) count($_POST);
}
/**
* set the Parameters into Request Object.
* normally its is automaticly done using _POST Data
* @param string $name the parameter name
* @param string $value the parameter value
* @return void
* @access public
*/
public function setParam($name, $value){
$this->POST[$name] = $value;
}
/**
* get The Data Parameters. Normally its a _POST data
* @param string $name
* @return mixed
* @access public
*/
public function getParam($name)
{
return isset($this->POST[$name])?$this->POST[$name]:NULL;
}
/**
* get All the POST data
* @return array
*/
public function getParams()
{
return $this->POST;
}
/**
* Get the Data from URL Request
*
* @param string $name
* @return string
* @access public
*/
public function getQuery($name=null)
{
if (is_null($name))
return $this->PAIRGET;
if( array_key_exists($name, $this->PAIRGET) &&
!is_null($this->PAIRGET[$name])
) {
return $this->PAIRGET[$name];
} else {
return (array_key_exists($name, $this->GET))
?$this->GET[$name]
:NULL;
}
}
/**
*
* return if have some data setted in QueryString
* @param string $name
* @return bool
*/
public function hasinQuery($name=null)
{
if (is_null($name)) {
return (count($this->GET)-2 > 0)?true:false;
} else {
return array_key_exists($name, $this->GET);
}
}
/**
*
* Return if have some POST Data
* @param string $name
* @return bool
*/
public function hasParam($name = null)
{
if (is_null($name)) {
return (count($this->POST)>0)?true:false;
} else {
return array_key_exists($name, $this->POST);
}
}
}