3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
33 class CRM_Utils_Type
{
52 // @todo What's the point of these constants? Backwards compatibility?
68 * Gets the string representation for a data type.
71 * Integer number identifying the data type.
74 * String identifying the data type, e.g. 'Int' or 'String'.
76 public static function typeToString($type) {
77 // @todo Use constants in the case statements, e.g. "case T_INT:".
78 // @todo return directly, instead of assigning a value.
79 // @todo Use a lookup array, as a property or as a local variable.
116 $string = 'Timestamp';
136 $string = 'Mediumblob';
140 return (isset($string)) ?
$string : "";
144 * Verify that a variable is of a given type, and apply a bit of processing.
147 * The value to be verified/escaped.
148 * @param string $type
149 * The type to verify against.
151 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
154 * The data, escaped if necessary.
156 public static function escape($data, $type, $abort = TRUE) {
160 if (CRM_Utils_Rule
::integer($data)) {
166 if (CRM_Utils_Rule
::positiveInteger($data)) {
171 // CRM-8925 for custom fields of this type
173 case 'StateProvince':
174 // Handle multivalued data in delimited or array format
175 if (is_array($data) ||
(strpos($data, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE)) {
177 foreach (CRM_Utils_Array
::explodePadded($data) as $item) {
178 if (!CRM_Utils_Rule
::positiveInteger($item)) {
186 elseif (CRM_Utils_Rule
::positiveInteger($data)) {
192 if (CRM_Utils_Rule
::positiveInteger($data)) {
198 if (CRM_Utils_Rule
::url($data = trim($data))) {
204 if (CRM_Utils_Rule
::boolean($data)) {
211 if (CRM_Utils_Rule
::numeric($data)) {
219 return CRM_Core_DAO
::escapeString($data);
223 // a null date or timestamp is valid
224 if (strlen(trim($data)) == 0) {
228 if ((preg_match('/^\d{8}$/', $data) ||
229 preg_match('/^\d{14}$/', $data)
231 CRM_Utils_Rule
::mysqlDate($data)
237 case 'ContactReference':
238 if (strlen(trim($data)) == 0) {
242 if (CRM_Utils_Rule
::validContact($data)) {
248 CRM_Core_Error
::fatal(
249 $type . " is not a recognised (camel cased) data type."
254 // @todo Use exceptions instead of CRM_Core_Error::fatal().
256 $data = htmlentities($data);
257 CRM_Core_Error
::fatal("$data is not of the type $type");
263 * Verify that a variable is of a given type.
266 * The value to validate.
267 * @param string $type
268 * The type to validate against.
270 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
272 * The name of the attribute
275 * The data, escaped if necessary
277 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
281 if (CRM_Utils_Rule
::integer($data)) {
287 if (CRM_Utils_Rule
::positiveInteger($data)) {
293 if (CRM_Utils_Rule
::boolean($data)) {
300 if (CRM_Utils_Rule
::numeric($data)) {
312 // a null date is valid
313 if (strlen(trim($data)) == 0) {
317 if (preg_match('/^\d{8}$/', $data) &&
318 CRM_Utils_Rule
::mysqlDate($data)
325 // a null timestamp is valid
326 if (strlen(trim($data)) == 0) {
330 if ((preg_match('/^\d{14}$/', $data) ||
331 preg_match('/^\d{8}$/', $data)
333 CRM_Utils_Rule
::mysqlDate($data)
339 case 'ContactReference':
341 if (strlen(trim($data)) == 0) {
345 if (CRM_Utils_Rule
::validContact($data)) {
351 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
356 $data = htmlentities($data);
357 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");