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

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_LSformElement_select_validValue extends LSformRule {
/**
* Validate value
*
@ -35,7 +35,7 @@ class LSformRule_LSformElement_select_validValue extends LSformRule {
* @param object $formElement The related formElement object
*
* @return boolean true if the value is valide, false if not
*/
*/
public static function validate($value,$option,$formElement) {
$ret = $formElement -> isValidValue($value);
if ($ret===False) return False;

View file

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_alphanumeric extends LSformRule {
/**
* Vérification de la valeur.
*
@ -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 {
@ -50,6 +49,6 @@ class LSformRule_alphanumeric extends LSformRule {
return LSformRule_regex :: validate($value,$regex,$formElement);
}
}

View file

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_callable extends LSformRule {
/**
* Check the value using the callable object
*
@ -40,15 +40,21 @@ class LSformRule_callable extends LSformRule {
* @param object $formElement The LSformElement object
*
* @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 {
LSerror :: addErrorCode('LSformRule_callable_01');
return False;
}
$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

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_compare extends LSformRule {
/**
* Retourne l'operateur de comparaison.
*
@ -61,18 +61,19 @@ class LSformRule_compare extends LSformRule {
* Vérification des valeurs.
*
* @param string $values Valeurs à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Operateur : $options['params']['operator']
* @param object $formElement L'objet formElement attaché
*
* @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);');
}
@ -81,6 +82,6 @@ class LSformRule_compare extends LSformRule {
}
return $compareFn($values[0], $values[1]);
}
}

View file

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_date extends LSformRule {
/**
* Validation de données
*
@ -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

@ -31,17 +31,18 @@ class LSformRule_differentPassword extends LSformRule {
* Check the value
*
* @param string $values Value to check
* @param array $options Validation options :
* - Other attribute : $options['params']['otherAttributes']
* @param array $options Validation options :
* - 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

@ -26,19 +26,23 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_email extends LSformRule {
/**
* Vérification de la valeur.
*
* @param string $value Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Check domain : $options['params']['checkDomain']
* @param object $formElement L'objet formElement attaché
*
* @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

@ -31,7 +31,7 @@ class LSformRule_filesize extends LSformRule {
* Vérification de la valeur.
*
* @param string $values Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Taille max (en octet) : $options['params']['maxSize']
* - Taille min (en octet) : $options['params']['minSize']
* @param object $formElement L'objet formElement attaché
@ -40,23 +40,19 @@ class LSformRule_filesize extends LSformRule {
*/
public static function validate ($value,$options,$formElement) {
$file = LSsession :: getTmpFile($value);
$size = filesize($file);
if (is_int($options['params']['maxSize'])) {
if ($size > $options['params']['maxSize']) {
return;
}
}
if (is_int($options['params']['minSize'])) {
if ($size < $options['params']['minSize']) {
return;
}
}
$size = filesize($file);
$maxSize = LSconfig :: get('params.maxSize', null, 'int', $options);
if (is_int($maxSize) && $size > $maxSize)
return;
$minSize = LSconfig :: get('params.minSize', null, 'int', $options);
if (is_int($minSize) && $size < $minSize)
return;
return true;
}
}

View file

@ -33,7 +33,7 @@ class LSformRule_imagefile extends LSformRule {
* Vérification de la valeur.
*
* @param string $values Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Type MIME : $options['params']['mimeType']
* - Type MIME (regex) : $options['params']['mimeTypeRegEx']
* @param object $formElement L'objet formElement attaché
@ -42,19 +42,21 @@ class LSformRule_imagefile extends LSformRule {
*/
public static function validate ($value,$options,$formElement) {
$file = LSsession :: getTmpFile($value);
$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\/.*/'
)
);
}
return LSformRule_mimetype :: validate($value,$options,$formElement);
}
}

View file

@ -31,7 +31,7 @@ class LSformRule_imagesize extends LSformRule {
* Vérification de la valeur.
*
* @param string $values Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Largeur max : $options['params']['maxWidth']
* - Largeur min : $options['params']['minWidth']
* - Hauteur max : $options['params']['maxHeight']
@ -43,30 +43,25 @@ class LSformRule_imagesize extends LSformRule {
public static function validate ($value,$options,$formElement) {
$file = LSsession :: getTmpFile($value);
list($width, $height, $type, $attr) = getimagesize($file);
if (is_int($options['params']['maxWidth'])) {
if ($width > $options['params']['maxWidth']) {
return;
}
}
if (is_int($options['params']['minWidth'])) {
if ($width < $options['params']['minWidth']) {
return;
}
}
if (is_int($options['params']['maxHeight'])) {
if ($height > $options['params']['maxHeight']) {
return;
}
}
if (is_int($options['params']['minHeight'])) {
if ($height < $options['params']['minHeight']) {
return;
}
}
$maxWidth = LSconfig :: get('params.maxWidth', null, 'int', $options);
if (is_int($maxWidth) && $width > $maxWidth)
return;
$minWidth = LSconfig :: get('params.minWidth', null, 'int', $options);
if (is_int($minWidth) && $width < $minWidth)
return;
$maxHeight = LSconfig :: get('params.maxHeight', null, 'int', $options);
if (is_int($maxHeight) && $height > $maxHeight)
return;
$minHeight = LSconfig :: get('params.minHeight', null, 'int', $options);
if (is_int($minHeight) && $height < $minHeight)
return;
return true;
}
}

View file

@ -26,23 +26,24 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_inarray extends LSformRule {
/**
* Vérification de la valeur.
*
* @param string $values Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Regex : $option['params']['possible_values'] ou $option
* @param object $formElement L'objet formElement attaché
*
* @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

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_integer extends LSformRule{
/**
* Verification value.
*
@ -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 {
@ -59,6 +61,6 @@ class LSformRule_integer extends LSformRule{
LSsession :: loadLSclass('LSformRule_regex');
return LSformRule_regex :: validate($value,$regex,$formElement);
}
}

View file

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_lettersonly extends LSformRule {
/**
* Vérification de la valeur.
*
@ -41,6 +41,6 @@ class LSformRule_lettersonly extends LSformRule {
LSsession :: loadLSclass('LSformRule_regex');
return LSformRule_regex :: validate($value,$regex,$formElement);
}
}

View file

@ -31,19 +31,20 @@ class LSformRule_maxlength extends LSformRule {
* Vérification de la valeur.
*
* @param string $values Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Longueur max : $options['params']['limit']
* @param object $formElement L'objet formElement attaché
*
* @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

@ -31,7 +31,7 @@ class LSformRule_mimetype extends LSformRule {
* Vérification de la valeur.
*
* @param string $values Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Type MIME : $options['params']['mimeType']
* - Type MIME (regex) : $options['params']['mimeTypeRegEx']
* @param object $formElement L'objet formElement attaché
@ -40,30 +40,26 @@ class LSformRule_mimetype extends LSformRule {
*/
public static function validate ($value,$options,$formElement) {
$file = LSsession :: getTmpFile($value);
$mimetype = mime_content_type($file);
if (isset($options['params']['mimeType'])) {
if (is_array($options['params']['mimeType'])) {
if (!in_array($mimetype,$options['params']['mimeType'])) {
$real_mimetype = mime_content_type($file);
$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)) {
return false;
}
}
$mimeTypeRegEx = LSconfig :: get('params.mimeTypeRegEx', null, 'string', $options);
if (is_string($mimeTypeRegEx) && !preg_match($mimeTypeRegEx, $real_mimetype))
return false;
return true;
}
}

View file

@ -31,19 +31,20 @@ class LSformRule_minlength extends LSformRule {
* Vérification de la valeur.
*
* @param string $values Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Longueur min : $options['params']['limit']
* @param object $formElement L'objet formElement attaché
*
* @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

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_nonzero extends LSformRule {
/**
* Vérification de la valeur.
*
@ -35,12 +35,12 @@ class LSformRule_nonzero extends LSformRule {
* @param object $formElement L'objet formElement attaché
*
* @return boolean true si la valeur est valide, false sinon
*/
*/
public static function validate ($value,$options,$formElement) {
$regex = '/^-?[1-9][0-9]*/';
LSsession :: loadLSclass('LSformRule_regex');
return LSformRule_regex :: validate($value,$regex,$formElement);
}
}

View file

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_nopunctuation extends LSformRule {
/**
* Vérification de la valeur.
*
@ -41,6 +41,6 @@ class LSformRule_nopunctuation extends LSformRule {
LSsession :: loadLSclass('LSformRule_regex');
return LSformRule_regex :: validate($value,$regex,$formElement);
}
}

View file

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_numeric extends LSformRule{
/**
* Vérification de la valeur.
*
@ -41,6 +41,6 @@ class LSformRule_numeric extends LSformRule{
LSsession :: loadLSclass('LSformRule_regex');
return LSformRule_regex :: validate($value,$regex,$formElement);
}
}

View file

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_password extends LSformRule {
/**
* Vérification de la valeur.
*
@ -42,52 +42,45 @@ class LSformRule_password extends LSformRule {
* @param object $formElement L'objet formElement attaché
*
* @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'])
return;
}
if(isset($options['params']['minLength'])) {
if (strlen($value)<$options['params']['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']);
}
$maxLength = LSconfig :: get('params.maxLength', null, 'int', $options);
if(is_int($maxLength) && strlen($value) > $maxLength)
return;
$minLength = LSconfig :: get('params.minLength', null, 'int', $options);
if(is_int($minLength) && strlen($value) < $minLength)
return;
$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']))
return;
}
$prohibitedValues = LSconfig :: get('params.prohibitedValues', null, null, $options);
if(is_array($prohibitedValues) && in_array($value, $prohibitedValues))
return;
return true;
}
}

View file

@ -26,26 +26,27 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_rangelength extends LSformRule {
/**
* Vérification de la valeur.
*
* @param string $values Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Longueur min : $options['params']['limits'][0]
* - Longueur max : $options['params']['limits'][1]
* @param object $formElement L'objet formElement attaché
*
* @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

@ -26,34 +26,31 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_regex extends LSformRule {
/**
* Vérification de la valeur.
*
* @param string $values Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* - Regex : $options['params']['regex'] ou $options
* @param object $formElement L'objet formElement attaché
*
* @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;
return true;
}
}

View file

@ -35,10 +35,10 @@ class LSformRule_required extends LSformRule {
* @param object $formElement L'objet formElement attaché
*
* @return boolean true si la valeur est valide, false sinon
*/
*/
public static function validate ($value,$options=NULL,$formElement) {
return ((string)$value != '');
}
}

View file

@ -26,7 +26,7 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_ssh_pub_key extends LSformRule {
/**
* Validate SSH public key value
*
@ -35,7 +35,7 @@ class LSformRule_ssh_pub_key extends LSformRule {
* @param object $formElement The related formElement object
*
* @return boolean true if the value is valide, false if not
*/
*/
public static function validate($value,$options,$formElement) {
if (preg_match('/^(ssh-[a-z0-9]+) +([^ ]+) +(.*)$/', $value, $m)) {
$data=@base64_decode($m[2]);

View file

@ -26,12 +26,12 @@
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSformRule_telephonenumber extends LSformRule {
/**
* Vérification de la valeur.
*
* @param string $value Valeur à vérifier
* @param array $options Options de validation :
* @param array $options Options de validation :
* @param object $formElement L'objet formElement attaché
*
* @return boolean true si la valeur est valide, false sinon