TValidate::validate()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace kalanis\kw_rules;
4
5
6
use kalanis\kw_rules\Interfaces;
7
use kalanis\kw_rules\Exceptions\RuleException;
8
9
10
/**
11
 * Trait TValidate
12
 * @package kalanis\kw_rules
13
 * Main class for validation - use it as include for your case
14
 */
15
trait TValidate
16
{
17
    /** @var RuleException[][] */
18
    protected array $errors = [];
19
20 5
    public function validate(Interfaces\IValidate $entry): bool
21
    {
22 5
        $this->errors = [];
23 5
        foreach ($entry->getRules() as $rule) {
24
            try {
25 5
                $rule->validate($entry); // for files need a whole object, so pack it all
26 3
            } catch (RuleException $ex) {
27 3
                if (empty($this->errors[$entry->getKey()])) {
28 3
                    $this->errors[$entry->getKey()] = [];
29
                }
30 3
                $this->errors[$entry->getKey()][] = $ex;
31 3
                while ($ex = $ex->/** @scrutinizer ignore-call */getPrev()) {
32 1
                    $this->errors[$entry->getKey()][] = $ex;
33
                }
34
            }
35
        }
36 5
        return empty($this->errors);
37
    }
38
39
    /**
40
     * @return RuleException[][]
41
     */
42 5
    public function getErrors(): array
43
    {
44 5
        return $this->errors;
45
    }
46
}
47