3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Utils_Type
{
54 // @todo What's the point of these constants? Backwards compatibility?
70 * Gets the string representation for a data type.
73 * Integer number identifying the data type.
76 * String identifying the data type, e.g. 'Int' or 'String'.
78 public static function typeToString($type) {
79 // @todo Use constants in the case statements, e.g. "case T_INT:".
80 // @todo return directly, instead of assigning a value.
81 // @todo Use a lookup array, as a property or as a local variable.
118 $string = 'Timestamp';
138 $string = 'Mediumblob';
142 return (isset($string)) ?
$string : "";
146 * Verify that a variable is of a given type, and apply a bit of processing.
149 * The value to be verified/escaped.
150 * @param string $type
151 * The type to verify against.
152 * @param boolean $abort
153 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
156 * The data, escaped if necessary.
158 public static function escape($data, $type, $abort = TRUE) {
162 if (CRM_Utils_Rule
::integer($data)) {
168 // CRM-8925 the 3 below are for custom fields of this type
170 case 'StateProvince':
171 // Checked for multi valued state/country value
172 if (is_array($data)) {
174 // @todo Reuse of the $data variable = asking for trouble.
175 // @todo This code will always return the last item in the array. Intended?
176 foreach ($data as $data) {
177 if (CRM_Utils_Rule
::positiveInteger($data) || CRM_Core_DAO
::escapeString($data)) {
188 elseif (!is_numeric($data) && CRM_Core_DAO
::escapeString($data)) {
191 elseif (CRM_Utils_Rule
::positiveInteger($data)) {
197 if (CRM_Utils_Rule
::positiveInteger($data)) {
203 if (CRM_Utils_Rule
::url($data = trim($data))) {
209 if (CRM_Utils_Rule
::boolean($data)) {
216 if (CRM_Utils_Rule
::numeric($data)) {
224 return CRM_Core_DAO
::escapeString($data);
228 // a null date or timestamp is valid
229 if (strlen(trim($data)) == 0) {
233 if ((preg_match('/^\d{8}$/', $data) ||
234 preg_match('/^\d{14}$/', $data)
236 CRM_Utils_Rule
::mysqlDate($data)
242 case 'ContactReference':
243 if (strlen(trim($data)) == 0) {
247 if (CRM_Utils_Rule
::validContact($data)) {
253 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
257 // @todo Use exceptions instead of CRM_Core_Error::fatal().
259 $data = htmlentities($data);
260 CRM_Core_Error
::fatal("$data is not of the type $type");
266 * Verify that a variable is of a given type
269 * The value to validate.
270 * @param string $type
271 * The type to validate against.
272 * @param boolean $abort
273 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
275 * The name of the attribute
278 * The data, escaped if necessary
280 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
284 if (CRM_Utils_Rule
::integer($data)) {
290 if (CRM_Utils_Rule
::positiveInteger($data)) {
296 if (CRM_Utils_Rule
::boolean($data)) {
303 if (CRM_Utils_Rule
::numeric($data)) {
315 // a null date is valid
316 if (strlen(trim($data)) == 0) {
320 if (preg_match('/^\d{8}$/', $data) &&
321 CRM_Utils_Rule
::mysqlDate($data)
328 // a null timestamp is valid
329 if (strlen(trim($data)) == 0) {
333 if ((preg_match('/^\d{14}$/', $data) ||
334 preg_match('/^\d{8}$/', $data)
336 CRM_Utils_Rule
::mysqlDate($data)
342 case 'ContactReference':
344 if (strlen(trim($data)) == 0) {
348 if (CRM_Utils_Rule
::validContact($data)) {
354 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
359 $data = htmlentities($data);
360 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");