Passed
Push — main ( ff7ab4...7d3a77 )
by Breno
01:49
created

RuleSet   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 12
Bugs 1 Features 0
Metric Value
eloc 39
c 12
b 1
f 0
dl 0
loc 110
rs 10
wmc 27

13 Methods

Rating   Name   Duplication   Size   Complexity  
A withRules() 0 3 1
A new() 0 3 1
A of() 0 3 1
A __construct() 0 7 3
A add() 0 3 1
A rules() 0 3 1
A hasRequired() 0 3 1
A someRule() 0 9 3
A hasAllowsNull() 0 3 1
A hasAllowsEmpty() 0 3 1
B validate() 0 21 7
A isEmpty() 0 3 1
A shouldValidate() 0 11 5
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation;
5
6
use BrenoRoosevelt\Validation\Exception\ValidateOrFailTrait;
7
use BrenoRoosevelt\Validation\Rules\AllowsEmpty;
8
use BrenoRoosevelt\Validation\Rules\AllowsNull;
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 ($rule instanceof Stopable && $rule->stopOnFailure() && !$result->isOk()) {
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->hasAllowsNull()) {
77
            return false;
78
        }
79
80
        if ((new IsEmpty)->isValid($input) && $this->hasAllowsEmpty()) {
81
            return false;
82
        }
83
84
        return true;
85
    }
86
87
    public function hasRequired(): bool
88
    {
89
        return $this->someRule(fn(Rule $rule) => $rule instanceof Required);
90
    }
91
92
    public function hasAllowsNull(): bool
93
    {
94
        return $this->someRule(fn(Rule $rule) => $rule instanceof AllowsNull);
95
    }
96
97
    public function hasAllowsEmpty(): bool
98
    {
99
        return $this->someRule(fn(Rule $rule) => $rule instanceof AllowsEmpty);
100
    }
101
102
    public function isEmpty(): bool
103
    {
104
        return empty($this->rules);
105
    }
106
107
    /** @return Rule[] */
108
    public function rules(): array
109
    {
110
        return $this->rules;
111
    }
112
113
    private function someRule(callable $callback): bool
114
    {
115
        foreach ($this->rules as $rule) {
116
            if (true === call_user_func_array($callback, [$rule])) {
117
                return true;
118
            }
119
        }
120
121
        return false;
122
    }
123
}
124