ProhibitedUnless::values()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 2
cp 0
crap 2
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 ProhibitedUnless 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
        $this->params['field']  = array_shift($params);
29
        $this->params['values'] = $this->convertStringsToBoolean($params);
30
31
        return $this;
32
    }
33
34
    public function field(string $field): static
35
    {
36
        $this->params['field'] = $field;
37
38
        return $this;
39
    }
40
41
    public function values(array $values): static
42
    {
43
        $this->params['values'] = $values;
44
45
        return $this;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function check($value): bool
52
    {
53
        $this->requireParameters(['field', 'values']);
54
55
        $anotherAttribute = $this->parameter('field');
56
        $definedValues    = $this->parameter('values');
57
        $anotherValue     = $this->getAttribute()->getValue($anotherAttribute);
58
59
        $this->setParameterTextValues($this->convertBooleansToString($definedValues), 'other');
60
        $this->setParameterTextValues([$anotherValue], 'values');
61
62
        $requiredValidator = $this->getRuleValidator('required');
63
64
        if (! in_array($anotherValue, $definedValues, is_bool($anotherValue) || null === $anotherValue)) {
65
            return ! $requiredValidator->check($value);
66
        }
67
68
        return true;
69
    }
70
}
71