return (isset($string)) ? $string : "";
}
+ /**
+ * @return array
+ * An array of type in the form 'type name' => 'int representing type'
+ */
+ public static function getValidTypes() {
+ return array(
+ 'Int' => self::T_INT,
+ 'String' => self::T_STRING,
+ 'Enum' => self::T_ENUM,
+ 'Date' => self::T_DATE,
+ 'Time' => self::T_TIME,
+ 'Boolean' => self::T_BOOLEAN,
+ 'Text' => self::T_TEXT,
+ 'Blob' => self::T_BLOB,
+ 'Timestamp' => self::T_TIMESTAMP,
+ 'Float' => self::T_FLOAT,
+ 'Money' => self::T_MONEY,
+ 'Email' => self::T_EMAIL,
+ 'Mediumblob' => self::T_MEDIUMBLOB,
+ );
+ }
+
/**
* Get the data_type for the field.
*
* The type to validate against.
* @param bool $abort
* If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
- * @name string $name
+ * @param string $name
* The name of the attribute
+ * @param bool $isThrowException
+ * Should an exception be thrown rather than a using a deprecated fatal error.
*
* @return mixed
* The data, escaped if necessary
+ *
+ * @throws \CRM_Core_Exception
*/
- public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
+ public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ', $isThrowException = FALSE) {
+
+ $possibleTypes = array(
+ 'Integer',
+ 'Int',
+ 'Positive',
+ 'CommaSeparatedIntegers',
+ 'Boolean',
+ 'Float',
+ 'Money',
+ 'Text',
+ 'String',
+ 'Link',
+ 'Memo',
+ 'Date',
+ 'Timestamp',
+ 'ContactReference',
+ 'MysqlColumnNameOrAlias',
+ 'MysqlOrderByDirection',
+ 'MysqlOrderBy',
+ 'ExtensionKey',
+ );
+ if (!in_array($type, $possibleTypes)) {
+ if ($isThrowException) {
+ throw new CRM_Core_Exception(ts('Invalid type, must be one of : ' . implode($possibleTypes)));
+ }
+ CRM_Core_Error::fatal(ts('Invalid type, must be one of : ' . implode($possibleTypes)));
+ }
switch ($type) {
case 'Integer':
case 'Int':
}
break;
+ case 'CommaSeparatedIntegers':
+ if (CRM_Utils_Rule::commaSeparatedIntegers($data)) {
+ return $data;
+ }
+ break;
+
case 'Boolean':
if (CRM_Utils_Rule::boolean($data)) {
return $data;
return $data;
}
break;
-
- default:
- CRM_Core_Error::fatal("Cannot recognize $type for $data");
- break;
}
if ($abort) {
$data = htmlentities($data);
+ if ($isThrowException) {
+ throw new CRM_Core_Exception("$name (value: $data) is not of the type $type");
+ }
CRM_Core_Error::fatal("$name (value: $data) is not of the type $type");
}
}
/**
- * Get list of avaliable Data Tupes for Option Groups
+ * Get list of avaliable Data Types for Option Groups
*
* @return array
*/