ArrayValidator::setSize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php declare(strict_types=1);
2
3
namespace indigerd\scenarios\validation\validator;
4
5
class ArrayValidator extends AbstractValidator
6
{
7
    protected $message = 'Value must be an array';
8
9
    protected $size = null;
10
11
    public function setSize($size)
12
    {
13
        $size = (int)$size;
14
        if ($size <= 0) {
15
            throw new \InvalidArgumentException('Invalid size param value');
16
        }
17
        $this->size = $size;
18
    }
19
20
    public function validate($value, array $context = []): bool
21
    {
22
        if ($this->skipOnEmpty and ($value === [] or $value === null)) {
23
            return true;
24
        }
25
        if (!\is_array($value)) {
26
            return false;
27
        }
28
        if ($this->size !== null and $this->size !== \sizeof($value)) {
29
            $this->message = 'Expected array size: ' . $this->size . '. Actual: ' . \sizeof($value);
30
        }
31
        return true;
32
    }
33
}
34