Completed
Push — master ( f16f53...9cb400 )
by ARCANEDEV
08:30
created

FormRequest::prepareForValidation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 8
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
     * Prepare the data for validation.
56
     */
57
    protected function prepareForValidation()
58
    {
59
        if (method_exists($this, 'sanitize')) {
60
            $this->merge(
61
                $this->container->call([$this, 'sanitize'])
62
            );
63
        }
64
    }
65
66
    /**
67
     * Get the proper failed validation response for the request.
68
     *
69
     * @param  array  $errors
70
     *
71
     * @return \Symfony\Component\HttpFoundation\Response
72
     */
73
    public function response(array $errors)
74
    {
75
        return $this->ajaxRequest
76
            ? $this->formatJsonErrorsResponse($errors)
77
            : parent::response($errors);
78
    }
79
80
    /* ------------------------------------------------------------------------------------------------
81
     |  Other Functions
82
     | ------------------------------------------------------------------------------------------------
83
     */
84
    /**
85
     * Format the json response.
86
     *
87
     * @param  array  $errors
88
     *
89
     * @return \Symfony\Component\HttpFoundation\Response
90
     */
91
    protected function formatJsonErrorsResponse(array $errors)
92
    {
93
        return new JsonResponse([
94
            'status' => 'error',
95
            'code'   => 422,
96
            'errors' => array_map('reset', $errors)
97
        ], 422);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    protected function formatErrors(Validator $validator)
104
    {
105
        if (is_null($this->errorsFormat)) {
106
            return parent::formatErrors($validator);
107
        }
108
109
        $errors   = [];
110
        $messages = $validator->getMessageBag();
111
112
        foreach ($messages->keys() as $key) {
113
            $errors[$key] = $messages->get($key, $this->errorsFormat);
114
        }
115
116
        return $errors;
117
    }
118
}
119