StringSize::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Constraint;
6
7 View Code Duplication
class StringSize extends Constraint
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $minSize;
13
14
    /**
15
     * @var int
16
     */
17
    protected $maxSize;
18
19
    public function __construct(int $minSize, int $maxSize = PHP_INT_MAX, string $errorMessage = null)
20
    {
21
        $this->minSize = $minSize;
22
        $this->maxSize = $maxSize;
23
24
        $this->setErrorMessage(
25
            $errorMessage ?? sprintf('Content out of min/max limit sizes [%s, %s]', $this->minSize, $this->maxSize)
26
        );
27
    }
28
29
    public function validate($content): bool
30
    {
31
        if (!is_scalar($content)) {
32
            return false;
33
        }
34
35
        if ($content === null) {
36
            return false;
37
        }
38
39
        $size = strlen($content);
40
41
        return $size >= $this->minSize && $size <= $this->maxSize;
42
    }
43
}
44