CategoryRequest::rules()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 22
rs 9.8666
c 1
b 0
f 0
cc 4
nc 5
nop 0
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