CategoryRequest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 13
dl 0
loc 29
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A rules() 0 22 4
1
<?php
2
3
namespace WebDevEtc\BlogEtc\Requests;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Validation\Rule;
7
8
class CategoryRequest extends BaseAdminRequest
9
{
10
    /**
11
     * Get the validation rules that apply to the request.
12
     *
13
     * @return array
14
     */
15
    public function rules(): array
16
    {
17
        if (Request::METHOD_DELETE === $this->method()) {
18
            // No rules are required for deleting.
19
            return [];
20
        }
21
        $rules = [
22
            'category_name'        => ['required', 'string', 'min:1', 'max:200'],
23
            'slug'                 => ['required', 'alpha_dash', 'max:100', 'min:1'],
24
            'category_description' => ['nullable', 'string', 'min:1', 'max:5000'],
25
        ];
26
27
        if (Request::METHOD_POST === $this->method()) {
28
            $rules['slug'][] = Rule::unique('blog_etc_categories', 'slug');
29
        }
30
31
        if (in_array($this->method(), [Request::METHOD_PUT, Request::METHOD_PATCH], true)) {
32
            $rules['slug'][] = Rule::unique('blog_etc_categories', 'slug')
33
                ->ignore($this->route()->parameter('categoryId'));
34
        }
35
36
        return $rules;
37
    }
38
}
39