Completed
Push — master ( 6630f6...b334d3 )
by Albert
05:18
created

Rule::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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