CRM-12595 fix formatting in CRM/Utils files
[civicrm-core.git] / CRM / Utils / Type.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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,
42 T_BOOL = 16,
43 T_BOOLEAN = 16,
44 T_TEXT = 32,
45 T_LONGTEXT = 32,
46 T_BLOB = 64,
47 T_TIMESTAMP = 256,
48 T_FLOAT = 512,
49 T_MONEY = 1024,
50 T_EMAIL = 2048,
51 T_URL = 4096,
52 T_CCNUM = 8192,
53 T_MEDIUMBLOB = 16384;
54
55 CONST
56 TWO = 2,
57 FOUR = 4,
58 SIX = 6,
59 EIGHT = 8,
60 TWELVE = 12,
61 SIXTEEN = 16,
62 TWENTY = 20,
63 MEDIUM = 20,
64 THIRTY = 30,
65 BIG = 30,
66 FORTYFIVE = 45,
67 HUGE = 45;
68
69 /**
70 * Convert Constant Data type to String
71 *
72 * @param $type integer datatype
73 *
74 * @return $string String datatype respective to integer datatype
75 *
76 * @access public
77 * @static
78 */
79 static function typeToString($type) {
80 switch ($type) {
81 case 1:
82 $string = 'Int';
83 break;
84
85 case 2:
86 $string = 'String';
87 break;
88
89 case 3:
90 $string = 'Enum';
91 break;
92
93 case 4:
94 $string = 'Date';
95 break;
96
97 case 8:
98 $string = 'Time';
99 break;
100
101 case 16:
102 $string = 'Boolean';
103 break;
104
105 case 32:
106 $string = 'Text';
107 break;
108
109 case 64:
110 $string = 'Blob';
111 break;
112
113 // CRM-10404
114 case 12:
115 case 256:
116 $string = 'Timestamp';
117 break;
118
119 case 512:
120 $string = 'Float';
121 break;
122
123 case 1024:
124 $string = 'Money';
125 break;
126
127 case 2048:
128 $string = 'Date';
129 break;
130
131 case 4096:
132 $string = 'Email';
133 break;
134
135 case 16384:
136 $string = 'Mediumblob';
137 break;
138 }
139
140 return (isset($string)) ? $string : "";
141 }
142
143 /**
144 * Verify that a variable is of a given type
145 *
146 * @param mixed $data The variable
147 * @param string $type The type
148 * @param boolean $abort Should we abort if invalid
149 *
150 * @return mixed The data, escaped if necessary
151 * @access public
152 * @static
153 */
154 public static function escape($data, $type, $abort = TRUE) {
155 switch ($type) {
156 case 'Integer':
157 case 'Int':
158 if (CRM_Utils_Rule::integer($data)) {
159 return $data;
160 }
161 break;
162
163 case 'Positive':
164 // the below 2 are for custom fields of this type
165 // CRM-8925
166 case 'Country':
167 case 'StateProvince':
168 if (CRM_Utils_Rule::positiveInteger($data)) {
169 return $data;
170 }
171 break;
172
173 case 'Link':
174 if (CRM_Utils_Rule::url($data = trim($data))) {
175 return $data;
176 }
177 break;
178
179 case 'Boolean':
180 if (CRM_Utils_Rule::boolean($data)) {
181 return $data;
182 }
183 break;
184
185 case 'Float':
186 case 'Money':
187 if (CRM_Utils_Rule::numeric($data)) {
188 return $data;
189 }
190 break;
191
192 case 'String':
193 case 'Memo':
194 return CRM_Core_DAO::escapeString($data);
195
196 case 'Date':
197 case 'Timestamp':
198 // a null date or timestamp is valid
199 if (strlen(trim($data)) == 0) {
200 return trim($data);
201 }
202
203 if ((preg_match('/^\d{8}$/', $data) ||
204 preg_match('/^\d{14}$/', $data)
205 ) &&
206 CRM_Utils_Rule::mysqlDate($data)
207 ) {
208 return $data;
209 }
210 break;
211
212 case 'ContactReference':
213 if (strlen(trim($data)) == 0) {
214 return trim($data);
215 }
216
217 if (CRM_Utils_Rule::validContact($data)) {
218 return $data;
219 }
220 break;
221
222 default:
223 CRM_Core_Error::fatal("Cannot recognize $type for $data");
224 break;
225 }
226
227 if ($abort) {
228 $data = htmlentities($data);
229 CRM_Core_Error::fatal("$data is not of the type $type");
230 }
231 return NULL;
232 }
233
234 /**
235 * Verify that a variable is of a given type
236 *
237 * @param mixed $data The variable
238 * @param string $type The type
239 * @param boolean $abort Should we abort if invalid
450f494d 240 * @name string $name The name of the attribute
6a488035
TO
241 *
242 * @return mixed The data, escaped if necessary
243 * @access public
244 * @static
245 */
246 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
247 switch ($type) {
248 case 'Integer':
249 case 'Int':
250 if (CRM_Utils_Rule::integer($data)) {
251 return $data;
252 }
253 break;
254
255 case 'Positive':
256 if (CRM_Utils_Rule::positiveInteger($data)) {
257 return $data;
258 }
259 break;
260
261 case 'Boolean':
262 if (CRM_Utils_Rule::boolean($data)) {
263 return $data;
264 }
265 break;
266
267 case 'Float':
268 case 'Money':
269 if (CRM_Utils_Rule::numeric($data)) {
270 return $data;
271 }
272 break;
273
274 case 'Text':
275 case 'String':
276 case 'Link':
277 case 'Memo':
278 return $data;
279
280 case 'Date':
281 // a null date is valid
282 if (strlen(trim($data)) == 0) {
283 return trim($data);
284 }
285
286 if (preg_match('/^\d{8}$/', $data) &&
287 CRM_Utils_Rule::mysqlDate($data)
288 ) {
289 return $data;
290 }
291 break;
292
293 case 'Timestamp':
294 // a null timestamp is valid
295 if (strlen(trim($data)) == 0) {
296 return trim($data);
297 }
298
299 if ((preg_match('/^\d{14}$/', $data) ||
300 preg_match('/^\d{8}$/', $data)
301 ) &&
302 CRM_Utils_Rule::mysqlDate($data)
303 ) {
304 return $data;
305 }
306 break;
307
308 case 'ContactReference':
309 // null is valid
310 if (strlen(trim($data)) == 0) {
311 return trim($data);
312 }
313
314 if (CRM_Utils_Rule::validContact($data)) {
315 return $data;
316 }
317 break;
318
319 default:
320 CRM_Core_Error::fatal("Cannot recognize $type for $data");
321 break;
322 }
323
324 if ($abort) {
325 $data = htmlentities($data);
326 CRM_Core_Error::fatal("$name (value: $data) is not of the type $type");
327 }
328
329 return NULL;
330 }
331}
332