Completed
Push — master ( 38deb0...7544ac )
by Anton
11s
created

BetweenRule::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Validator\Rule;
12
13
use Bluz\Validator\Exception\ComponentException;
14
15
/**
16
 * Check for value in range between minimum and maximum values
17
 *
18
 * @package Bluz\Validator\Rule
19
 */
20
class BetweenRule extends AbstractCompareRule
21
{
22
    /**
23
     * @var mixed minimum value
24
     */
25
    protected $minValue;
26
27
    /**
28
     * @var mixed maximum value
29
     */
30
    protected $maxValue;
31
32
    /**
33
     * Setup validation rule
34
     *
35
     * @param  mixed $min
36
     * @param  mixed $max
37
     *
38
     * @throws \Bluz\Validator\Exception\ComponentException
39
     */
40 27
    public function __construct($min, $max)
41
    {
42 27
        $this->minValue = $min;
43 27
        $this->maxValue = $max;
44
45 27
        if (is_null($min) || is_null($max)) {
46 2
            throw new ComponentException('Minimum and maximum is required');
47
        }
48
49 25
        if ($min > $max) {
50 1
            throw new ComponentException(sprintf('%s cannot be less than %s for validation', $min, $max));
51
        }
52 24
    }
53
54
    /**
55
     * Check input data
56
     *
57
     * @param  NumericRule $input
58
     *
59
     * @return bool
60
     */
61 24
    public function validate($input): bool
62
    {
63 24
        return $this->less($this->minValue, $input)
64 24
            && $this->less($input, $this->maxValue);
65
    }
66
67
    /**
68
     * Get error template
69
     *
70
     * @return string
71
     */
72 16
    public function getDescription() : string
73
    {
74 16
        return __('must be between %1 and %2', $this->minValue, $this->maxValue);
75
    }
76
}
77