Passed
Push — master ( 3bffb6...f32028 )
by Aleksandr
31:58
created

LengthConstraint   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 23
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 4
A __construct() 0 4 1
1
<?php
2
3
namespace Drobotik\Eav\Validation\Constraints;
4
5
use Drobotik\Eav\Interface\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
    public function validate($value): ?string
19
    {
20
        if (!is_string($value)) {
21
            return "This value must be a string.";
22
        }
23
24
        $length = strlen($value);
25
        if ($length < $this->min || $length > $this->max) {
26
            return "This value must be between {$this->min} and {$this->max} characters long.";
27
        }
28
29
        return null;
30
    }
31
}