Completed
Push — master ( 3309bb...059e5b )
by Changwan
02:58
created

AssertRuleDefinition   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 39
cts 41
cp 0.9512
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
D prop() 0 38 10
B checkRules() 0 14 5
1
<?php
2
namespace Wandu\Validator;
3
4
use Closure;
5
use Wandu\Validator\Contracts\Rule;
6
use Wandu\Validator\Contracts\RuleDefinition;
7
8
class AssertRuleDefinition implements RuleDefinition
9
{
10
    /** @var \Wandu\Validator\TesterFactory */
11
    protected $tester;
12
    
13
    /** @var \Wandu\Validator\ErrorBag */
14
    protected $errors;
15
    
16
    /** @var mixed $data */
17
    protected $data;
18
    
19
    /** @var mixed $origin */
20
    protected $origin;
21
    
22 2
    public function __construct(TesterFactory $tester, ErrorBag $errors, $data, $origin)
23
    {
24 2
        $this->tester = $tester;
25 2
        $this->errors = $errors;
26 2
        $this->data = $data;
27 2
        $this->origin = $origin;
28 2
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 2
    public function prop(string $target, ...$rules)
34
    {
35 2
        $targetName = $target;
36 2
        $iterable = 0;
37 2
        $optional = false;
38
39 2
        if (strpos($targetName, '?') !== false) {
40 2
            $targetName = trim($targetName, '?');
41 2
            $optional = true;
42
        }
43 2
        while (strpos($targetName, "[]") !== false) {
44 1
            $targetName = str_replace("[]", "", $targetName);
45 1
            $iterable++;
46
        }
47
48 2
        if ($optional && !isset($this->data[$targetName])) return;
49 2
        if (!isset($this->data[$targetName])) {
50 2
            $this->errors->store("required", [$targetName]);
51 2
            return;
52
        }
53
54 2
        if ($iterable && !is_array($this->data[$targetName])) {
55
            $this->errors->store("array", [$targetName]);
56
            return;
57
        }
58
59 2
        $this->errors->pushPrefix($targetName);
60 2
        if ($iterable) {
61 1
            foreach ($this->data[$targetName] as $index => $subData) {
62 1
                $this->errors->pushPrefix($index);
63 1
                $this->checkRules($rules, $subData, $this->origin);
64 1
                $this->errors->popPrefix();
65
            }
66
        } else {
67 2
            $this->checkRules($rules, $this->data[$targetName], $this->origin);
68
        }
69 2
        $this->errors->popPrefix();
70 2
    }
71
72
    /**
73
     * @param string[]|\Wandu\Validator\Contracts\Rule[]|\Closure[] $rules
74
     * @param mixed $data
75
     * @param mixed $origin
76
     */
77 2
    protected function checkRules(array $rules, $data, $origin)
78
    {
79 2
        foreach ($rules as $rule) {
80 2
            if ($rule instanceof Rule) {
81 1
                $rule->define(new AssertRuleDefinition($this->tester, $this->errors, $data, $origin));
82 2
            } elseif ($rule instanceof Closure) {
83 1
                $rule->__invoke(new AssertRuleDefinition($this->tester, $this->errors, $data, $origin));
84
            } else {
85 2
                if (!$this->tester->parse($rule)->test($data, $origin)) {
86 2
                    $this->errors->store($rule);
87
                }
88
            }
89
        }
90 2
    }
91
}
92