Cleanup phpdoc comments
[civicrm-core.git] / CRM / Utils / Type.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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 *
77b97be7 73 * @return string $string String datatype respective to integer datatype@access public
6a488035
TO
74 * @static
75 */
76 static function typeToString($type) {
77 switch ($type) {
78 case 1:
79 $string = 'Int';
80 break;
81
82 case 2:
83 $string = 'String';
84 break;
85
86 case 3:
87 $string = 'Enum';
88 break;
89
90 case 4:
91 $string = 'Date';
92 break;
93
94 case 8:
95 $string = 'Time';
96 break;
97
98 case 16:
99 $string = 'Boolean';
100 break;
101
102 case 32:
103 $string = 'Text';
104 break;
105
106 case 64:
107 $string = 'Blob';
108 break;
109
110 // CRM-10404
111 case 12:
112 case 256:
113 $string = 'Timestamp';
114 break;
115
116 case 512:
117 $string = 'Float';
118 break;
119
120 case 1024:
121 $string = 'Money';
122 break;
123
124 case 2048:
125 $string = 'Date';
126 break;
127
128 case 4096:
129 $string = 'Email';
130 break;
131
132 case 16384:
133 $string = 'Mediumblob';
134 break;
135 }
136
137 return (isset($string)) ? $string : "";
138 }
139
140 /**
141 * Verify that a variable is of a given type
142 *
143 * @param mixed $data The variable
144 * @param string $type The type
145 * @param boolean $abort Should we abort if invalid
146 *
147 * @return mixed The data, escaped if necessary
148 * @access public
149 * @static
150 */
151 public static function escape($data, $type, $abort = TRUE) {
152 switch ($type) {
153 case 'Integer':
154 case 'Int':
155 if (CRM_Utils_Rule::integer($data)) {
156 return $data;
157 }
158 break;
159
160 case 'Positive':
e7dcccf0 161 // CRM-8925 the 3 below are for custom fields of this type
6a488035
TO
162 case 'Country':
163 case 'StateProvince':
9ff5f6c0
N
164 // Checked for multi valued state/country value
165 if (is_array($data)) {
166 $returnData = TRUE;
167 foreach ($data as $data) {
168 if (CRM_Utils_Rule::positiveInteger($data) || CRM_Core_DAO::escapeString($data)) {
169 $returnData = TRUE;
170 }
171 else {
172 $returnData = FALSE;
173 }
174 }
175 if ($returnData) {
176 return $data;
177 }
178 }
179 elseif (!is_numeric($data) && CRM_Core_DAO::escapeString($data)) {
180 return $data;
181 }
182 elseif (CRM_Utils_Rule::positiveInteger($data)) {
6a488035
TO
183 return $data;
184 }
185 break;
186
e7dcccf0 187 case 'File':
6a488035
TO
188 if (CRM_Utils_Rule::positiveInteger($data)) {
189 return $data;
190 }
191 break;
192
193 case 'Link':
194 if (CRM_Utils_Rule::url($data = trim($data))) {
195 return $data;
196 }
197 break;
198
199 case 'Boolean':
200 if (CRM_Utils_Rule::boolean($data)) {
201 return $data;
202 }
203 break;
204
205 case 'Float':
206 case 'Money':
207 if (CRM_Utils_Rule::numeric($data)) {
208 return $data;
209 }
210 break;
211
212 case 'String':
213 case 'Memo':
85bdc94e 214 case 'Text':
6a488035
TO
215 return CRM_Core_DAO::escapeString($data);
216
217 case 'Date':
218 case 'Timestamp':
219 // a null date or timestamp is valid
220 if (strlen(trim($data)) == 0) {
221 return trim($data);
222 }
223
224 if ((preg_match('/^\d{8}$/', $data) ||
225 preg_match('/^\d{14}$/', $data)
226 ) &&
227 CRM_Utils_Rule::mysqlDate($data)
228 ) {
229 return $data;
230 }
231 break;
232
233 case 'ContactReference':
234 if (strlen(trim($data)) == 0) {
235 return trim($data);
236 }
237
238 if (CRM_Utils_Rule::validContact($data)) {
239 return $data;
240 }
241 break;
242
243 default:
244 CRM_Core_Error::fatal("Cannot recognize $type for $data");
245 break;
246 }
247
248 if ($abort) {
249 $data = htmlentities($data);
250 CRM_Core_Error::fatal("$data is not of the type $type");
251 }
252 return NULL;
253 }
254
255 /**
256 * Verify that a variable is of a given type
257 *
258 * @param mixed $data The variable
259 * @param string $type The type
260 * @param boolean $abort Should we abort if invalid
450f494d 261 * @name string $name The name of the attribute
6a488035
TO
262 *
263 * @return mixed The data, escaped if necessary
264 * @access public
265 * @static
266 */
267 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
268 switch ($type) {
269 case 'Integer':
270 case 'Int':
271 if (CRM_Utils_Rule::integer($data)) {
272 return $data;
273 }
274 break;
275
276 case 'Positive':
277 if (CRM_Utils_Rule::positiveInteger($data)) {
278 return $data;
279 }
280 break;
281
282 case 'Boolean':
283 if (CRM_Utils_Rule::boolean($data)) {
284 return $data;
285 }
286 break;
287
288 case 'Float':
289 case 'Money':
290 if (CRM_Utils_Rule::numeric($data)) {
291 return $data;
292 }
293 break;
294
295 case 'Text':
296 case 'String':
297 case 'Link':
298 case 'Memo':
299 return $data;
300
301 case 'Date':
302 // a null date is valid
303 if (strlen(trim($data)) == 0) {
304 return trim($data);
305 }
306
307 if (preg_match('/^\d{8}$/', $data) &&
308 CRM_Utils_Rule::mysqlDate($data)
309 ) {
310 return $data;
311 }
312 break;
313
314 case 'Timestamp':
315 // a null timestamp is valid
316 if (strlen(trim($data)) == 0) {
317 return trim($data);
318 }
319
320 if ((preg_match('/^\d{14}$/', $data) ||
321 preg_match('/^\d{8}$/', $data)
322 ) &&
323 CRM_Utils_Rule::mysqlDate($data)
324 ) {
325 return $data;
326 }
327 break;
328
329 case 'ContactReference':
330 // null is valid
331 if (strlen(trim($data)) == 0) {
332 return trim($data);
333 }
334
335 if (CRM_Utils_Rule::validContact($data)) {
336 return $data;
337 }
338 break;
339
340 default:
341 CRM_Core_Error::fatal("Cannot recognize $type for $data");
342 break;
343 }
344
345 if ($abort) {
346 $data = htmlentities($data);
347 CRM_Core_Error::fatal("$name (value: $data) is not of the type $type");
348 }
349
350 return NULL;
351 }
352}
353