1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
4
|
|
|
|
5
|
|
|
use Illuminate\Validation\Rules\Unique; |
6
|
|
|
use Sco\Admin\Contracts\Validatable; |
7
|
|
|
|
8
|
|
|
abstract class NamedElement extends Element implements Validatable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $size = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
protected $disabled = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var mixed |
22
|
|
|
*/ |
23
|
|
|
protected $defaultValue = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $validationRules = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $validationMessages = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function getSize() |
39
|
|
|
{ |
40
|
|
|
return $this->size; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $value |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function setSize(string $value) |
48
|
|
|
{ |
49
|
|
|
$this->size = $value; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function largeSize() |
58
|
|
|
{ |
59
|
|
|
return $this->setSize('large'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function mediumSize() |
66
|
|
|
{ |
67
|
|
|
return $this->setSize('medium'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function smallSize() |
74
|
|
|
{ |
75
|
|
|
return $this->setSize('small'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function miniSize() |
82
|
|
|
{ |
83
|
|
|
return $this->setSize('mini'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function isDisabled() |
90
|
|
|
{ |
91
|
|
|
return $this->disabled; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function disabled() |
98
|
|
|
{ |
99
|
|
|
$this->disabled = true; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param null|string $message |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function required($message = null) |
109
|
|
|
{ |
110
|
|
|
$this->addValidationRule('required', $message); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param null|string $message |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function unique($message = null) |
120
|
|
|
{ |
121
|
|
|
$this->addValidationRule('unique', $message); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function getValidationRules() |
130
|
|
|
{ |
131
|
|
|
$rules = array_merge( |
132
|
|
|
$this->getDefaultValidationRules(), |
133
|
|
|
$this->validationRules |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
if (isset($rules['unique']) && $rules['unique'] == 'unique') { |
137
|
|
|
$model = $this->getModel(); |
138
|
|
|
$table = $model->getTable(); |
139
|
|
|
|
140
|
|
|
$rule = new Unique($table, $this->getName()); |
141
|
|
|
if ($model->exists) { |
142
|
|
|
$rule->ignore($model->getKey(), $model->getKeyName()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$rules['unique'] = $rule; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return [$this->getName() => array_values($rules)]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
protected function getDefaultValidationRules() |
155
|
|
|
{ |
156
|
|
|
return [ |
157
|
|
|
'bail' => 'bail', |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function getValidationMessages() |
165
|
|
|
{ |
166
|
|
|
return $this->validationMessages; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get validation custom attributes |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
public function getValidationTitles() |
175
|
|
|
{ |
176
|
|
|
return [$this->getName() => $this->getTitle()]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $rule |
181
|
|
|
* @param null $message |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
View Code Duplication |
public function addValidationRule($rule, $message = null) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$this->validationRules[$this->getValidationRuleName($rule)] = $rule; |
187
|
|
|
|
188
|
|
|
if (is_null($message)) { |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->addValidationMessage($rule, $message); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param $rule |
197
|
|
|
* @param $message |
198
|
|
|
* @return $this |
199
|
|
|
*/ |
200
|
|
|
public function addValidationMessage($rule, $message) |
201
|
|
|
{ |
202
|
|
|
$key = $this->getName() . '.' . $this->getValidationRuleName($rule); |
203
|
|
|
|
204
|
|
|
$this->validationMessages[$key] = $message; |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param $rule |
211
|
|
|
* @return mixed |
212
|
|
|
*/ |
213
|
|
|
protected function getValidationRuleName(string $rule) |
214
|
|
|
{ |
215
|
|
|
list($name,) = explode(':', $rule, 2); |
216
|
|
|
|
217
|
|
|
return $name; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function toArray() |
221
|
|
|
{ |
222
|
|
|
return parent::toArray() + [ |
223
|
|
|
'size' => $this->getSize(), |
224
|
|
|
'disabled' => $this->isDisabled(), |
225
|
|
|
]; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
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.