Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
15 / 18
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
IsLimitable
83.33% covered (warning)
83.33%
15 / 18
80.00% covered (warning)
80.00%
4 / 5
10.46
0.00% covered (danger)
0.00%
0 / 1
 limit
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
4.59
 getLimitMin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLimitMax
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowsMultipleItems
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 limitValidators
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Field\Capability;
6
7use Cosray\Exception\RuntimeException;
8
9trait IsLimitable
10{
11    protected int $limitMin = 0;
12    protected int $limitMax = -1;
13
14    public function limit(int $max, int $min = 0): static
15    {
16        if ($max < 1) {
17            throw new RuntimeException('Limit max must be >= 1');
18        }
19
20        if ($min < 0) {
21            throw new RuntimeException('Limit min must be >= 0');
22        }
23
24        if ($min > $max) {
25            throw new RuntimeException('Limit min must be <= max');
26        }
27
28        $this->limitMax = $max;
29        $this->limitMin = $min;
30
31        return $this;
32    }
33
34    public function getLimitMin(): int
35    {
36        return $this->limitMin;
37    }
38
39    public function getLimitMax(): int
40    {
41        return $this->limitMax;
42    }
43
44    protected function allowsMultipleItems(): bool
45    {
46        return $this->getLimitMax() !== 1;
47    }
48
49    /** @return string[] */
50    private function limitValidators(): array
51    {
52        $validators = [];
53
54        if ($this->limitMax >= 1) {
55            $validators[] = 'maxitems:' . $this->getLimitMax();
56        }
57
58        if ($this->getLimitMin() > 0) {
59            $validators[] = 'minitems:' . $this->getLimitMin();
60        }
61
62        return $validators;
63    }
64}