1 | <?php |
---|
2 | require_once('class.utils.php'); |
---|
3 | /** |
---|
4 | * Contains useful methods for PHP type checking and casting |
---|
5 | * @author Carlos Eduardo Nogueira Gonçalves |
---|
6 | * @author Marcos Pont |
---|
7 | * @version 1.0 |
---|
8 | * @link http://workflow.celepar.parana/doc-workflow/classes/typeutils Complete reference |
---|
9 | * @package Workflow |
---|
10 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
11 | */ |
---|
12 | class TypeUtils extends Utils |
---|
13 | { |
---|
14 | /** |
---|
15 | * Return the type of variable |
---|
16 | * @param $value |
---|
17 | * @return type of element |
---|
18 | * @access public |
---|
19 | */ |
---|
20 | function getType($value) { |
---|
21 | return gettype($value); |
---|
22 | } |
---|
23 | |
---|
24 | /** |
---|
25 | * Check if value is a float number |
---|
26 | * @param int $value |
---|
27 | * @param bool $stritct |
---|
28 | * @access public |
---|
29 | * @return bool |
---|
30 | */ |
---|
31 | function isFloat(&$value, $strict=FALSE) { |
---|
32 | $locale = localeconv(); |
---|
33 | $dp = $locale['decimal_point']; |
---|
34 | $exp = "/^\-?[0-9]+(\\" . $dp . "[0-9]+)?$/"; |
---|
35 | if (preg_match($exp, $value)) { |
---|
36 | if (!$strict && !is_float($value)) { |
---|
37 | $value = $this->parseFloat($value); |
---|
38 | } |
---|
39 | return TRUE; |
---|
40 | } else { |
---|
41 | return FALSE; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Parse Float number |
---|
47 | * @param string $value |
---|
48 | * @access public |
---|
49 | * @return float |
---|
50 | */ |
---|
51 | function parseFloat($value) { |
---|
52 | if ($this->isString($value)) { |
---|
53 | $locale = localeconv(); |
---|
54 | if ($locale['decimal_point'] != '.') { |
---|
55 | $value = str_replace($locale['decimal_point'], '.', $value); |
---|
56 | } |
---|
57 | } |
---|
58 | return floatval($value); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | /** |
---|
63 | * Parse Float Positive |
---|
64 | * @param string $value |
---|
65 | * @access public |
---|
66 | * @return int |
---|
67 | */ |
---|
68 | function parseFloatPositive($value) { |
---|
69 | return abs(floatval($value)); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Checks if $value is a string |
---|
74 | * @param string $value |
---|
75 | * @access public |
---|
76 | * @return bool |
---|
77 | */ |
---|
78 | function isString($value) { |
---|
79 | return is_string($value); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Checks if $values is a array |
---|
84 | * @param string $value |
---|
85 | * @access public |
---|
86 | * @return bool |
---|
87 | */ |
---|
88 | function isArray($value) { |
---|
89 | return is_array($value); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Checks if $value is a Hash array |
---|
94 | * @param string $value |
---|
95 | * @access public |
---|
96 | * @return bool |
---|
97 | */ |
---|
98 | function isHashArray($value) { |
---|
99 | if (is_array($value) && sizeof($value)) { |
---|
100 | $i = 0; |
---|
101 | $keys = array_keys($value); |
---|
102 | foreach ($keys as $k=>$v) { |
---|
103 | if ($v !== $i) { |
---|
104 | return TRUE; |
---|
105 | } |
---|
106 | $i++; |
---|
107 | } |
---|
108 | } |
---|
109 | return FALSE; |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * the value to array |
---|
114 | * @param mixed $value |
---|
115 | * @access public |
---|
116 | * @return array |
---|
117 | */ |
---|
118 | function toArray($value) { |
---|
119 | return is_array($value) ? $value : array($value); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Check if the parameter is a object |
---|
124 | * @param mixed $value type to test |
---|
125 | * @access public |
---|
126 | * @return bool |
---|
127 | */ |
---|
128 | function isObject($value) { |
---|
129 | return is_object($value); |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * Check if object is instance of class |
---|
134 | * @param object $object |
---|
135 | * @param string $className |
---|
136 | * @param bool $recurse |
---|
137 | * @return bool |
---|
138 | * @access public |
---|
139 | */ |
---|
140 | function isInstanceOf($object, $className, $recurse=TRUE) { |
---|
141 | if (!is_object($object)) |
---|
142 | return FALSE; |
---|
143 | $objClass = get_class($object); |
---|
144 | $otherClass = ($this->isPHP(5) ? $className : strtolower($className)); |
---|
145 | if ($recurse) |
---|
146 | return ($objClass == $otherClass || is_subclass_of($object, $otherClass)); |
---|
147 | return ($objClass == $otherClass); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Check if the parameter is a Resource |
---|
152 | * @param mixed $value |
---|
153 | * @return bool |
---|
154 | * @access public |
---|
155 | */ |
---|
156 | function isResource($value) { |
---|
157 | if (is_resource($value)) |
---|
158 | return get_resource_type($value); |
---|
159 | return FALSE; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Checks if the value is a boolean |
---|
164 | * @param mixed $value |
---|
165 | * @return bool |
---|
166 | * @access public |
---|
167 | */ |
---|
168 | function isBoolean($value) { |
---|
169 | return ($value === TRUE || $value === FALSE); |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Check if the value is a boolean and true |
---|
174 | * @param bool $value |
---|
175 | * @return bool |
---|
176 | * @access public |
---|
177 | */ |
---|
178 | function isTrue($value) { |
---|
179 | return ($value === TRUE); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Return a default value if first parameter is false |
---|
184 | * @param mixed $value test value |
---|
185 | * @param mixed $default default value to return if first parameter is false |
---|
186 | * @access public |
---|
187 | */ |
---|
188 | function ifFalse($value, $default = FALSE) { |
---|
189 | if ($value === FALSE) |
---|
190 | return $default; |
---|
191 | return $value; |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * Converts value to boolean 0 false otherwise true |
---|
196 | * @param mixed $value |
---|
197 | * @return bool true or false |
---|
198 | * @access public |
---|
199 | */ |
---|
200 | function toBoolean($value) { |
---|
201 | return (bool)$value; |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * Checks if value parameter is empty |
---|
206 | * @param mixed $value |
---|
207 | * @return bool true empty otherwise false |
---|
208 | * @access public |
---|
209 | */ |
---|
210 | function isEmpty($value) { |
---|
211 | $result = empty($value); |
---|
212 | return $result; |
---|
213 | } |
---|
214 | } |
---|
215 | ?> |
---|