Completed
Push — master ( 43e4d4...f56513 )
by Ben
11:09
created

ValidatesRequests::isValid()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace RealPage\JsonApi\Validation;
4
5
use Illuminate\Contracts\Validation\Factory;
6
use Neomerx\JsonApi\Exceptions\ErrorCollection;
7
use RealPage\JsonApi\Requests\Request;
8
use Neomerx\JsonApi\Document\Error;
9
10
trait ValidatesRequests
11
{
12
    /** @var ErrorCollection */
13
    protected $errors;
14
15
    /** @var Factory */
16
    protected $validatorFactory;
17
18
    public function isValid(Request $request) : bool
19
    {
20
        $this->errors = new ErrorCollection();
21
22
        /** @var \Illuminate\Contracts\Validation\Validator $validator */
23
        $validator = $this->validatorFactory->make($request->json(), $this->rules(), $this->messages());
24
        if ($validator->fails()) {
25
            // @todo put this somewhere else, this is getting messy
26
            // @see https://jsonapi.org/examples/#error-objects-basic
27
            foreach ($validator->errors()->all() as $field => $errorMessage) {
0 ignored issues
show
Bug introduced by
The method errors() does not seem to exist on object<Illuminate\Contracts\Validation\Validator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
29
                // get the pointer for an array so we can pinpoint the section
30
                // of json where the error occurred
31
                $field = '/'.str_replace('.', '/', $field).'/';
32
33
                $this->errors->add(new Error(
34
                    null,
35
                    null,
36
                    422,
37
                    null,
38
                    'Invalid Attribute',
39
                    $errorMessage,
40
                    [
41
                        'pointer' => $field,
42
                    ]
43
                ));
44
            }
45
        }
46
    }
47
48
    public function rules(array $rules = null) : array
49
    {
50
        $rules = $rules ?? [];
51
52
        return array_merge([
53
            'data'      => 'required',
54
            'data.type' => 'required|in:'.$this->type(),
55
        ], $rules);
56
    }
57
58
    public function messages(array $messages = null) : array
59
    {
60
        $messages = $messages ?? [];
61
62
        return array_merge([
63
            'data.required'      => 'Data is required in a valid json api format.',
64
            'data.type.required' => 'A valid resource type must be provided.',
65
            'data.type.in'       => 'The resource type provided does not match the expected type of "'.$this->type().'".',
66
        ], $messages);
67
    }
68
69
    public function errors() : ErrorCollection
70
    {
71
        return $this->errors;
72
    }
73
74
    /**
75
     * The type of entity this request is for.
76
     */
77
    abstract public function type() : string;
78
79
    public function setValidatorFactory(Factory $factory)
80
    {
81
        $this->validatorFactory = $factory;
82
    }
83
}
84