Test Failed
Push — request-validation ( 876935...295ccd )
by Alex
02:34
created

JsonApiRequest::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 1
1
<?php
2
3
namespace Huntie\JsonApi\Http\Requests;
4
5
use Huntie\JsonApi\Exceptions\HttpException;
6
use Huntie\JsonApi\Http\JsonApiResponse;
7
use Illuminate\Contracts\Validation\Validator;
8
use Illuminate\Foundation\Http\FormRequest;
9
use Illuminate\Http\Response;
10
11
abstract class JsonApiRequest extends FormRequest
12
{
13
    /**
14
     * Base validation rules for all JSON API requests.
15
     *
16
     * @var array
17
     */
18
    private $baseRules = [
19
        'fields' => 'regex:^([A-Za-z]+[A-Za-z_.\-,]*)*[A-Za-z]+$',
20
        'include' => 'regex:^([A-Za-z]+[A-Za-z_.\-,]*)*[A-Za-z]+$',
21
        'sort' => 'regex:^-?([A-Za-z]+[A-Za-z_.\-,]*)*[A-Za-z]+$',
22
        'filter' => 'array',
23
        'filter.*' => 'alpha_dash',
24
        'page' => 'array',
25
        'page.size' => 'numeric',
26
        'page.number' => 'numeric',
27
    ];
28
29
    /**
30
     * Base validation rules for the individual request type.
31
     *
32
     * @var array
33
     */
34
    protected $rules = [];
35
36
    /**
37
     * Get the validator instance for the request.
38
     *
39
     * @return Validator
40
     */
41
    protected function getValidatorInstance()
42
    {
43
        $this->beforeValidation();
44
45
        return parent::getValidatorInstance()
46
            ->setRules(array_merge($this->baseRules, $this->rules, $validator->getRules()));
0 ignored issues
show
Bug introduced by
The variable $validator does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47
    }
48
49
    /**
50
     * Perform additional logic before the request input is validated.
51
     */
52
    protected function beforeValidation()
53
    {
54
        if ($request->exists('include') && config('jsonapi.enable_included_resources') === false) {
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
55
            throw new HttpException('Inclusion of related resources is not supported');
56
        }
57
    }
58
59
    /**
60
     * Format the errors from the given Validator instance.
61
     *
62
     * {@inheritdoc}
63
     */
64
    protected function formatErrors(Validator $validator)
65
    {
66
        $errors = [];
67
68
        foreach ($validator->getMessageBag()->messages() as $field => $messages) {
69
            foreach ($messages as $message) {
70
                $errors[] = [
71
                    'source' => [
72
                        'pointer' => str_replace('.', '/', $field),
73
                    ],
74
                    'title' => 'Invalid attribute',
75
                    'detail' => str_replace('data.attributes.', '', $message),
76
                ];
77
            }
78
        }
79
80
        return compact('errors');
81
    }
82
83
    /**
84
     * Get the proper failed validation response for the request.
85
     *
86
     * @param array $errors
87
     *
88
     * @return JsonApiResponse
89
     */
90
    public function response(array $errors)
91
    {
92
        return new JsonApiResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
93
    }
94
}
95