Completed
Push — develop ( 8b1080...56861b )
by Abdelrahman
05:19
created

GuardianFormRequest::rules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
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 GuardianFormRequest 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
     * Prepare the data for validation.
23
     *
24
     * @return void
25
     */
26
    protected function prepareForValidation(): void
27
    {
28
        $data = $this->all();
29
30
        $guardian = $this->route('guardian') ?? app('cortex.auth.guardian');
31
32
        if ($guardian->exists && empty($data['password'])) {
33
            unset($data['password'], $data['password_confirmation']);
34
        }
35
36
        $this->replace($data);
37
    }
38
39
    /**
40
     * Get the validation rules that apply to the request.
41
     *
42
     * @return array
43
     */
44
    public function rules(): array
45
    {
46
        $guardian = $this->route('guardian') ?? app('cortex.auth.guardian');
47
        $guardian->updateRulesUniques();
48
        $rules = $guardian->getRules();
49
50
        $rules['password'] = $guardian->exists
51
            ? 'confirmed|min:'.config('cortex.auth.password_min_chars')
52
            : 'required|confirmed|min:'.config('cortex.auth.password_min_chars');
53
54
        return $rules;
55
    }
56
}
57