X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FRule.php;h=4730d930a9fce9ede3cbee24c5d55b7cef0c40fd;hb=d44c681d9105af668449d16d9f53832d7982f47e;hp=89ee7718657ea599da2da4898d4e2ef3d8638c8e;hpb=504e40d849d89dffb3d851e49cbb92c4d7b70795;p=civicrm-core.git diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index 89ee771865..4730d930a9 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -1,9 +1,9 @@ 31) { @@ -70,6 +89,11 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $str + * + * @return bool + */ static function qfVariable($str) { // check length etc //if ( empty( $str ) || strlen( $str ) > 31 ) { @@ -86,6 +110,11 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $phone + * + * @return bool + */ static function phone($phone) { // check length etc if (empty($phone) || strlen($phone) > 16) { @@ -99,6 +128,11 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $query + * + * @return bool + */ static function query($query) { // check length etc if (empty($query) || strlen($query) < 3 || strlen($query) > 127) { @@ -113,15 +147,30 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $url + * + * @return bool + */ static function url($url) { return (bool) filter_var($url, FILTER_VALIDATE_URL); } + /** + * @param $string + * + * @return bool + */ static function wikiURL($string) { $items = explode(' ', trim($string), 2); return self::url($items[0]); } + /** + * @param $domain + * + * @return bool + */ static function domain($domain) { // not perfect, but better than the previous one; see CRM-1502 if (!preg_match('/^[A-Za-z0-9]([A-Za-z0-9\.\-]*[A-Za-z0-9])?$/', $domain)) { @@ -130,6 +179,12 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $value + * @param null $default + * + * @return null + */ static function date($value, $default = NULL) { if (is_string($value) && preg_match('/^\d\d\d\d-?\d\d-?\d\d$/', $value) @@ -139,6 +194,12 @@ class CRM_Utils_Rule { return $default; } + /** + * @param $value + * @param null $default + * + * @return null|string + */ static function dateTime($value, $default = NULL) { $result = $default; if (is_string($value) && @@ -268,6 +329,11 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $value + * + * @return bool + */ static function integer($value) { if (is_int($value)) { return TRUE; @@ -296,6 +362,11 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $value + * + * @return bool + */ static function positiveInteger($value) { if (is_int($value)) { return ($value < 0) ? FALSE : TRUE; @@ -314,6 +385,11 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $value + * + * @return bool + */ static function numeric($value) { // lets use a php gatekeeper to ensure this is numeric if (!is_numeric($value)) { @@ -323,16 +399,36 @@ class CRM_Utils_Rule { return preg_match('/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/', $value) ? TRUE : FALSE; } + /** + * @param $value + * @param $noOfDigit + * + * @return bool + */ static function numberOfDigit($value, $noOfDigit) { return preg_match('/^\d{' . $noOfDigit . '}$/', $value) ? TRUE : FALSE; } + /** + * @param $value + * + * @return mixed + */ static function cleanMoney($value) { // first remove all white space $value = str_replace(array(' ', "\t", "\n"), '', $value); $config = CRM_Core_Config::singleton(); + //CRM-14868 + $currencySymbols = CRM_Core_PseudoConstant::get( + 'CRM_Contribute_DAO_Contribution', + 'currency', array( + 'keyColumn' => 'name', + 'labelColumn' => 'symbol' + )); + $value = str_replace($currencySymbols,'',$value); + if ($config->monetaryThousandSeparator) { $mon_thousands_sep = $config->monetaryThousandSeparator; } @@ -358,6 +454,11 @@ class CRM_Utils_Rule { return $value; } + /** + * @param $value + * + * @return bool + */ static function money($value) { $config = CRM_Core_Config::singleton(); @@ -381,6 +482,12 @@ class CRM_Utils_Rule { return preg_match('/(^-?\d+\.\d?\d?$)|(^-?\.\d\d?$)/', $value) ? TRUE : FALSE; } + /** + * @param $value + * @param int $maxLength + * + * @return bool + */ static function string($value, $maxLength = 0) { if (is_string($value) && ($maxLength === 0 || strlen($value) <= $maxLength) @@ -390,16 +497,31 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $value + * + * @return bool + */ static function boolean($value) { return preg_match( '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value ) ? TRUE : FALSE; } + /** + * @param $value + * + * @return bool + */ static function email($value) { return (bool) filter_var($value, FILTER_VALIDATE_EMAIL); } + /** + * @param $list + * + * @return bool + */ static function emailList($list) { $emails = explode(',', $list); foreach ($emails as $email) { @@ -414,6 +536,11 @@ class CRM_Utils_Rule { // allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or // if u disregard the first 0, 4 (thanx excel!) // FIXME: we need to figure out how to localize such rules + /** + * @param $value + * + * @return bool + */ static function postalCode($value) { if (preg_match('/^\d{4,6}(-\d{4})?$/', $value)) { return TRUE; @@ -505,21 +632,44 @@ class CRM_Utils_Rule { return CRM_Core_DAO::objectExists($value, CRM_Utils_Array::value(0, $options), CRM_Utils_Array::value(1, $options), CRM_Utils_Array::value(2, $options, $name)); } + /** + * @param $value + * @param $options + * + * @return bool + */ static function optionExists($value, $options) { return CRM_Core_OptionValue::optionExists($value, $options[0], $options[1], $options[2], CRM_Utils_Array::value(3, $options, 'name')); } + /** + * @param $value + * @param $type + * + * @return bool + */ static function creditCardNumber($value, $type) { require_once 'Validate/Finance/CreditCard.php'; return Validate_Finance_CreditCard::number($value, $type); } + /** + * @param $value + * @param $type + * + * @return bool + */ static function cvv($value, $type) { require_once 'Validate/Finance/CreditCard.php'; return Validate_Finance_CreditCard::cvv($value, $type); } + /** + * @param $value + * + * @return bool + */ static function currencyCode($value) { static $currencyCodes = NULL; if (!$currencyCodes) { @@ -531,6 +681,11 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $value + * + * @return bool + */ static function xssString($value) { if (is_string($value)) { return preg_match('!<(vb)?script[^>]*>.*!ims', @@ -542,10 +697,21 @@ class CRM_Utils_Rule { } } + /** + * @param $path + * + * @return bool + */ static function fileExists($path) { return file_exists($path); } + /** + * @param $value + * @param $options + * + * @return bool + */ static function autocomplete($value, $options) { if ($value) { $selectOption = CRM_Core_BAO_CustomOption::valuesByID($options['fieldID'], $options['optionGroupID']); @@ -557,6 +723,12 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $value + * @param null $actualElementValue + * + * @return bool + */ static function validContact($value, $actualElementValue = NULL) { if ($actualElementValue) { $value = $actualElementValue; @@ -621,6 +793,11 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $key + * + * @return bool + */ static function qfKey($key) { return ($key) ? CRM_Core_Key::valid($key) : FALSE; }