RuleChainTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 73
ccs 13
cts 13
cp 1
rs 10
c 0
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 validate() 0 4 1
A addRule() 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 10
    public function getField(): ?Field
23
    {
24 10
        return $this->field;
25
    }
26
27
    /**
28
     * @param Validator $validator
29
     * @param Field $field
30
     *
31
     * @return Rule
32
     *
33
     * @internal
34
     */
35 2
    public function setValidatorAndField(Validator $validator, Field $field): Rule
36
    {
37 2
        $this->validator = $validator;
38 2
        $this->field = $field;
39
40
        /** @var Rule $rule */
41 2
        $rule = $this;
42 2
        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 2
    public function addField(string $name): Field
55
    {
56 2
        return $this->validator->addField($name);
57
    }
58
59
    /**
60
     * @param $rule
61
     * @param array $options
62
     *
63
     * @return Rule
64
     */
65 2
    public function addRule($rule, array $options = []): Rule
66
    {
67 2
        return $this->field->addRule($rule, $options);
68
    }
69
70
    /**
71
     * @param $data
72
     *
73
     * @return VerdictList
74
     */
75 2
    public function validate($data): VerdictList
76
    {
77 2
        return $this->validator->validate($data);
78
    }
79
}
80