Completed
Push — master ( ca5bfa...79e784 )
by ARCANEDEV
05:48
created

FormRequest::sanitizedInputs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Arcanedev\Support\Bases;
2
3
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
4
use Illuminate\Contracts\Validation\Validator;
5
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
6
7
/**
8
 * Class     FormRequest
9
 *
10
 * @package  Arcanedev\Support\Laravel
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
abstract class FormRequest extends BaseFormRequest
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * The errors format.
21
     *
22
     * @var string|null
23
     */
24
    protected $errorsFormat = null;
25
26
    /* ------------------------------------------------------------------------------------------------
27
     |  Main Functions
28
     | ------------------------------------------------------------------------------------------------
29
     */
30
    /**
31
     * Determine if the user is authorized to make this request.
32
     *
33
     * @return bool
34
     */
35
    public function authorize()
36
    {
37
        return false;
38
    }
39
40
    /**
41
     * Get the validation rules that apply to the request.
42
     *
43
     * @return array
44
     */
45
    abstract public function rules();
46
47
    /**
48
     * Get the validator instance for the request.
49
     *
50
     * @param  \Illuminate\Contracts\Validation\Factory  $factory
51
     *
52
     * @return \Illuminate\Contracts\Validation\Validator
53
     */
54
    public function validator(ValidationFactory $factory)
55
    {
56
        return $factory->make(
57
            $this->sanitizedInputs(),
58
            $this->container->call([$this, 'rules']),
59
            $this->messages(),
60
            $this->attributes()
61
        );
62
    }
63
64
    /* ------------------------------------------------------------------------------------------------
65
     |  Other Functions
66
     | ------------------------------------------------------------------------------------------------
67
     */
68
    /**
69
     * Sanitize the inputs.
70
     *
71
     * @return array
72
     */
73
    protected function sanitizedInputs()
74
    {
75
        $inputs = $this->all();
76
77
        if (method_exists($this, 'sanitize')) {
78
            $this->replace(
79
                $inputs = $this->container->call([$this, 'sanitize'], [$inputs])
80
            );
81
        }
82
83
        return $inputs;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function formatErrors(Validator $validator)
90
    {
91
        if (is_null($this->errorsFormat)) {
92
            return parent::formatErrors($validator);
93
        }
94
95
        $errors   = [];
96
        $messages = $validator->getMessageBag();
97
98
        foreach ($messages->keys() as $key) {
99
            $errors[$key] = $messages->get($key, $this->errorsFormat);
100
        }
101
102
        return $errors;
103
    }
104
}
105