Passed
Push — main ( e61c6f...c95f3c )
by Breno
02:01
created

RuleSet::withRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation;
5
6
use BrenoRoosevelt\Validation\Exception\ValidateOrFailTrait;
7
use BrenoRoosevelt\Validation\Rules\AllowEmpty;
8
use BrenoRoosevelt\Validation\Rules\AllowNull;
9
use BrenoRoosevelt\Validation\Rules\IsEmpty;
10
use BrenoRoosevelt\Validation\Rules\Required;
11
12
class RuleSet implements Rule, BelongsToField, Stopable
13
{
14
    use RuleChainTrait, BelongsToFieldTrait, StopableTrait, ValidateOrFailTrait;
15
16
    /** @var Rule[] */
17
    private array $rules = [];
18
19
    final public function __construct(?string $field = null, Rule | RuleSet ...$rules)
20
    {
21
        $this->field = $field;
22
        foreach ($rules as $ruleOrRuleSet) {
23
            array_push(
24
                $this->rules,
25
                ...($ruleOrRuleSet instanceof RuleSet ? $ruleOrRuleSet->rules() : [$ruleOrRuleSet])
26
            );
27
        }
28
    }
29
30
    public static function new(): self
31
    {
32
        return new self;
33
    }
34
35
    public static function of(string $field, Rule | RuleSet ...$rules): self
36
    {
37
        return new self($field, ...$rules);
38
    }
39
40
    public static function withRules(Rule | RuleSet ...$rules): self
41
    {
42
        return new self(null, ...$rules);
43
    }
44
45
    public function add(Rule | RuleSet ...$rules): static
46
    {
47
        return new self($this->field, ...$this->rules, ...$rules);
48
    }
49
50
    /** @inheritDoc */
51
    public function validate(mixed $input, array $context = []): Result
52
    {
53
        if (!$this->shouldValidate($input, $context)) {
54
            return ErrorReporting::success();
55
        }
56
57
        $errorReporting = new ErrorReporting;
58
        foreach ($this->rules as $rule) {
59
            if ($rule instanceof BelongsToField) {
60
                $rule = $rule->setField($this->getField());
61
            }
62
63
            $result = $rule->validate($input, $context);
64
            $errorReporting = $errorReporting->add($result);
65
            if ($this->shouldStop($rule, $result)) {
66
                $this->stopOnFailure = true;
67
                break;
68
            }
69
        }
70
71
        return $errorReporting;
72
    }
73
74
    private function shouldValidate(mixed $input, array $context): bool
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

74
    private function shouldValidate(mixed $input, /** @scrutinizer ignore-unused */ array $context): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        if (null === $input && $this->containsRuleType(AllowNull::class)) {
77
            return false;
78
        }
79
80
        if ((new IsEmpty)->isValid($input) && $this->containsRuleType(AllowEmpty::class)) {
81
            return false;
82
        }
83
84
        return true;
85
    }
86
87
    private function shouldStop(Rule $rule, Result $result): bool
88
    {
89
        return $rule instanceof Stopable && $rule->stopOnFailure() && !$result->isOk();
90
    }
91
92
    public function isEmpty(): bool
93
    {
94
        return empty($this->rules);
95
    }
96
97
    /** @return Rule[] */
98
    public function rules(): array
99
    {
100
        return $this->rules;
101
    }
102
103
    public function containsRuleType(string $ruleClassName): bool
104
    {
105
        foreach ($this->rules as $rule) {
106
            if (is_a($rule, $ruleClassName, true)) {
107
                return true;
108
            }
109
        }
110
111
        return false;
112
    }
113
}
114