Completed
Push — develop ( 5abd4c...dae2aa )
by Abdelrahman
09:07
created

AccountAttributesRequest::rules()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 6
eloc 17
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Illuminate\Foundation\Http\FormRequest;
8
9
class AccountAttributesRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16
    public function authorize(): bool
17
    {
18
        return true;
19
    }
20
21
    /**
22
     * Get the validation rules that apply to the request.
23
     *
24
     * @return array
25
     */
26
    public function rules(): array
27
    {
28
        $user = $this->user($this->get('guard'));
29
30
        // Attach attribute rules
31
        $user->getEntityAttributes()->each(function ($attribute, $attributeSlug) use (&$rules) {
32
            switch ($attribute->type) {
33
                case 'datetime':
34
                    $type = 'date';
35
                    break;
36
                case 'text':
37
                case 'varchar':
38
                    $type = 'string';
39
                    break;
40
                default:
41
                    $type = $attribute->type;
42
                    break;
43
            }
44
45
            $rule = ($attribute->is_required ? 'required|' : 'nullable|').$type;
46
            $rules[$attributeSlug.($attribute->is_collection ? '.*' : '')] = $rule;
47
        });
48
49
        return $rules ?? [];
50
    }
51
}
52