Completed
Push — master ( 71eacb...140864 )
by Albert
01:23
created

Rule::setValidatorAndField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Albert221\Validation\Rule;
6
7
use Albert221\Validation\Field;
8
use Albert221\Validation\ValidationState;
9
use Albert221\Validation\Validator;
10
use Albert221\Validation\Verdict;
11
use Albert221\Validation\VerdictInterface;
12
use Albert221\Validation\VerdictList;
13
14
abstract class Rule
15
{
16
    /**
17
     * @var Validator
18
     */
19
    private $validator;
20
21
    /**
22
     * @var Field
23
     */
24
    private $field;
25
26
    /**
27
     * @var array
28
     */
29
    protected $options;
30
31
    /**
32
     * @var string
33
     */
34
    protected $message;
35
36
    /**
37
     * Rule constructor.
38
     *
39
     * @param array $options
40
     */
41 2
    public function __construct(array $options = [])
42
    {
43 2
        $this->options = $options;
44 2
    }
45
46
    /**
47
     * @param Validator $validator
48
     * @param Field $field
49
     *
50
     * @return Rule
51
     *
52
     * @internal
53
     */
54 2
    public function setValidatorAndField(Validator $validator, Field $field): Rule
55
    {
56 2
        $this->validator = $validator;
57 2
        $this->field = $field;
58
59 2
        return $this;
60
    }
61
62
    /**
63
     * @return Field
64
     */
65 2
    public function getField(): Field
66
    {
67 2
        return $this->field;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    protected function getOptions(): array
74
    {
75
        return $this->options;
76
    }
77
78
    /**
79
     * @param string $name
80
     * @param $value
81
     *
82
     * @return Rule
83
     */
84
    public function setOption(string $name, $value)
85
    {
86
        $this->options[$name] = $value;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getMessage(): string
95
    {
96
        return $this->message;
97
    }
98
99
    /**
100
     * @param string $message
101
     *
102
     * @return Rule
103
     */
104
    public function setMessage(string $message)
105
    {
106
        $this->message = $message;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114 2
    public function validatedBy(): string
115
    {
116 2
        return get_class($this) . 'Validator';
117
    }
118
119
    //
120
    // Methods taken from Field and ValidatorBuilder for easy methods chaining.
121
    //
122
123
    /**
124
     * @param string $name
125
     *
126
     * @return Field
127
     */
128 2
    public function addField(string $name): Field
129
    {
130 2
        return $this->validator->addField($name);
131
    }
132
133
    /**
134
     * @param $rule
135
     * @param array $options
136
     *
137
     * @return Rule
138
     */
139
    public function addRule($rule, array $options = []): Rule
140
    {
141
        return $this->field->addRule($rule, $options);
142
    }
143
144
    /**
145
     * @param $data
146
     *
147
     * @return VerdictList
148
     */
149 2
    public function validate($data): VerdictList
150
    {
151 2
        return $this->validator->validate($data);
152
    }
153
}
154