Passed
Push — master ( ef2617...12d5d1 )
by Zhengchao
12:27
created

FormRequest::formatValidationErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of emanci/lumen-request package.
5
 *
6
 * (c) emanci <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Emanci\LumenRequest;
13
14
use Illuminate\Contracts\Container\Container;
15
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
16
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
17
use Illuminate\Contracts\Validation\Validator;
18
use Illuminate\Http\JsonResponse;
19
use Illuminate\Http\Request;
20
use Illuminate\Validation\ValidatesWhenResolvedTrait;
21
use Illuminate\Validation\ValidationException;
22
23
abstract class FormRequest extends Request implements ValidatesWhenResolved
24
{
25
    use ValidatesWhenResolvedTrait;
26
27
    /**
28
     * The container instance.
29
     *
30
     * @var \Illuminate\Contracts\Container\Container
31
     */
32
    protected $container;
33
34
    /**
35
     * Get the validation rules that apply to the request.
36
     *
37
     * @return array
38
     */
39
    abstract public function rules();
40
41
    /**
42
     * Get custom messages for validator errors.
43
     *
44
     * @return array
45
     */
46
    public function messages()
47
    {
48
        return [];
49
    }
50
51
    /**
52
     * Get custom attributes for validator errors.
53
     *
54
     * @return array
55
     */
56 3
    public function attributes()
57
    {
58 3
        return [];
59
    }
60
61
    /**
62
     * Set the container implementation.
63
     *
64
     * @param \Illuminate\Contracts\Container\Container $container
65
     *
66
     * @return $this
67
     */
68 3
    public function setContainer(Container $container)
69
    {
70 3
        $this->container = $container;
71
72 3
        return $this;
73
    }
74
75
    /**
76
     * Get the validator instance for the request.
77
     *
78
     * @return \Illuminate\Contracts\Validation\Validator
79
     */
80 3
    protected function getValidatorInstance()
81
    {
82 3
        $factory = $this->container->make(ValidationFactory::class);
83
84 3
        if (method_exists($this, 'validator')) {
85
            $validator = $this->container->call([$this, 'validator'], compact('factory'));
86
        } else {
87 3
            $validator = $this->createDefaultValidator($factory);
88
        }
89
90 3
        if (method_exists($this, 'withValidator')) {
91
            $this->withValidator($validator);
0 ignored issues
show
Documentation Bug introduced by
The method withValidator does not exist on object<Emanci\LumenRequest\FormRequest>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
92
        }
93
94 3
        return $validator;
95
    }
96
97
    /**
98
     * Create the default validator instance.
99
     *
100
     * @param \Illuminate\Contracts\Validation\Factory $factory
101
     *
102
     * @return \Illuminate\Contracts\Validation\Validator
103
     */
104 3
    protected function createDefaultValidator(ValidationFactory $factory)
105
    {
106 3
        return $factory->make(
107 3
            $this->validationData(), $this->container->call([$this, 'rules']),
108 3
            $this->messages(), $this->attributes()
109
        );
110
    }
111
112
    /**
113
     * Get data to be validated from the request.
114
     *
115
     * @return array
116
     */
117 3
    protected function validationData()
118
    {
119 3
        return $this->all();
120
    }
121
122
    /**
123
     * Handle a failed validation attempt.
124
     *
125
     * @throws \Illuminate\Validation\ValidationException
126
     *
127
     * @param \Illuminate\Contracts\Validation\Validator $validator
128
     */
129 1
    protected function failedValidation(Validator $validator)
130
    {
131 1
        throw new ValidationException($validator, $this->buildFailedValidationResponse(
132 1
            $this->formatValidationErrors($validator)
133
        ));
134
    }
135
136
    /**
137
     * Build a failed validation response.
138
     *
139
     * @param $errors
140
     *
141
     * @return JsonResponse
142
     */
143 1
    protected function buildFailedValidationResponse($errors)
144
    {
145 1
        return new JsonResponse($errors, 422);
146
    }
147
148
    /**
149
     * Format the errors from the given Validator instance.
150
     *
151
     * @param \Illuminate\Contracts\Validation\Validator $validator
152
     *
153
     * @return array
154
     */
155 1
    protected function formatValidationErrors(Validator $validator)
156
    {
157 1
        return $validator->errors()->getMessages();
158
    }
159
}
160