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
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.
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(
254 $type . " is not a recognised (camel cased) data type."
259 // @todo Use exceptions instead of CRM_Core_Error::fatal().
261 $data = htmlentities($data);
262 CRM_Core_Error
::fatal("$data is not of the type $type");
268 * Verify that a variable is of a given type.
271 * The value to validate.
272 * @param string $type
273 * The type to validate against.
275 * If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
277 * The name of the attribute
280 * The data, escaped if necessary
282 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
286 if (CRM_Utils_Rule
::integer($data)) {
292 if (CRM_Utils_Rule
::positiveInteger($data)) {
298 if (CRM_Utils_Rule
::boolean($data)) {
305 if (CRM_Utils_Rule
::numeric($data)) {
317 // a null date is valid
318 if (strlen(trim($data)) == 0) {
322 if (preg_match('/^\d{8}$/', $data) &&
323 CRM_Utils_Rule
::mysqlDate($data)
330 // a null timestamp is valid
331 if (strlen(trim($data)) == 0) {
335 if ((preg_match('/^\d{14}$/', $data) ||
336 preg_match('/^\d{8}$/', $data)
338 CRM_Utils_Rule
::mysqlDate($data)
344 case 'ContactReference':
346 if (strlen(trim($data)) == 0) {
350 if (CRM_Utils_Rule
::validContact($data)) {
356 CRM_Core_Error
::fatal("Cannot recognize $type for $data");
361 $data = htmlentities($data);
362 CRM_Core_Error
::fatal("$name (value: $data) is not of the type $type");