TValidate   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 30
ccs 13
cts 13
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 17 5
A getErrors() 0 3 1
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