Docblock and newline improvements in CRM_Core_Config and CRM_Utils_Hook. The IDE...
[civicrm-core.git] / CRM / Utils / Type.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id: $
33 *
34 */
35class CRM_Utils_Type {
36 CONST
37 T_INT = 1,
38 T_STRING = 2,
39 T_ENUM = 2,
40 T_DATE = 4,
41 T_TIME = 8,
6a488035
TO
42 T_BOOLEAN = 16,
43 T_TEXT = 32,
44 T_LONGTEXT = 32,
45 T_BLOB = 64,
46 T_TIMESTAMP = 256,
47 T_FLOAT = 512,
48 T_MONEY = 1024,
49 T_EMAIL = 2048,
50 T_URL = 4096,
51 T_CCNUM = 8192,
52 T_MEDIUMBLOB = 16384;
53
54 CONST
55 TWO = 2,
56 FOUR = 4,
57 SIX = 6,
58 EIGHT = 8,
59 TWELVE = 12,
60 SIXTEEN = 16,
61 TWENTY = 20,
62 MEDIUM = 20,
63 THIRTY = 30,
64 BIG = 30,
65 FORTYFIVE = 45,
66 HUGE = 45;
67
68 /**
69 * Convert Constant Data type to String
70 *
71 * @param $type integer datatype
72 *
73 * @return $string String datatype respective to integer datatype
74 *
75 * @access public
76 * @static
77 */
78 static function typeToString($type) {
79 switch ($type) {
80 case 1:
81 $string = 'Int';
82 break;
83
84 case 2:
85 $string = 'String';
86 break;
87
88 case 3:
89 $string = 'Enum';
90 break;
91
92 case 4:
93 $string = 'Date';
94 break;
95
96 case 8:
97 $string = 'Time';
98 break;
99
100 case 16:
101 $string = 'Boolean';
102 break;
103
104 case 32:
105 $string = 'Text';
106 break;
107
108 case 64:
109 $string = 'Blob';
110 break;
111
112 // CRM-10404
113 case 12:
114 case 256:
115 $string = 'Timestamp';
116 break;
117
118 case 512:
119 $string = 'Float';
120 break;
121
122 case 1024:
123 $string = 'Money';
124 break;
125
126 case 2048:
127 $string = 'Date';
128 break;
129
130 case 4096:
131 $string = 'Email';
132 break;
133
134 case 16384:
135 $string = 'Mediumblob';
136 break;
137 }
138
139 return (isset($string)) ? $string : "";
140 }
141
142 /**
143 * Verify that a variable is of a given type
144 *
145 * @param mixed $data The variable
146 * @param string $type The type
147 * @param boolean $abort Should we abort if invalid
148 *
149 * @return mixed The data, escaped if necessary
150 * @access public
151 * @static
152 */
153 public static function escape($data, $type, $abort = TRUE) {
154 switch ($type) {
155 case 'Integer':
156 case 'Int':
157 if (CRM_Utils_Rule::integer($data)) {
158 return $data;
159 }
160 break;
161
162 case 'Positive':
163 // the below 2 are for custom fields of this type
164 // CRM-8925
165 case 'Country':
166 case 'StateProvince':
9ff5f6c0
N
167 // Checked for multi valued state/country value
168 if (is_array($data)) {
169 $returnData = TRUE;
170 foreach ($data as $data) {
171 if (CRM_Utils_Rule::positiveInteger($data) || CRM_Core_DAO::escapeString($data)) {
172 $returnData = TRUE;
173 }
174 else {
175 $returnData = FALSE;
176 }
177 }
178 if ($returnData) {
179 return $data;
180 }
181 }
182 elseif (!is_numeric($data) && CRM_Core_DAO::escapeString($data)) {
183 return $data;
184 }
185 elseif (CRM_Utils_Rule::positiveInteger($data)) {
6a488035
TO
186 return $data;
187 }
188 break;
189
190 case 'Link':
191 if (CRM_Utils_Rule::url($data = trim($data))) {
192 return $data;
193 }
194 break;
195
196 case 'Boolean':
197 if (CRM_Utils_Rule::boolean($data)) {
198 return $data;
199 }
200 break;
201
202 case 'Float':
203 case 'Money':
204 if (CRM_Utils_Rule::numeric($data)) {
205 return $data;
206 }
207 break;
208
209 case 'String':
210 case 'Memo':
85bdc94e 211 case 'Text':
6a488035
TO
212 return CRM_Core_DAO::escapeString($data);
213
214 case 'Date':
215 case 'Timestamp':
216 // a null date or timestamp is valid
217 if (strlen(trim($data)) == 0) {
218 return trim($data);
219 }
220
221 if ((preg_match('/^\d{8}$/', $data) ||
222 preg_match('/^\d{14}$/', $data)
223 ) &&
224 CRM_Utils_Rule::mysqlDate($data)
225 ) {
226 return $data;
227 }
228 break;
229
230 case 'ContactReference':
231 if (strlen(trim($data)) == 0) {
232 return trim($data);
233 }
234
235 if (CRM_Utils_Rule::validContact($data)) {
236 return $data;
237 }
238 break;
239
240 default:
241 CRM_Core_Error::fatal("Cannot recognize $type for $data");
242 break;
243 }
244
245 if ($abort) {
246 $data = htmlentities($data);
247 CRM_Core_Error::fatal("$data is not of the type $type");
248 }
249 return NULL;
250 }
251
252 /**
253 * Verify that a variable is of a given type
254 *
255 * @param mixed $data The variable
256 * @param string $type The type
257 * @param boolean $abort Should we abort if invalid
450f494d 258 * @name string $name The name of the attribute
6a488035
TO
259 *
260 * @return mixed The data, escaped if necessary
261 * @access public
262 * @static
263 */
264 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
265 switch ($type) {
266 case 'Integer':
267 case 'Int':
268 if (CRM_Utils_Rule::integer($data)) {
269 return $data;
270 }
271 break;
272
273 case 'Positive':
274 if (CRM_Utils_Rule::positiveInteger($data)) {
275 return $data;
276 }
277 break;
278
279 case 'Boolean':
280 if (CRM_Utils_Rule::boolean($data)) {
281 return $data;
282 }
283 break;
284
285 case 'Float':
286 case 'Money':
287 if (CRM_Utils_Rule::numeric($data)) {
288 return $data;
289 }
290 break;
291
292 case 'Text':
293 case 'String':
294 case 'Link':
295 case 'Memo':
296 return $data;
297
298 case 'Date':
299 // a null date is valid
300 if (strlen(trim($data)) == 0) {
301 return trim($data);
302 }
303
304 if (preg_match('/^\d{8}$/', $data) &&
305 CRM_Utils_Rule::mysqlDate($data)
306 ) {
307 return $data;
308 }
309 break;
310
311 case 'Timestamp':
312 // a null timestamp is valid
313 if (strlen(trim($data)) == 0) {
314 return trim($data);
315 }
316
317 if ((preg_match('/^\d{14}$/', $data) ||
318 preg_match('/^\d{8}$/', $data)
319 ) &&
320 CRM_Utils_Rule::mysqlDate($data)
321 ) {
322 return $data;
323 }
324 break;
325
326 case 'ContactReference':
327 // null is valid
328 if (strlen(trim($data)) == 0) {
329 return trim($data);
330 }
331
332 if (CRM_Utils_Rule::validContact($data)) {
333 return $data;
334 }
335 break;
336
337 default:
338 CRM_Core_Error::fatal("Cannot recognize $type for $data");
339 break;
340 }
341
342 if ($abort) {
343 $data = htmlentities($data);
344 CRM_Core_Error::fatal("$name (value: $data) is not of the type $type");
345 }
346
347 return NULL;
348 }
349}
350