Passed
Pull Request — master (#222)
by Rustam
02:39
created

CountHandler::validate()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 9.4614

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 47
ccs 23
cts 28
cp 0.8214
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 3
crap 9.4614
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Countable;
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Formatter;
10
use Yiisoft\Validator\FormatterInterface;
11
use Yiisoft\Validator\Result;
12
use Yiisoft\Validator\ValidationContext;
13
14
use function count;
15
16
/**
17
 * Validates that the value contains certain number of items. Can be applied to arrays or classes implementing
18
 * {@see Countable} interface.
19
 */
20
final class CountHandler implements RuleHandlerInterface
21
{
22
    private FormatterInterface $formatter;
23
24 20
    public function __construct(?FormatterInterface $formatter = null)
25
    {
26 20
        $this->formatter = $formatter ?? new Formatter();
27
    }
28
29 20
    public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
30
    {
31 20
        if (!$rule instanceof Count) {
32 1
            throw new UnexpectedRuleException(Count::class, $rule);
33
        }
34
35 19
        $result = new Result();
36
37 19
        if (!is_countable($value)) {
38
            $formattedMessage = $this->formatter->format(
39
                $rule->getMessage(),
40
                ['attribute' => $context?->getAttribute(), 'value' => $value]
41
            );
42
            $result->addError($formattedMessage);
43
44
            return $result;
45
        }
46
47 19
        $count = count($value);
48
49 19
        if ($rule->getExactly() !== null && $count !== $rule->getExactly()) {
50 1
            $formattedMessage = $this->formatter->format(
51 1
                $rule->getNotExactlyMessage(),
52 1
                ['exactly' => $rule->getExactly(), 'attribute' => $context?->getAttribute(), 'value' => $value]
53
            );
54 1
            $result->addError($formattedMessage);
55
56 1
            return $result;
57
        }
58
59 18
        if ($rule->getMin() !== null && $count < $rule->getMin()) {
60 9
            $formattedMessage = $this->formatter->format(
61 9
                $rule->getTooFewItemsMessage(),
62 9
                ['min' => $rule->getMin(), 'attribute' => $context?->getAttribute(), 'value' => $value]
63
            );
64 9
            $result->addError($formattedMessage);
65
        }
66
67 18
        if ($rule->getMax() !== null && $count > $rule->getMax()) {
68 2
            $formattedMessage = $this->formatter->format(
69 2
                $rule->getTooManyItemsMessage(),
70 2
                ['max' => $rule->getMax(), 'attribute' => $context?->getAttribute(), 'value' => $value]
71
            );
72 2
            $result->addError($formattedMessage);
73
        }
74
75 18
        return $result;
76
    }
77
}
78