1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models\Validators; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\Validator as FacadeValidator; |
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Validation\Rule; |
7
|
|
|
use Illuminate\Validation\Validator; |
8
|
|
|
|
9
|
|
|
class SettingValidator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get the validator for an incoming create request. |
13
|
|
|
* |
14
|
|
|
* @param array $data The data to validate. |
15
|
|
|
* |
16
|
|
|
* @return \Illuminate\Validation\Validator |
17
|
|
|
*/ |
18
|
|
|
public static function create(array $data): Validator |
19
|
|
|
{ |
20
|
|
|
$rules = [ |
21
|
|
|
'name' => 'required|min:3|max:30|unique:settings', |
22
|
|
|
'value' => 'required', |
23
|
|
|
'type' => [ |
24
|
|
|
'required', |
25
|
|
|
Rule::in(['value_int', 'value_str', 'value_bool']) |
26
|
|
|
], |
27
|
|
|
'description' => 'max:150' |
28
|
|
|
]; |
29
|
|
|
$data['name'] = Str::slug($data['name'], '.'); |
30
|
|
|
|
31
|
|
|
return FacadeValidator::make($data, $rules); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get a validator for an incoming update request. |
36
|
|
|
* |
37
|
|
|
* @param array $data The data to validate. |
38
|
|
|
* @param int $id The actual setting id to ignore the name rule. |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Validation\Validator |
41
|
|
|
*/ |
42
|
|
|
public static function update(array $data, int $id): Validator |
43
|
|
|
{ |
44
|
|
|
$rules = [ |
45
|
|
|
'name' => [ |
46
|
|
|
'required', |
47
|
|
|
'min:3', |
48
|
|
|
'max:30', |
49
|
|
|
Rule::unique('settings')->ignore($id) |
50
|
|
|
], |
51
|
|
|
'value' => 'required', |
52
|
|
|
'type' => [ |
53
|
|
|
'required', |
54
|
|
|
Rule::in(['value_int', 'value_str', 'value_bool']) |
55
|
|
|
], |
56
|
|
|
'description' => 'max:150' |
57
|
|
|
]; |
58
|
|
|
$data['name'] = Str::slug($data['name'], '.'); |
59
|
|
|
|
60
|
|
|
return FacadeValidator::make($data, $rules); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|