1 | <?php |
||
2 | |||
3 | namespace SeedsStd\SlugValidation; |
||
4 | |||
5 | use Illuminate\Contracts\Validation\Rule; |
||
6 | use Illuminate\Support\Str; |
||
7 | |||
8 | class Slug implements Rule |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | protected $configs; |
||
14 | |||
15 | /** |
||
16 | * Create a new rule instance. |
||
17 | * |
||
18 | * @param array $configs |
||
19 | * @return void |
||
20 | */ |
||
21 | public function __construct(array $configs = []) |
||
22 | { |
||
23 | $default_configs = [ |
||
24 | 'separator' => '-', |
||
25 | 'language' => 'en', |
||
26 | ]; |
||
27 | |||
28 | $this->configs = array_merge($default_configs, $configs); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Determine if the validation rule passes. |
||
33 | * |
||
34 | * @param string $attribute |
||
35 | * @param mixed $value |
||
36 | * @return bool |
||
37 | */ |
||
38 | public function passes($attribute, $value) |
||
39 | { |
||
40 | return $value === Str::slug($value, $this->configs['separator'], $this->configs['language']); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Get the validation error message. |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | public function message() |
||
49 | { |
||
50 | return __('validation.slug'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
51 | } |
||
52 | } |
||
53 |