Completed
Push — master ( 140864...c6c9a7 )
by Albert
02:58
created

Rule::setMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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