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

Length   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 36
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 23 4
A test() 0 4 2
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