Completed
Pull Request — master (#7)
by Albert
02:45
created

Rule::getField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 Validator $validator
40
     * @param Field $field
41
     * @param array $options
42
     */
43 2
    public function __construct(Validator $validator, Field $field, array $options = [])
44
    {
45 2
        $this->field = $field;
46 2
        $this->options = $options;
47 2
        $this->validator = $validator;
48 2
    }
49
50
    /**
51
     * @return Field
52
     */
53 2
    public function getField(): Field
54
    {
55 2
        return $this->field;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    protected function getOptions(): array
62
    {
63
        return $this->options;
64
    }
65
66
    /**
67
     * @param string $name
68
     * @param $value
69
     *
70
     * @return Rule
71
     */
72
    public function setOption(string $name, $value)
73
    {
74
        $this->options[$name] = $value;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getMessage(): string
83
    {
84
        return $this->message;
85
    }
86
87
    /**
88
     * @param string $message
89
     *
90
     * @return Rule
91
     */
92
    public function setMessage(string $message)
93
    {
94
        $this->message = $message;
95
96
        return $this;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 2
    public function validatedBy(): string
103
    {
104 2
        return get_class($this) . 'Validator';
105
    }
106
107
    //
108
    // Methods taken from Field and ValidatorBuilder for easy methods chaining.
109
    //
110
111
    /**
112
     * @param string $name
113
     *
114
     * @return Field
115
     */
116 2
    public function addField(string $name): Field
117
    {
118 2
        return $this->validator->addField($name);
119
    }
120
121
    /**
122
     * @param $rule
123
     * @param array $options
124
     *
125
     * @return Rule
126
     */
127
    public function addRule($rule, array $options = []): Rule
128
    {
129
        return $this->field->addRule($rule, $options);
130
    }
131
132
    /**
133
     * @param $data
134
     *
135
     * @return VerdictList
136
     */
137 2
    public function validate($data): VerdictList
138
    {
139 2
        return $this->validator->validate($data);
140
    }
141
}
142