1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models\Validators; |
3
|
|
|
|
4
|
|
|
use Eloquence\Behaviours\Slug; |
5
|
|
|
use Illuminate\Support\Facades\Validator as FacadeValidator; |
6
|
|
|
use Illuminate\Validation\Rule; |
7
|
|
|
use Illuminate\Validation\Validator; |
8
|
|
|
|
9
|
|
View Code Duplication |
class DiscussCategoryValidator |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get a 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
|
|
|
'title' => 'required|min:5', |
22
|
|
|
'slug' => 'unique:discuss_categories', |
23
|
|
|
'color' => 'min:7|max:7', |
24
|
|
|
'description' => 'required|min:10' |
25
|
|
|
]; |
26
|
|
|
$data['slug'] = Slug::fromTitle($data['title']); |
27
|
|
|
|
28
|
|
|
return FacadeValidator::make($data, $rules); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get a validator for an incoming update request. |
33
|
|
|
* |
34
|
|
|
* @param array $data The data to validate. |
35
|
|
|
* @param int $id The actual category id to ignore the slug rule. |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\Validation\Validator |
38
|
|
|
*/ |
39
|
|
|
public static function update(array $data, int $id): Validator |
40
|
|
|
{ |
41
|
|
|
$rules = [ |
42
|
|
|
'title' => 'required|min:5', |
43
|
|
|
'slug' => [ |
44
|
|
|
Rule::unique('discuss_categories')->ignore($id) |
45
|
|
|
], |
46
|
|
|
'color' => 'min:7|max:7', |
47
|
|
|
'description' => 'required|min:10' |
48
|
|
|
]; |
49
|
|
|
$data['slug'] = Slug::fromTitle($data['title']); |
50
|
|
|
|
51
|
|
|
return FacadeValidator::make($data, $rules); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
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.