1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Net;
14:
15: 16: 17: 18: 19: 20: 21:
22: class Request
23: {
24:
25: protected ;
26: protected $get;
27: protected $post;
28: protected $cookie;
29: protected $request;
30: protected $file;
31: protected $filters;
32: protected $defaultMethod = 'request';
33:
34: public function __construct()
35: {
36: $this->get = $_GET;
37: $this->post = $_POST;
38: $this->cookie = $_COOKIE;
39: $this->file = $_FILES;
40: $this->request = $_REQUEST;
41: }
42:
43: 44: 45:
46: public function reset()
47: {
48: $this->get = $_GET;
49: $this->post = $_POST;
50: $this->cookie = $_COOKIE;
51: $this->file = $_FILES;
52: $this->request = $_REQUEST;
53: $this->headers = null;
54: }
55:
56: 57: 58: 59:
60: public function ()
61: {
62: if (empty($this->headers))
63: $this->headers = getallheaders();
64:
65: return $this->headers;
66: }
67:
68: 69: 70: 71: 72:
73: public function ($name)
74: {
75: $this->getHeaders();
76: return (isset($this->headers[$name])) ? $this->headers[$name] : null;
77: }
78:
79: 80: 81: 82: 83:
84: public function getServer($name = null)
85: {
86: if (!empty($name))
87: return (isset($_SERVER[$name])) ? $_SERVER[$name] : null;
88:
89: return $_SERVER;
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function getParam($name, $type = null)
99: {
100: $type = empty($type) ? $this->defaultMethod : strtolower($type);
101:
102: if (!$this->isValidMethod($type))
103: throw new \InvalidArgumentException('Type de paramètre invalide');
104:
105: if (!empty($this->filters[$type]) && $this->filters[$type]['isFiltered'] == false)
106: $this->applyFilters($type);
107:
108: return (isset($this->{$type}[$name])) ? $this->{$type}[$name] : null;
109: }
110:
111: 112: 113: 114: 115: 116:
117: public function getParams($type = null)
118: {
119: $type = empty($type) ? $this->defaultMethod : strtolower($type);
120:
121: if (!$this->isValidMethod($type))
122: throw new \InvalidArgumentException('Type de paramètre invalide');
123:
124: if (!empty($this->filters[$type]) && $this->filters[$type]['isFiltered'] == false)
125: $this->applyFilters($type);
126:
127: return $this->$type;
128: }
129:
130: 131: 132: 133: 134: 135: 136: 137:
138: public function getPost($name, $dataType = null,$flag = null)
139: {
140: return $this->getWithFilter($name, 'post', $dataType, $flag);
141: }
142:
143: 144: 145: 146: 147: 148: 149:
150: public function get($name, $dataType = null,$flag = null)
151: {
152: return $this->getWithFilter($name, 'get', $dataType, $flag);
153: }
154:
155: 156: 157: 158:
159: public function getEnv($name)
160: {
161: return getenv($name);
162: }
163:
164: 165: 166: 167: 168:
169: public function getFile($name)
170: {
171: return isset($this->file[$name]) ? $this->file[$name] : null;
172: }
173:
174: 175: 176: 177:
178: public function getFiles()
179: {
180: return $this->file;
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199:
200: public function setFilter(array $filtre, $type = null)
201: {
202: $type = empty($type) ? $this->defaultMethod : strtolower($type);
203:
204: if (!$this->isValidMethod($type))
205: throw new \InvalidArgumentException('Type de paramètre invalide');
206:
207: $this->filters[$type] = array(
208: 'filter' => $filtre,
209: 'isFiltered' => false
210: );
211:
212: return $this;
213: }
214:
215: 216: 217: 218: 219: 220:
221: public function setDefaultMethod($name)
222: {
223: $this->defaultMethod = strtolower($name);
224: }
225:
226: public function __get($name)
227: {
228: return $this->getParam($name);
229: }
230:
231: public function __isset($name)
232: {
233: $tmp = $this->getParam($name);
234: return isset($tmp);
235: }
236:
237: public function add($params, $type = null)
238: {
239: $type = empty($type) ? $this->defaultMethod : strtolower($type);
240:
241: $this->{$type} = array_merge($this->{$type}, $params);
242:
243: if (isset($this->filters[$type]))
244: $this->filters[$type]['isFiltered'] = false;
245: }
246:
247: 248: 249: 250:
251: public function isPost()
252: {
253: if($this->getServer('REQUEST_METHOD') == 'POST') {
254: return true;
255: }
256:
257: return false;
258: }
259:
260: 261: 262: 263:
264: public function isGet()
265: {
266: if($this->getServer('REQUEST_METHOD') == 'GET') {
267: return true;
268: }
269:
270: return false;
271: }
272:
273: 274: 275: 276: 277:
278: protected function isValidMethod($method)
279: {
280: return in_array($method, array('get', 'post', 'request', 'cookie'));
281: }
282:
283: 284: 285: 286: 287: 288:
289: protected function applyFilters($type)
290: {
291: if (empty($this->filters[$type]) || empty($this->$type))
292: return false;
293:
294: $this->$type = filter_var_array($this->$type, $this->filters[$type]['filter']);
295:
296: if (!$this->$type)
297: throw new \RuntimeException('Filtres invalide');
298:
299: $this->filters['isFiltered'] = true;
300: }
301:
302: 303: 304: 305: 306: 307: 308:
309: protected function getWithFilter($name, $type, $dataType,$flag = null)
310: {
311: $type = empty($type) ? $this->defaultMethod : strtolower($type);
312: if (isset($this->{$type}[$name]))
313: {
314: switch ($dataType)
315: {
316: case 'int' :
317: return intval($this->{$type}[$name]);
318: break;
319: case 'float' :
320: return floatval($this->{$type}[$name]);
321: break;
322: case 'string' :
323: $str = filter_var($this->{$type}[$name], FILTER_SANITIZE_STRING,$flag);
324: return ($str != false) ? $str : null;
325: break;
326: case 'email' :
327: $str = filter_var($this->{$type}[$name], FILTER_VALIDATE_EMAIL,$flag);
328: return ($str != false) ? $str : null;
329: break;
330: case 'url' :
331: $str = filter_var($this->{$type}[$name], FILTER_VALIDATE_URL,$flag);
332: return ($str != false) ? $str : null;
333: break;
334: case 'ip' :
335: $str = filter_var($this->{$type}[$name], FILTER_VALIDATE_IP,$flag);
336: return ($str != false) ? $str : null;
337: break;
338: default :
339: return $this->{$type}[$name];
340: }
341: }
342: else
343: {
344: return null;
345: }
346: }
347:
348: }
349:
350: ?>
351: