Completed
Push — master ( 70e100...0915e8 )
by Albert
02:03
created

Length::__construct()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 8.7972
c 1
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
crap 4
1
<?php
2
3
namespace Albert221\Validation\Rule;
4
5
class Length implements RuleInterface
6
{
7
    use RuleTrait;
8
9
    protected $minLength;
10
    protected $maxLength;
11
12 8
    public function __construct($minLength, $maxLength)
13
    {
14 8
        $this->message = 'This field does not meet the permitted length.';
15
16 8
        if (! is_int($minLength)) {
17 1
            throw new \InvalidArgumentException(
18 1
                sprintf('Minimum length must be type of int, %s given.', gettype($minLength))
19 1
            );
20
        }
21
22 7
        if (! is_int($maxLength)) {
23 1
            throw new \InvalidArgumentException(
24 1
                sprintf('Maximum length must be type of int, %s given.', gettype($maxLength))
25 1
            );
26
        }
27
28 6
        if ($minLength > $maxLength) {
29 1
            throw new \InvalidArgumentException('Minimum length must not be greater than maximum length.');
30
        }
31
32 5
        $this->minLength = $minLength;
33 5
        $this->maxLength = $maxLength;
34 5
    }
35
36 5
    public function test($value)
37
    {
38 5
        return mb_strlen($value) >= $this->minLength && mb_strlen($value) <= $this->maxLength;
39
    }
40
}
41