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) {
if (isset($options['params']['withAccents']) && $options['params']['withAccents'] == true){
if (LSconfig :: get('params.withAccents', false, 'bool', $options)) {
$regex = '/(*UTF8)^[0-9\p{L}]+$/';
}
else {

View file

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

View file

@ -68,11 +68,12 @@ class LSformRule_compare extends LSformRule {
* @return boolean true si la valeur est valide, false sinon
*/
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'));
return;
}
$operator = self :: _findOperator($options['params']['operator']);
$operator = self :: _findOperator($operator);
if ('==' != $operator && '!=' != $operator) {
$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.
*/
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');
return;
}
$date = strptime($value,$options['params']['format']);
$date = strptime($value, $format);
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);
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 array $options Validation options :
* - Other attribute : $options['params']['otherAttributes']
* - Other attribute : $options['params']['otherPasswordAttributes']
* @param object $formElement The linked LSformElement object
*
* @return boolean true si la valeur est valide, false sinon
*/
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
if (!is_array($options['params']['otherPasswordAttributes']))
$options['params']['otherPasswordAttributes'] = array($options['params']['otherPasswordAttributes']);
if (!is_array($otherPasswordAttributes))
$otherPasswordAttributes = array($otherPasswordAttributes);
// Load 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
foreach($options['params']['otherPasswordAttributes'] as $attr) {
foreach($otherPasswordAttributes as $attr) {
// Check attribute exist
if (!isset($formElement -> attr_html -> attribute -> ldapObject -> attrs[$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
*/
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);
if (is_int($options['params']['maxSize'])) {
if ($size > $options['params']['maxSize']) {
$maxSize = LSconfig :: get('params.maxSize', null, 'int', $options);
if (is_int($maxSize) && $size > $maxSize)
return;
}
}
if (is_int($options['params']['minSize'])) {
if ($size < $options['params']['minSize']) {
$minSize = LSconfig :: get('params.minSize', null, 'int', $options);
if (is_int($minSize) && $size < $minSize)
return;
}
}
return true;
}

View file

@ -45,7 +45,9 @@ class LSformRule_imagefile extends LSformRule {
$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(
'params' => array(
'mimeTypeRegEx' => '/image\/.*/'

View file

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

View file

@ -38,11 +38,12 @@ class LSformRule_inarray extends LSformRule {
* @return boolean true si la valeur est valide, false sinon
*/
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');
return;
}
if (!in_array($value,$option['params']['possible_values']))
if (!in_array($value, $possible_values))
return false;
return true;
}

View file

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

View file

@ -38,11 +38,12 @@ class LSformRule_maxlength extends LSformRule {
* @return boolean true si la valeur est valide, false sinon
*/
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'));
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) {
$file = LSsession :: getTmpFile($value);
$real_mimetype = mime_content_type($file);
$mimetype = mime_content_type($file);
if (isset($options['params']['mimeType'])) {
if (is_array($options['params']['mimeType'])) {
if (!in_array($mimetype,$options['params']['mimeType'])) {
$mimetype = LSconfig :: get('params.mimeType', null, null, $options);
if (!is_null($mimetype)) {
if (is_array($mimetype)) {
if (!in_array($real_mimetype, $mimetype))
return;
}
}
else {
if ($mimetype != $options['params']['mimeType']) {
if ($real_mimetype != $mimetype)
return;
}
}
}
if (isset($options['params']['mimeTypeRegEx'])) {
if (!preg_match($options['params']['mimeTypeRegEx'], $mimetype)) {
$mimeTypeRegEx = LSconfig :: get('params.mimeTypeRegEx', null, 'string', $options);
if (is_string($mimeTypeRegEx) && !preg_match($mimeTypeRegEx, $real_mimetype))
return false;
}
}
return true;
}

View file

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

View file

@ -39,12 +39,13 @@ class LSformRule_rangelength extends LSformRule {
* @return boolean true si la valeur est valide, false sinon
*/
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'));
return;
}
$len=strlen($value);
return ($len >= $options['params']['limits'][0] && $len <= $options['params']['limits'][1]);
$len = strlen($value);
return ($len >= $limits[0] && $len <= $limits[1]);
}
}

View file

@ -37,22 +37,19 @@ class LSformRule_regex extends LSformRule {
*
* @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)) {
if (isset($options['params']['regex'])) {
$regex=$options['params']['regex'];
}
else {
$regex = LSconfig :: get('params.regex', null, 'string', $options);
if (!is_string($regex)) {
LSerror :: addErrorCode('LSformRule_regex_01');
return;
}
}
else {
$regex=$options;
$regex = $options;
}
if (!preg_match($regex, $value)) {
if (!preg_match($regex, $value))
return false;
}
return true;
}