Completed
Push — master ( b6d955...929034 )
by Marcus
02:03
created

StrlenBetween::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Mbright\Validation\Rule\Validate;
4
5
use Mbright\Validation\Rule\AbstractStringCase;
6
7
class StrlenBetween extends AbstractStringCase implements ValidateRuleInterface
8
{
9
    /** @var int */
10
    protected $min;
11
    
12
    /** @var int */
13
    protected $max;
14
15
    /**
16
     * @param int $min The minimum valid length.
17
     * @param int $max The maximum valid length.
18
     */
19 33
    public function __construct(int $min, int $max)
20
    {
21 33
        $this->min = $min;
22 33
        $this->max = $max;
23 33
    }
24
25
    /**
26
     * Validates that the length of the value is within a given range.
27
     *
28
     * @param object $subject The subject to be filtered.
29
     * @param string $field The subject field name.
30
     *
31
     * @return bool True if valid, false if not.
32
     */
33 33
    public function __invoke($subject, string $field): bool
34
    {
35 33
        $value = $subject->$field;
36 33
        if (!is_scalar($value)) {
37 3
            return false;
38
        }
39 30
        $len = $this->strlen($value);
40
41 30
        return ($len >= $this->min && $len <= $this->max);
42
    }
43
}
44