/
param.php
executable file
·124 lines (114 loc) · 3.25 KB
/
param.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
<?php
/*
*
Copyright 2016 Trigallez Arno
Distributed under the terms of the GNU Lesser General Public License v3
This file is part of the mini-mvc library.
mini-mvc is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mini-mvc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with mini-mvc. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Return casted http parameter sent by GET
* if the parameter is not define null is return
*
* @param string $name parameter's name sent by GET
* @param string $type Type of data to return:
* @return int|string|bool|float|array parameter sent by GET
*/
function getGet($name, $type)
{
assert('is_string($name); //* $name should be a string');
assert('is_string($type); //* $name should be a string');
if(isSet($_GET[$name])):
switch ($type)
{
case 'int':
return (int) $_GET[$name] + 0;
break;
case 'string':
return (string) $_GET[$name];
break;
case 'bool':
return (bool) $_GET[$name];
break;
case 'float':
return (float) $_GET[$name];
break;
case 'array':
return (array) $_GET[$name];
break;
default:
throw new Exception("$type is not a valid type");
}
else:
return null;
endif;
}
/**
* Return casted http parameter sent by POST
* if the parameter is not define null is return
*
* @param string $name parameter's name sent by POST
* @param string $type Type of data to return:
* @return int|string|bool|float|array parameter sent by POST
*/
function getPost($name, $type)
{
assert('is_string($name); //* $name should be a string');
assert('is_string($type); //* $name should be a string');
if(isSet($_POST[$name])):
switch ($type)
{
case 'int':
return (int) $_POST[$name] + 0;
break;
case 'string':
return (string) $_POST[$name];
break;
case 'bool':
return (bool) $_POST[$name];
break;
case 'float':
return (float) $_POST[$name];
break;
case 'array':
return (array) $_POST[$name];
break;
default:
throw new Exception("$type is not a valid type");
}
else:
return null;
endif;
}
/**
* Check if given variable is empty
* work with array and string too
*
* @param mixed $param parameter's name sent by POST
* @return bool
*/
function is_empty($param)
{
if(is_null($param)):
return true;
elseif(is_string($param)):
if(empty($param))
return true;
elseif(is_array($param)):
if(count($param) == 0)
return true;
else:
return false;
endif;
}
?>