public static function CRM_Utils_Type::validate in CiviCRM Entity 8.3
Verify that a variable is of a given type.
@name string $name The name of the attribute
Parameters
mixed $data: The value to validate.
string $type: The type to validate against.
bool $abort: If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
Return value
mixed The data, escaped if necessary
1 call to CRM_Utils_Type::validate()
- CRM_Utils_Type::validateAll in tests/
src/ Type.php - Helper function to call validate on arrays
File
- tests/
src/ Type.php, line 379
Class
- CRM_Utils_Type
- @package CRM @copyright CiviCRM LLC (c) 2004-2017
Code
public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ') {
switch ($type) {
case 'Integer':
case 'Int':
if (CRM_Utils_Rule::integer($data)) {
return (int) $data;
}
break;
case 'Positive':
if (CRM_Utils_Rule::positiveInteger($data)) {
return (int) $data;
}
break;
case 'Boolean':
if (CRM_Utils_Rule::boolean($data)) {
return $data;
}
break;
case 'Float':
case 'Money':
if (CRM_Utils_Rule::numeric($data)) {
return $data;
}
break;
case 'Text':
case 'String':
case 'Link':
case 'Memo':
return $data;
case 'Date':
// a null date is valid
if (strlen(trim($data)) == 0) {
return trim($data);
}
if (preg_match('/^\\d{8}$/', $data) && CRM_Utils_Rule::mysqlDate($data)) {
return $data;
}
break;
case 'Timestamp':
// a null timestamp is valid
if (strlen(trim($data)) == 0) {
return trim($data);
}
if ((preg_match('/^\\d{14}$/', $data) || preg_match('/^\\d{8}$/', $data)) && CRM_Utils_Rule::mysqlDate($data)) {
return $data;
}
break;
case 'ContactReference':
// null is valid
if (strlen(trim($data)) == 0) {
return trim($data);
}
if (CRM_Utils_Rule::validContact($data)) {
return $data;
}
break;
case 'MysqlColumnNameOrAlias':
if (CRM_Utils_Rule::mysqlColumnNameOrAlias($data)) {
return $data;
}
break;
case 'MysqlOrderByDirection':
if (CRM_Utils_Rule::mysqlOrderByDirection($data)) {
return strtolower($data);
}
break;
case 'MysqlOrderBy':
if (CRM_Utils_Rule::mysqlOrderBy($data)) {
return $data;
}
break;
case 'ExtensionKey':
if (CRM_Utils_Rule::checkExtesnionKeyIsValid($data)) {
return $data;
}
break;
default:
CRM_Core_Error::fatal("Cannot recognize {$type} for {$data}");
break;
}
if ($abort) {
$data = htmlentities($data);
CRM_Core_Error::fatal("{$name} (value: {$data}) is not of the type {$type}");
}
return NULL;
}