Completed
Push — master ( 3e8f18...c69213 )
by Abdelrahman
02:02
created

GuardianFormRequest::rules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Rinvex\Support\Traits\Escaper;
8
use Illuminate\Foundation\Http\FormRequest;
9
10
class GuardianFormRequest extends FormRequest
11
{
12
    use Escaper;
13
14
    /**
15
     * Determine if the user is authorized to make this request.
16
     *
17
     * @return bool
18
     */
19
    public function authorize(): bool
20
    {
21
        return true;
22
    }
23
24
    /**
25
     * Prepare the data for validation.
26
     *
27
     * @return void
28
     */
29
    protected function prepareForValidation(): void
30
    {
31
        $data = $this->all();
32
33
        $guardian = $this->route('guardian') ?? app('cortex.auth.guardian');
34
35
        if ($guardian->exists && empty($data['password'])) {
36
            unset($data['password'], $data['password_confirmation']);
37
        }
38
39
        $this->replace($data);
40
    }
41
42
    /**
43
     * Get the validation rules that apply to the request.
44
     *
45
     * @return array
46
     */
47
    public function rules(): array
48
    {
49
        $guardian = $this->route('guardian') ?? app('cortex.auth.guardian');
50
        $guardian->updateRulesUniques();
51
        $rules = $guardian->getRules();
52
53
        $rules['password'] = $guardian->exists
54
            ? 'confirmed|min:'.config('cortex.auth.password_min_chars')
55
            : 'required|confirmed|min:'.config('cortex.auth.password_min_chars');
56
57
        return $rules;
58
    }
59
}
60