]> git.openstreetmap.org Git - chef.git/blob - cookbooks/dmca/files/default/html/HTML/QuickForm/Rule/Compare.php
Merge remote-tracking branch 'github/pull/422'
[chef.git] / cookbooks / dmca / files / default / html / HTML / QuickForm / Rule / Compare.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5  * Rule to compare two form fields
6  * 
7  * PHP versions 4 and 5
8  *
9  * LICENSE: This source file is subject to version 3.01 of the PHP license
10  * that is available through the world-wide-web at the following URI:
11  * http://www.php.net/license/3_01.txt If you did not receive a copy of
12  * the PHP License and are unable to obtain it through the web, please
13  * send a note to license@php.net so we can mail you a copy immediately.
14  *
15  * @category    HTML
16  * @package     HTML_QuickForm
17  * @author      Alexey Borzov <avb@php.net>
18  * @copyright   2001-2011 The PHP Group
19  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
20  * @version     CVS: $Id$
21  * @link        http://pear.php.net/package/HTML_QuickForm
22  */
23
24 /**
25  * Abstract base class for QuickForm validation rules 
26  */
27 require_once 'HTML/QuickForm/Rule.php';
28
29 /**
30  * Rule to compare two form fields
31  * 
32  * The most common usage for this is to ensure that the password 
33  * confirmation field matches the password field
34  * 
35  * @category    HTML
36  * @package     HTML_QuickForm
37  * @author      Alexey Borzov <avb@php.net>
38  * @version     Release: 3.2.16
39  * @since       3.2
40  */
41 class HTML_QuickForm_Rule_Compare extends HTML_QuickForm_Rule
42 {
43    /**
44     * Possible operators to use
45     * @var array
46     * @access private
47     */
48     var $_operators = array(
49         'eq'  => '===',
50         'neq' => '!==',
51         'gt'  => '>',
52         'gte' => '>=',
53         'lt'  => '<',
54         'lte' => '<=',
55         '=='  => '===',
56         '!='  => '!=='
57     );
58
59
60    /**
61     * Returns the operator to use for comparing the values
62     * 
63     * @access private
64     * @param  string     operator name
65     * @return string     operator to use for validation
66     */
67     function _findOperator($name)
68     {
69         if (empty($name)) {
70             return '===';
71         } elseif (isset($this->_operators[$name])) {
72             return $this->_operators[$name];
73         } elseif (in_array($name, $this->_operators)) {
74             return $name;
75         } else {
76             return '===';
77         }
78     }
79
80
81     function validate($values, $operator = null)
82     {
83         $operator = $this->_findOperator($operator);
84         if ('===' != $operator && '!==' != $operator) {
85             $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
86         } else {
87             $compareFn = create_function('$a, $b', 'return strval($a) ' . $operator . ' strval($b);');
88         }
89         
90         return $compareFn($values[0], $values[1]);
91     }
92
93
94     function getValidationScript($operator = null)
95     {
96         $operator = $this->_findOperator($operator);
97         if ('===' != $operator && '!==' != $operator) {
98             $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
99         } else {
100             $check = "!(String({jsVar}[0]) {$operator} String({jsVar}[1]))";
101         }
102         return array('', "'' != {jsVar}[0] && {$check}");
103     }
104 }
105 ?>