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

ProhibitedIf::values()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
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 Dimtrovich\Validation\Traits\CanConvertValuesToBooleans;
15
use Rakit\Validation\Rule as RakitRule;
16
17
class ProhibitedIf extends AbstractRule
18
{
19
    use CanConvertValuesToBooleans;
20
21
    protected $implicit = true;
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function fillParameters(array $params): RakitRule
27
    {
28 2
        $this->params['field']  = array_shift($params);
29 2
        $this->params['values'] = $this->convertStringsToBoolean($params);
30
31 2
        return $this;
32
    }
33
34
    public function field(string $field): static
35
    {
36 2
        $this->params['field'] = $field;
37
38 2
        return $this;
39
    }
40
41
    public function values(array $values): static
42
    {
43 2
        $this->params['values'] = $values;
44
45 2
        return $this;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function check($value): bool
52
    {
53 2
        $this->requireParameters(['field', 'values']);
54
55 2
        $anotherAttribute = $this->parameter('field');
56 2
        $definedValues    = $this->parameter('values');
57 2
        $anotherValue     = $this->getAttribute()->getValue($anotherAttribute);
58
59 2
        $this->setParameterTextValues($this->convertBooleansToString($definedValues), 'other');
60 2
        $this->setParameterTextValues($anotherValue, 'value');
61
62 2
        $requiredValidator = $this->getRuleValidator('required');
63
64
        if (in_array($anotherValue, $definedValues, is_bool($anotherValue) || null === $anotherValue)) {
65 2
            return ! $requiredValidator->check($value);
66
        }
67
68 2
        return true;
69
    }
70
}
71