Completed
Push — master ( 675da0...765c9a )
by Changwan
02:43
created

BetweenTester::test()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 8.1867

Importance

Changes 0
Metric Value
cc 8
eloc 7
nc 7
nop 3
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 8.1867
rs 7.7777
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Validator\Testers;
3
4
use Wandu\Validator\Contracts\Tester;
5
6
class BetweenTester implements Tester
7
{
8
    /** @var int|string */
9
    protected $min;
10
    
11
    /** @var int|string */
12
    protected $max;
13
14
    /**
15
     * @param int|string $min
16
     * @param int|string $max
17
     */
18 1
    public function __construct($min, $max)
19
    {
20 1
        $this->min = $min;
21 1
        $this->max = $max;
22 1
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function test($data, $origin = null, array $keys = []): bool
28
    {
29 1
        if ($data === null) return false;
30 1
        if (is_int($data) || is_float($data)) {
31 1
            return $data <= (int) $this->max && $data >= (int) $this->min;
32
        }
33 1
        if (is_string($data)) {
34 1
            return strcmp($data, $this->min) >= 0 && strcmp($data, $this->max) <= 0;
35
        }
36
        return $data <= $this->max && $data >= $this->min;
37
    }
38
}
39