Between   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 104
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 4 2
A tooSmall() 0 8 2
A tooLarge() 0 8 2
A getMessageParameters() 0 7 1
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Particle\Validator\Rule;
12
13
/**
14
 * This rule will validate a value to be between min and max.
15
 *
16
 * @package Particle\Validator\Rule
17
 */
18
class Between extends Rule
19
{
20
    /**
21
     * A constant for an error message if the value is exceeding the max value.
22
     */
23
    const TOO_BIG = 'Between::TOO_BIG';
24
25
    /**
26
     * A constant for an error message if the value is below the min value.
27
     */
28
    const TOO_SMALL = 'Between::TOO_SMALL';
29
30
    /**
31
     * The message templates which can be returned by this validator.
32
     *
33
     * @var array
34
     */
35
    protected $messageTemplates = [
36
        self::TOO_BIG => '{{ name }} must be less than or equal to {{ max }}',
37
        self::TOO_SMALL => '{{ name }} must be greater than or equal to {{ min }}',
38
    ];
39
40
    /**
41
     * The lower boundary.
42
     *
43
     * @var int
44
     */
45
    protected $min;
46
47
    /**
48
     * The upper boundary.
49
     *
50
     * @var int
51
     */
52
    protected $max;
53
54
    /**
55
     * Construct the Between rule.
56
     *
57
     * @param int $min
58
     * @param int $max
59
     */
60 8
    public function __construct($min, $max)
61
    {
62 8
        $this->min = $min;
63 8
        $this->max = $max;
64 8
    }
65
66
    /**
67
     * Checks whether or not $value is between min and max for this rule.
68
     *
69
     * @param mixed $value
70
     * @return bool
71
     */
72 7
    public function validate($value)
73
    {
74 7
        return !$this->tooSmall($value, self::TOO_SMALL) && !$this->tooLarge($value, self::TOO_BIG);
75
    }
76
77
    /**
78
     * Returns whether or not the value is too small, and logs an error if it is.
79
     *
80
     * @param mixed $value
81
     * @param string $error
82
     * @return bool
83
     */
84 13
    protected function tooSmall($value, $error)
85
    {
86 13
        if ($value < $this->min) {
87 2
            $this->error($error);
88 2
            return true;
89
        }
90 12
        return false;
91
    }
92
93
    /**
94
     * Returns whether or not the value is too large, and logs an error if it is.
95
     *
96
     * @param mixed $value
97
     * @param string $error
98
     * @return bool
99
     */
100 11
    protected function tooLarge($value, $error)
101
    {
102 11
        if ($value > $this->max) {
103 3
            $this->error($error);
104 3
            return true;
105
        }
106 8
        return false;
107
    }
108
109
    /**
110
     * Returns the parameters that may be used in a validation message.
111
     *
112
     * @return array
113
     */
114 5
    protected function getMessageParameters()
115
    {
116 5
        return array_merge(parent::getMessageParameters(), [
117 5
            'min' => $this->min,
118 5
            'max' => $this->max
119 5
        ]);
120
    }
121
}
122