Passed
Push — master ( 36a33a...a2faa0 )
by Aleksandr
01:53
created

LengthConstraint::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
namespace Drobotik\Eav\Validation\Constraints;
4
5
use Drobotik\Eav\Interfaces\ConstraintInterface;
6
7
class LengthConstraint implements ConstraintInterface
8
{
9
    private int $min;
10
    private int $max;
11
12
    public function __construct(int $min = 0, int $max = PHP_INT_MAX)
13
    {
14
        $this->min = $min;
15
        $this->max = $max;
16
    }
17
18 1
    public function validate($value): ?string
19
    {
20 1
        if (!is_string($value)) {
21 1
            return "This value must be a string.";
22
        }
23
24 1
        $length = strlen($value);
25 1
        if ($length < $this->min || $length > $this->max) {
26 1
            return "This value must be between {$this->min} and {$this->max} characters long.";
27
        }
28
29 1
        return null;
30
    }
31
}