*/ class LSformRule_compare extends LSformRule { // CLI parameters autocompleters protected static $cli_params_autocompleters = array( 'operator' => null, ); /** * Retourne l'operateur de comparaison. * * @access private * @param string Nom de l'operateur * * @return string Operateur à utiliser */ private static function _findOperator($operator_name) { $_operators = array( 'eq' => '==', 'neq' => '!=', 'gt' => '>', 'gte' => '>=', 'lt' => '<', 'lte' => '<=' ); if (empty($operator_name)) { return '=='; } elseif (isset($_operators[$operator_name])) { return $_operators[$operator_name]; } elseif (in_array($operator_name, $_operators)) { return $operator_name; } else { return '=='; } } /** * Vérification des valeurs. * * @param string $values Valeurs à vérifier * @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($value, $options=array(), &$formElement) { $operator = LSconfig :: get('params.operator', null, 'string', $options); if (!$operator) { LSerror :: addErrorCode('LSformRule_01',array('type' => 'compare', 'param' => 'operator')); return; } $operator = self :: _findOperator($operator); if ('==' != $operator && '!=' != $operator) { $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);'); } else { $compareFn = create_function('$a, $b', 'return $a ' . $operator . ' $b;'); } return $compareFn($values[0], $values[1]); } }