Completed
Push — 1.10.x ( b87948...d30235 )
by
unknown
102:50 queued 50:07
created

HTML_QuickForm_Rule_Range   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 43
rs 10
wmc 9
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 17 6
A getValidationScript() 0 14 3
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
 * Checks that the length of value is within range
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 [email protected] so we can mail you a copy immediately.
14
 *
15
 * @category    HTML
16
 * @package     HTML_QuickForm
17
 * @author      Bertrand Mansion <[email protected]>
18
 * @copyright   2001-2009 The PHP Group
19
 * @license     http://www.php.net/license/3_01.txt PHP License 3.01
20
 * @version     CVS: $Id: Range.php,v 1.8 2009/04/04 21:34:04 avb Exp $
21
 * @link        http://pear.php.net/package/HTML_QuickForm
22
 */
23
24
/**
25
 * Checks that the length of value is within range
26
 *
27
 * @category    HTML
28
 * @package     HTML_QuickForm
29
 * @author      Bertrand Mansion <[email protected]>
30
 * @version     Release: 3.2.11
31
 * @since       3.2
32
 */
33
class HTML_QuickForm_Rule_Range extends HTML_QuickForm_Rule
34
{
35
    /**
36
     * Validates a value using a range comparison
37
     *
38
     * @param     string    $value      Value to be checked
39
     * @param     mixed     $options    Int for length, array for range
40
     * @access    public
41
     * @return    boolean   true if value is valid
42
     */
43
    public function validate($value, $options)
44
    {
45
        $length = api_strlen($value);
46
47
        switch ($this->name) {
48
            case 'min_numeric_length':
49
                return (float) $value >= $options;
50
            case 'max_numeric_length':
51
                return (float) $value <= $options;
52
            case 'minlength':
53
                return $length >= $options;
54
            case 'maxlength':
55
                return $length <= $options;
56
            default:
57
                return $length >= $options[0] && $length <= $options[1];
58
        }
59
    }
60
61
    public function getValidationScript($options = null)
62
    {
63
        switch ($this->name) {
64
            case 'minlength':
65
                $test = '{jsVar}.length < '.$options;
66
                break;
67
            case 'maxlength':
68
                $test = '{jsVar}.length > '.$options;
69
                break;
70
            default:
71
                $test = '({jsVar}.length < '.$options[0].' || {jsVar}.length > '.$options[1].')';
72
        }
73
        return array('', "{jsVar} != '' && {$test}");
74
    }
75
}