Between::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mbright\Validation\Rule\Validate;
4
5
class Between implements ValidateRuleInterface
6
{
7
    /** @var int */
8
    protected $min;
9
    
10
    /** @var int */
11
    protected $max;
12
13
    /**
14
     * @param int $min minimum 'floor' value
15
     * @param int $max maximum 'ceiling' value
16
     */
17 27
    public function __construct(int $min, int $max)
18
    {
19 27
        $this->min = $min;
20 27
        $this->max = $max;
21 27
    }
22
23
    /**
24
     * Validates that a field's value is between the given min and max.
25
     *
26
     * @param $subject
27
     * @param string $field
28
     *
29
     * @return bool
30
     */
31 27
    public function __invoke($subject, string $field): bool
32
    {
33 27
        $value = $subject->$field;
34 27
        if (!is_scalar($value)) {
35 3
            return false;
36
        }
37
38 24
        return ($value >= $this->min && $value <= $this->max);
39
    }
40
}
41