1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: harry |
5
|
|
|
* Date: 2/16/18 |
6
|
|
|
* Time: 11:01 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace PluginSimpleValidate\BaseAbstract; |
10
|
|
|
|
11
|
|
|
use PluginSimpleValidate\Libraries\Language; |
12
|
|
|
|
13
|
|
|
abstract class Field implements \PluginSimpleValidate\Contracts\Field |
14
|
|
|
{ |
15
|
|
|
const VAR_LIMIT = 'limit'; |
16
|
|
|
|
17
|
|
|
const VAR_MATCH = 'match'; |
18
|
|
|
|
19
|
|
|
const VAR_LOWER_LIMIT = 'lower'; |
20
|
|
|
|
21
|
|
|
const VAR_UPPER_LIMIT = 'upper'; |
22
|
|
|
|
23
|
|
|
const VAR_MESSAGE = 'message'; |
24
|
|
|
|
25
|
|
|
const VAR_REGION = 'region'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var mixed |
34
|
|
|
*/ |
35
|
|
|
protected $value; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $errors; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
* array of Rule |
45
|
|
|
*/ |
46
|
|
|
protected $rules = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $status; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \PluginSimpleValidate\Contracts\RuleMapping |
55
|
|
|
*/ |
56
|
|
|
protected $ruleMapping; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Field constructor. |
60
|
|
|
* @param string $name |
61
|
|
|
* @param mixed $value |
62
|
|
|
*/ |
63
|
10 |
|
public function __construct( |
64
|
|
|
string $name, |
65
|
|
|
$value |
66
|
|
|
) { |
67
|
10 |
|
$this->name = $name; |
68
|
10 |
|
$this->value = $value; |
69
|
10 |
|
$this->errors = []; |
70
|
10 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Language $language |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
9 |
View Code Duplication |
public function isValid(Language $language) : bool |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
// empty the `errors` array |
|
|
|
|
79
|
9 |
|
$this->emptyErrors(); |
80
|
|
|
|
81
|
|
|
/** @var \PluginSimpleValidate\Contracts\Rule $rule */ |
82
|
9 |
|
foreach ($this->rules as $ruleName => $rule) { |
83
|
9 |
|
if (!$rule->isValid($language, $this->value)) { |
84
|
9 |
|
$this->status = false; |
85
|
9 |
|
$this->errors[] = $rule->getError(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
9 |
|
if (empty($this->errors)) { |
90
|
1 |
|
$this->status = true; |
91
|
|
|
} |
92
|
|
|
|
93
|
9 |
|
return $this->status; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
3 |
|
public function getName(): string |
100
|
|
|
{ |
101
|
3 |
|
return $this->name; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
1 |
|
public function getValue() |
108
|
|
|
{ |
109
|
1 |
|
return $this->value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param mixed $value |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
1 |
|
public function setValue($value) |
117
|
|
|
{ |
118
|
1 |
|
$this->value = $value; |
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getRules(): array |
126
|
|
|
{ |
127
|
|
|
return $this->rules; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
8 |
|
public function getErrors(): array |
134
|
|
|
{ |
135
|
8 |
|
return $this->errors; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $rulesMethod |
140
|
|
|
* @param array $args |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
9 |
|
protected function addRules(string $rulesMethod, array $args = []) |
144
|
|
|
{ |
145
|
9 |
|
$this->rules[$rulesMethod] = \PluginSimpleValidate\Libraries\RuleMapping::getInstance()->getRule($rulesMethod, $args); |
146
|
9 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
9 |
|
protected function emptyErrors() |
153
|
|
|
{ |
154
|
9 |
|
$this->errors = []; |
155
|
9 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.