Completed
Push — master ( c6c9a7...0c7f9d )
by Albert
08:53
created

RuleChainTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 73
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getField() 0 4 1
A setValidatorAndField() 0 9 1
A addField() 0 4 1
A addRule() 0 4 1
A validate() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Albert221\Validation;
6
7
trait RuleChainTrait
8
{
9
    /**
10
     * @var Validator
11
     */
12
    private $validator;
13
14
    /**
15
     * @var Field|null
16
     */
17
    private $field;
18
19
    /**
20
     * @return Field
21
     */
22
    public function getField(): ?Field
23
    {
24
        return $this->field;
25
    }
26
27
    /**
28
     * @param Validator $validator
29
     * @param Field $field
30
     *
31
     * @return Rule
32
     *
33
     * @internal
34
     */
35
    public function setValidatorAndField(Validator $validator, Field $field): Rule
36
    {
37
        $this->validator = $validator;
38
        $this->field = $field;
39
40
        /** @var Rule $rule */
41
        $rule = $this;
42
        return $rule;
43
    }
44
45
    //
46
    // Methods taken from Field and ValidatorBuilder for easy methods chaining.
47
    //
48
49
    /**
50
     * @param string $name
51
     *
52
     * @return Field
53
     */
54
    public function addField(string $name): Field
55
    {
56
        return $this->validator->addField($name);
57
    }
58
59
    /**
60
     * @param $rule
61
     * @param array $options
62
     *
63
     * @return Rule
64
     */
65
    public function addRule($rule, array $options = []): Rule
66
    {
67
        return $this->field->addRule($rule, $options);
68
    }
69
70
    /**
71
     * @param $data
72
     *
73
     * @return VerdictList
74
     */
75
    public function validate($data): VerdictList
76
    {
77
        return $this->validator->validate($data);
78
    }
79
}
80