Completed
Push — master ( beada0...f16f53 )
by ARCANEDEV
06:00
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 10
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\Validator;
4
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
5
use Illuminate\Http\JsonResponse;
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
     * Specify if the form request is an ajax request.
21
     *
22
     * @var bool
23
     */
24
    protected $ajaxRequest = false;
25
26
    /**
27
     * The errors format.
28
     *
29
     * @var string|null
30
     */
31
    protected $errorsFormat = null;
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Main Functions
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Determine if the user is authorized to make this request.
39
     *
40
     * @return bool
41
     */
42
    public function authorize()
43
    {
44
        return false;
45
    }
46
47
    /**
48
     * Get the validation rules that apply to the request.
49
     *
50
     * @return array
51
     */
52
    abstract public function rules();
53
54
    /**
55
     * Get data to be validated from the request.
56
     *
57
     * @return array
58
     */
59
    protected function validationData()
60
    {
61
        if (method_exists($this, 'sanitize')) {
62
            $this->merge(
63
                $this->container->call([$this, 'sanitize'])
64
            );
65
        }
66
67
        return $this->all();
68
    }
69
70
    /**
71
     * Get the proper failed validation response for the request.
72
     *
73
     * @param  array  $errors
74
     *
75
     * @return \Symfony\Component\HttpFoundation\Response
76
     */
77
    public function response(array $errors)
78
    {
79
        return $this->ajaxRequest
80
            ? $this->formatJsonErrorsResponse($errors)
81
            : parent::response($errors);
82
    }
83
84
    /* ------------------------------------------------------------------------------------------------
85
     |  Other Functions
86
     | ------------------------------------------------------------------------------------------------
87
     */
88
    /**
89
     * Format the json response.
90
     *
91
     * @param  array  $errors
92
     *
93
     * @return \Symfony\Component\HttpFoundation\Response
94
     */
95
    protected function formatJsonErrorsResponse(array $errors)
96
    {
97
        return new JsonResponse([
98
            'status' => 'error',
99
            'code'   => 422,
100
            'errors' => array_map('reset', $errors)
101
        ], 422);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function formatErrors(Validator $validator)
108
    {
109
        if (is_null($this->errorsFormat)) {
110
            return parent::formatErrors($validator);
111
        }
112
113
        $errors   = [];
114
        $messages = $validator->getMessageBag();
115
116
        foreach ($messages->keys() as $key) {
117
            $errors[$key] = $messages->get($key, $this->errorsFormat);
118
        }
119
120
        return $errors;
121
    }
122
}
123