CategoryRequest::prepareForValidation()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Adminetic\Website\Http\Requests;
4
5
use Adminetic\Website\Models\Admin\Category;
6
use Illuminate\Foundation\Http\FormRequest;
7
use Illuminate\Support\Str;
8
9
class CategoryRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     */
14
    public function authorize(): bool
15
    {
16
        return true;
17
    }
18
19
    /**
20
     * Prepare the data for validation.
21
     */
22
    protected function prepareForValidation(): void
23
    {
24
        $this->merge([
25
            'root_parent_id' => $this->getMainCategory($this->category->parent_id ?? null),
26
            'slug' => ! is_null($this->name) ? Str::slug($this->name) : null,
27
            'meta_name' => $this->category->meta_name ?? $this->meta_name ?? $this->name ?? null,
28
            'meta_description' => $this->category->meta_description ?? $this->meta_description ?? $this->excerpt ?? null,
29
        ]);
30
    }
31
32
    /**
33
     * Get the validation rules that apply to the request.
34
     *
35
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
36
     */
37
    public function rules(): array
38
    {
39
        $id = $this->category->id ?? '';
40
41
        return [
42
            'slug' => 'required|max:100|unique:'.config('website.table_prefix', 'website').'_categories,slug,'.$id,
43
            'name' => 'required|max:100|unique:'.config('website.table_prefix', 'website').'_categories,name,'.$id,
44
            'excerpt' => 'nullable|max:5500',
45
            'parent_id' => 'nullable|exists:'.config('website.table_prefix', 'website').'_categories,id',
46
            'root_parent_id' => 'nullable|exists:'.config('website.table_prefix', 'website').'_categories,id',
47
            'model' => 'nullable|max:50',
48
            'active' => 'sometimes|boolean',
49
            'featured' => 'sometimes|boolean',
50
            'position' => 'nullable|numeric',
51
            'icon' => 'nullable|max:255',
52
            'color' => 'nullable|max:255',
53
            'meta_name' => 'nullable|max:100',
54
            'meta_description' => 'nullable|max:255',
55
            'meta_keywords' => 'nullable|max:100',
56
        ];
57
    }
58
59
    // Get Main Category
60
    public function getMainCategory($given_category_id = null)
61
    {
62
        $parent_id = $given_category_id ?? $this->parent_id ?? null;
63
        $category = Category::find($parent_id);
64
        if (isset($category)) {
65
            while (true) {
66
                if (isset($category->parent_id)) {
67
                    $category = Category::find($category->parent_id);
68
                } else {
69
                    break;
70
                }
71
            }
72
73
            return $category->id;
74
        }
75
76
        return null;
77
    }
78
}
79