Passed
Push — main ( 926316...d9c688 )
by Dimitri
02:57
created

AnyOf   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 48
ccs 12
cts 12
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 12 4
A strict() 0 5 1
A fillParameters() 0 3 1
A separator() 0 5 1
A values() 0 5 1
1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Rules;
13
14
use Rakit\Validation\Rule;
15
16
class AnyOf extends AbstractRule
17
{
18
    protected bool $strict = false;
19
20
    /**
21
     * {@inheritDoc}
22
     */
23
    public function fillParameters(array $params): Rule
24
    {
25 2
        return $this->fillAllowedValuesParameters($params);
26
    }
27
28
    public function values(array $values): static
29
    {
30 2
        $this->params['allowed_values'] = $values;
31
32 2
        return $this;
33
    }
34
35
    public function separator(string $char): static
36
    {
37 2
        $this->params['separator'] = $char;
38
39 2
        return $this;
40
    }
41
42
    public function strict(bool $strict = true): static
43
    {
44 2
        $this->strict = $strict;
45
46 2
        return $this;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function check($value): bool
53
    {
54 2
        $this->requireParameters(['allowed_values']);
55
56 2
        $valid  = true;
57 2
        $values = is_string($value) ? explode($this->parameter('separator', ','), $value) : (array) $value;
58
59
        foreach ($values as $v) {
60 2
            $valid = $valid && in_array($v, $this->parameter('allowed_values'), $this->strict);
61
        }
62
63 2
        return $valid;
64
    }
65
}
66