LSformRules : clean code by using LSconfig :: get() helper

This commit is contained in:
Benjamin Renard 2020-02-18 12:31:13 +01:00
parent f32fb31d6e
commit c4687bdfdb
25 changed files with 184 additions and 187 deletions

View file

@ -39,8 +39,7 @@ class LSformRule_alphanumeric extends LSformRule {
*/ */
public static function validate ($value,$options=array(),$formElement) { public static function validate ($value,$options=array(),$formElement) {
if (LSconfig :: get('params.withAccents', false, 'bool', $options)) {
if (isset($options['params']['withAccents']) && $options['params']['withAccents'] == true){
$regex = '/(*UTF8)^[0-9\p{L}]+$/'; $regex = '/(*UTF8)^[0-9\p{L}]+$/';
} }
else { else {

View file

@ -42,14 +42,20 @@ class LSformRule_callable extends LSformRule {
* @return boolean true if the value is valid, false otherwise * @return boolean true if the value is valid, false otherwise
*/ */
public static function validate($value,$options,$formElement) { public static function validate($value,$options,$formElement) {
if (is_callable($options['params']['callable'])) { $callable = LSconfig :: get('params.callable', null, null, $options);
return call_user_func_array($options['params']['callable'],array($value,$options['params'],&$formElement)); if (is_callable($callable))
} return call_user_func_array(
else { $callable,
array(
$value,
LSconfig :: get('params', array(), null, $options),
&$formElement
)
);
LSerror :: addErrorCode('LSformRule_callable_01'); LSerror :: addErrorCode('LSformRule_callable_01');
return False; return False;
} }
}
} }

View file

@ -68,11 +68,12 @@ class LSformRule_compare extends LSformRule {
* @return boolean true si la valeur est valide, false sinon * @return boolean true si la valeur est valide, false sinon
*/ */
public static function validate ($values,$options=array(),$formElement) { public static function validate ($values,$options=array(),$formElement) {
if (!isset($options['params']['operator'])) { $operator = LSconfig :: get('params.operator', null, 'string', $options);
if (!$operator) {
LSerror :: addErrorCode('LSformRule_01',array('type' => 'compare', 'param' => 'operator')); LSerror :: addErrorCode('LSformRule_01',array('type' => 'compare', 'param' => 'operator'));
return; return;
} }
$operator = self :: _findOperator($options['params']['operator']); $operator = self :: _findOperator($operator);
if ('==' != $operator && '!=' != $operator) { if ('==' != $operator && '!=' != $operator) {
$compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);'); $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
} }

View file

@ -38,11 +38,12 @@ class LSformRule_date extends LSformRule {
* @return boolean True si les données sont valide, False sinon. * @return boolean True si les données sont valide, False sinon.
*/ */
public static function validate($value,$options=NULL,$formElement) { public static function validate($value,$options=NULL,$formElement) {
if (!isset($options['params']['format'])) { $format = LSconfig :: get('params.format', null, 'string', $options);
if (is_null($format)) {
LSerror :: addErrorCode('LSformRule_date_01'); LSerror :: addErrorCode('LSformRule_date_01');
return; return;
} }
$date = strptime($value,$options['params']['format']); $date = strptime($value, $format);
if(is_array($date)) { if(is_array($date)) {
$res = mktime($date['tm_hour'],$date['tm_min'],$date['tm_sec'],$date['tm_mon']+1,$date['tm_mday'],$date['tm_year']+1900); $res = mktime($date['tm_hour'],$date['tm_min'],$date['tm_sec'],$date['tm_mon']+1,$date['tm_mday'],$date['tm_year']+1900);
if ((is_int($res)) && ($res != -1) && ($res !== False)) { if ((is_int($res)) && ($res != -1) && ($res !== False)) {

View file

@ -32,16 +32,17 @@ class LSformRule_differentPassword extends LSformRule {
* *
* @param string $values Value to check * @param string $values Value to check
* @param array $options Validation options : * @param array $options Validation options :
* - Other attribute : $options['params']['otherAttributes'] * - Other attribute : $options['params']['otherPasswordAttributes']
* @param object $formElement The linked LSformElement object * @param object $formElement The linked LSformElement object
* *
* @return boolean true si la valeur est valide, false sinon * @return boolean true si la valeur est valide, false sinon
*/ */
public static function validate($value, $options, $formElement) { public static function validate($value, $options, $formElement) {
if (is_array($options) && isset($options['params']['otherPasswordAttributes'])) { $otherPasswordAttributes = LSconfig :: get('params.otherPasswordAttributes', null, null, $options);
if (!is_null($otherPasswordAttributes)) {
// Make sure otherPasswordAttributes is an array // Make sure otherPasswordAttributes is an array
if (!is_array($options['params']['otherPasswordAttributes'])) if (!is_array($otherPasswordAttributes))
$options['params']['otherPasswordAttributes'] = array($options['params']['otherPasswordAttributes']); $otherPasswordAttributes = array($otherPasswordAttributes);
// Load LSattr_ldap_password // Load LSattr_ldap_password
if (!LSsession :: loadLSclass("LSattr_ldap_password")) { if (!LSsession :: loadLSclass("LSattr_ldap_password")) {
@ -50,7 +51,7 @@ class LSformRule_differentPassword extends LSformRule {
} }
// Iter on otherPasswordAttributes to check password does not match // Iter on otherPasswordAttributes to check password does not match
foreach($options['params']['otherPasswordAttributes'] as $attr) { foreach($otherPasswordAttributes as $attr) {
// Check attribute exist // Check attribute exist
if (!isset($formElement -> attr_html -> attribute -> ldapObject -> attrs[$attr])) { if (!isset($formElement -> attr_html -> attribute -> ldapObject -> attrs[$attr])) {
LSerror :: addErrorCode('LSformRule_differentPassword_03', $attr); LSerror :: addErrorCode('LSformRule_differentPassword_03', $attr);

View file

@ -38,7 +38,11 @@ class LSformRule_email extends LSformRule {
* @return boolean true si la valeur est valide, false sinon * @return boolean true si la valeur est valide, false sinon
*/ */
public static function validate($value,$options=array(),$formElement) { public static function validate($value,$options=array(),$formElement) {
return checkEmail($value,$options['params']['domain'],$options['params']['checkDomain']); return checkEmail(
$value,
LSconfig :: get('params.domain', NULL, 'string', $options),
LSconfig :: get('params.checkDomain', true, 'bool', $options)
);
} }
} }

View file

@ -43,17 +43,13 @@ class LSformRule_filesize extends LSformRule {
$size = filesize($file); $size = filesize($file);
if (is_int($options['params']['maxSize'])) { $maxSize = LSconfig :: get('params.maxSize', null, 'int', $options);
if ($size > $options['params']['maxSize']) { if (is_int($maxSize) && $size > $maxSize)
return; return;
}
}
if (is_int($options['params']['minSize'])) { $minSize = LSconfig :: get('params.minSize', null, 'int', $options);
if ($size < $options['params']['minSize']) { if (is_int($minSize) && $size < $minSize)
return; return;
}
}
return true; return true;
} }

View file

@ -45,7 +45,9 @@ class LSformRule_imagefile extends LSformRule {
$mimetype = mime_content_type($file); $mimetype = mime_content_type($file);
if ( (!isset($options['params']['mimeType'])) && (!isset($options['params']['mimeTypeRegEx'])) ) { $mimeType = LSconfig :: get('params.mimeType', null, null, $options);
$mimeTypeRegEx = LSconfig :: get('params.mimeTypeRegEx', null, null, $options);
if ( is_null($mimeType) && is_null($mimeTypeRegEx)) {
$options = array( $options = array(
'params' => array( 'params' => array(
'mimeTypeRegEx' => '/image\/.*/' 'mimeTypeRegEx' => '/image\/.*/'

View file

@ -44,26 +44,21 @@ class LSformRule_imagesize extends LSformRule {
$file = LSsession :: getTmpFile($value); $file = LSsession :: getTmpFile($value);
list($width, $height, $type, $attr) = getimagesize($file); list($width, $height, $type, $attr) = getimagesize($file);
if (is_int($options['params']['maxWidth'])) { $maxWidth = LSconfig :: get('params.maxWidth', null, 'int', $options);
if ($width > $options['params']['maxWidth']) { if (is_int($maxWidth) && $width > $maxWidth)
return; return;
}
} $minWidth = LSconfig :: get('params.minWidth', null, 'int', $options);
if (is_int($options['params']['minWidth'])) { if (is_int($minWidth) && $width < $minWidth)
if ($width < $options['params']['minWidth']) {
return; return;
}
} $maxHeight = LSconfig :: get('params.maxHeight', null, 'int', $options);
if (is_int($options['params']['maxHeight'])) { if (is_int($maxHeight) && $height > $maxHeight)
if ($height > $options['params']['maxHeight']) {
return; return;
}
} $minHeight = LSconfig :: get('params.minHeight', null, 'int', $options);
if (is_int($options['params']['minHeight'])) { if (is_int($minHeight) && $height < $minHeight)
if ($height < $options['params']['minHeight']) {
return; return;
}
}
return true; return true;
} }

View file

@ -38,11 +38,12 @@ class LSformRule_inarray extends LSformRule {
* @return boolean true si la valeur est valide, false sinon * @return boolean true si la valeur est valide, false sinon
*/ */
public static function validate($value,$option,$formElement) { public static function validate($value,$option,$formElement) {
if (!isset($option['params']['possible_values']) || !is_array($option['params']['possible_values'])) { $possible_values = LSconfig :: get('params.possible_values', null, null, $options);
if (!is_array($possible_values)) {
LSerror :: addErrorCode('LSformRule_inarray_01'); LSerror :: addErrorCode('LSformRule_inarray_01');
return; return;
} }
if (!in_array($value,$option['params']['possible_values'])) if (!in_array($value, $possible_values))
return false; return false;
return true; return true;
} }

View file

@ -41,16 +41,18 @@ class LSformRule_integer extends LSformRule{
* @return boolean true if the value is valided, false otherwise * @return boolean true if the value is valided, false otherwise
*/ */
public static function validate ($value,$options=array(),$formElement) { public static function validate ($value,$options=array(),$formElement) {
if($options['params']['max'] && $value > $options['params']['max']) { $max = LSconfig :: get('params.max', null, 'int', $options);
if(is_int($max) && $value > $max)
return; return;
}
if($options['params']['min'] && $value < $options['params']['min']) { $min = LSconfig :: get('params.min', null, 'int', $options);
if(is_int($min) && $value < $min)
return; return;
}
if($options['params']['negative']) { if(LSconfig :: get('params.negative', false, 'bool', $options)) {
$regex = '/^-[0-9]*$/'; $regex = '/^-[0-9]*$/';
} }
elseif($options['params']['positive']) { elseif(LSconfig :: get('params.positive', false, 'bool', $options)) {
$regex = '/^[0-9]*$/'; $regex = '/^[0-9]*$/';
} }
else { else {

View file

@ -38,11 +38,12 @@ class LSformRule_maxlength extends LSformRule {
* @return boolean true si la valeur est valide, false sinon * @return boolean true si la valeur est valide, false sinon
*/ */
public static function validate ($value,$options,$formElement) { public static function validate ($value,$options,$formElement) {
if(!isset($options['params']['limit'])) { $limit = LSconfig :: get('params.limit', null, 'int', $options);
if(is_null($limit)) {
LSerror :: addErrorCode('LSformRule_01',array('type' => 'maxlength', 'param' => 'limit')); LSerror :: addErrorCode('LSformRule_01',array('type' => 'maxlength', 'param' => 'limit'));
return; return;
} }
return (strlen($value)<=$options['params']['limit']); return (strlen($value) <= $limit);
} }
} }

View file

@ -40,27 +40,23 @@ class LSformRule_mimetype extends LSformRule {
*/ */
public static function validate ($value,$options,$formElement) { public static function validate ($value,$options,$formElement) {
$file = LSsession :: getTmpFile($value); $file = LSsession :: getTmpFile($value);
$real_mimetype = mime_content_type($file);
$mimetype = mime_content_type($file); $mimetype = LSconfig :: get('params.mimeType', null, null, $options);
if (!is_null($mimetype)) {
if (isset($options['params']['mimeType'])) { if (is_array($mimetype)) {
if (is_array($options['params']['mimeType'])) { if (!in_array($real_mimetype, $mimetype))
if (!in_array($mimetype,$options['params']['mimeType'])) {
return; return;
} }
}
else { else {
if ($mimetype != $options['params']['mimeType']) { if ($real_mimetype != $mimetype)
return; return;
} }
} }
}
if (isset($options['params']['mimeTypeRegEx'])) { $mimeTypeRegEx = LSconfig :: get('params.mimeTypeRegEx', null, 'string', $options);
if (!preg_match($options['params']['mimeTypeRegEx'], $mimetype)) { if (is_string($mimeTypeRegEx) && !preg_match($mimeTypeRegEx, $real_mimetype))
return false; return false;
}
}
return true; return true;
} }

View file

@ -38,11 +38,12 @@ class LSformRule_minlength extends LSformRule {
* @return boolean true si la valeur est valide, false sinon * @return boolean true si la valeur est valide, false sinon
*/ */
public static function validate ($value,$options,$formElement) { public static function validate ($value,$options,$formElement) {
if(!isset($options['params']['limit'])) { $limit = LSconfig :: get('params.limit', null, 'int', $options);
if(is_null($limit)) {
LSerror :: addErrorCode('LSformRule_01',array('type' => 'minlength', 'param' => 'limit')); LSerror :: addErrorCode('LSformRule_01',array('type' => 'minlength', 'param' => 'limit'));
return; return;
} }
return (strlen($value)>=$options['params']['limit']); return (strlen($value) >= $limit);
} }
} }

View file

@ -44,46 +44,39 @@ class LSformRule_password extends LSformRule {
* @return boolean true si la valeur est valide, false sinon * @return boolean true si la valeur est valide, false sinon
*/ */
public static function validate ($value,$options=array(),$formElement) { public static function validate ($value,$options=array(),$formElement) {
if(isset($options['params']['maxLength'])) { $maxLength = LSconfig :: get('params.maxLength', null, 'int', $options);
if (strlen($value)>$options['params']['maxLength']) if(is_int($maxLength) && strlen($value) > $maxLength)
return; return;
}
if(isset($options['params']['minLength'])) { $minLength = LSconfig :: get('params.minLength', null, 'int', $options);
if (strlen($value)<$options['params']['minLength']) if(is_int($minLength) && strlen($value) < $minLength)
return; return;
}
if(isset($options['params']['regex'])) { $regex = LSconfig :: get('params.regex', null, null, $options);
if (!is_array($options['params']['regex'])) { if(!is_null($regex)) {
$options['params']['regex']=array($options['params']['regex']); if (!is_array($regex))
} $regex = array($regex);
if (isset($options['params']['minValidRegex'])) {
$options['params']['minValidRegex']=(int)$options['params']['minValidRegex']; $minValidRegex = LSconfig :: get('params.minValidRegex', count($regex), 'int', $options);
if ($options['params']['minValidRegex']==0 || $options['params']['minValidRegex']>count($options['params']['regex'])) { if ($minValidRegex == 0 || $minValidRegex > count($regex))
$options['params']['minValidRegex']=count($options['params']['regex']); $minValidRegex = count($regex);
}
}
else {
$options['params']['minValidRegex']=count($options['params']['regex']);
}
$valid=0; $valid=0;
foreach($options['params']['regex'] as $regex) { foreach($regex as $r) {
if ($regex[0]!='/') { if ($r[0] != '/') {
LSerror :: addErrorCode('LSformRule_password_01'); LSerror :: addErrorCode('LSformRule_password_01');
continue; continue;
} }
if (preg_match($regex,$value)) if (preg_match($r, $value))
$valid++; $valid++;
} }
if ($valid<$options['params']['minValidRegex']) if ($valid < $minValidRegex)
return; return;
} }
if(isset($options['params']['prohibitedValues']) && is_array($options['params']['prohibitedValues'])) { $prohibitedValues = LSconfig :: get('params.prohibitedValues', null, null, $options);
if (in_array($value,$options['params']['prohibitedValues'])) if(is_array($prohibitedValues) && in_array($value, $prohibitedValues))
return; return;
}
return true; return true;
} }

View file

@ -39,12 +39,13 @@ class LSformRule_rangelength extends LSformRule {
* @return boolean true si la valeur est valide, false sinon * @return boolean true si la valeur est valide, false sinon
*/ */
public static function validate ($value,$options=array(),$formElement) { public static function validate ($value,$options=array(),$formElement) {
if(!isset($options['params']['limits'])) { $limits = LSconfig :: get('params.limits', null, null, $options);
if(!is_array($limits) || count($limits) != 2) {
LSerror :: addErrorCode('LSformRule_01',array('type' => 'rangelength', 'param' => 'limits')); LSerror :: addErrorCode('LSformRule_01',array('type' => 'rangelength', 'param' => 'limits'));
return; return;
} }
$len = strlen($value); $len = strlen($value);
return ($len >= $options['params']['limits'][0] && $len <= $options['params']['limits'][1]); return ($len >= $limits[0] && $len <= $limits[1]);
} }
} }

View file

@ -39,10 +39,8 @@ class LSformRule_regex extends LSformRule {
*/ */
public static function validate($value, $options, $formElement) { public static function validate($value, $options, $formElement) {
if (is_array($options)) { if (is_array($options)) {
if (isset($options['params']['regex'])) { $regex = LSconfig :: get('params.regex', null, 'string', $options);
$regex=$options['params']['regex']; if (!is_string($regex)) {
}
else {
LSerror :: addErrorCode('LSformRule_regex_01'); LSerror :: addErrorCode('LSformRule_regex_01');
return; return;
} }
@ -50,9 +48,8 @@ class LSformRule_regex extends LSformRule {
else { else {
$regex = $options; $regex = $options;
} }
if (!preg_match($regex, $value)) { if (!preg_match($regex, $value))
return false; return false;
}
return true; return true;
} }