2 /*******************************************************************************
3 * Copyright (C) 2007 Easter-eggs
4 * http://ldapsaisie.labs.libre-entreprise.org
6 * Author: See AUTHORS file in top-level directory.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ******************************************************************************/
24 * Règle de validation par comparaison de valeurs.
26 * @author Benjamin Renard <brenard@easter-eggs.com>
28 class LSformRule_compare extends LSformRule {
31 * Retourne l'operateur de comparaison.
34 * @param string Nom de l'operateur
36 * @return string Operateur à utiliser
38 function _findOperator($operator_name) {
49 if (empty($operator_name)) {
51 } elseif (isset($this->_operators[$operator_name])) {
52 return $this->_operators[$operator_name];
53 } elseif (in_array($operator_name, $this->_operators)) {
54 return $operator_name;
61 * Vérification des valeurs.
63 * @param string $values Valeurs à vérifier
64 * @param array $options Options de validation :
65 * - Operateur : $options['params']['operator']
66 * @param object $formElement L'objet formElement attaché
68 * @return boolean true si la valeur est valide, false sinon
70 function validate ($values,$options=array(),$formElement) {
71 if (!isset($options['params']['operator'])) {
72 LSerror :: addErrorCode('LSformRule_01',array('type' => 'compare', 'param' => 'operator');
75 $operator = LSformRule_compare :: _findOperator($options['params']['operator']);
76 if ('==' != $operator && '!=' != $operator) {
77 $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
80 $compareFn = create_function('$a, $b', 'return $a ' . $operator . ' $b;');
82 return $compareFn($values[0], $values[1]);