ValidationService::attributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PerfectOblivion\Valid\ValidationService;
4
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Contracts\Validation\Validator;
7
use Illuminate\Validation\ValidationException;
8
use PerfectOblivion\Valid\Traits\SanitizesInput;
9
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
10
use PerfectOblivion\Valid\ValidationService\Concerns\HandlesRedirects;
11
use PerfectOblivion\Valid\Traits\PreparesCustomRulesForServiceValidator;
12
use PerfectOblivion\Valid\ValidationService\Concerns\InteractsWithValidationData;
13
use PerfectOblivion\Valid\Contracts\ValidationService\ValidationService as Contract;
14
15
class ValidationService implements Contract
16
{
17
    use HandlesRedirects, InteractsWithValidationData, SanitizesInput, PreparesCustomRulesForServiceValidator;
18
19
    /**
20
     * The container instance.
21
     *
22
     * @var \Illuminate\Contracts\Container\Container
23
     */
24
    protected $container;
25
26
    /**
27
     * The key to be used for the view error bag.
28
     *
29
     * @var string
30
     */
31
    protected $errorBag = 'default';
32
33
    /**
34
     * The data to be validated.
35
     *
36
     * @var array
37
     */
38
    protected $data;
39
40
    /**
41
     * Validate the class instance.
42
     *
43
     * @param  array  $data
44
     *
45
     * @throws \Illuminate\Validation\ValidationException
46
     *
47
     * @return array
48
     */
49
    public function validate(array $data)
50
    {
51
        $this->data = $data;
52
        $this->prepareCustomRules();
53
54
        $this->prepareForValidation();
55
56
        $validator = $this->getValidator();
57
58
        if (! $validator->passes()) {
59
            $this->failedValidation($validator);
60
        }
61
62
        return $this->validated();
63
    }
64
65
    /**
66
     * Get the validator for the request.
67
     *
68
     * @return \Illuminate\Contracts\Validation\Validator
69
     */
70
    protected function getValidator()
71
    {
72
        $factory = $this->container->make(ValidationFactory::class);
73
        $validator = $this->container->call([$this, 'validator'], compact('factory'));
74
75
        if (method_exists($this, 'withValidator')) {
76
            $this->withValidator($validator);
0 ignored issues
show
Bug introduced by
The method withValidator() does not exist on PerfectOblivion\Valid\Va...rvice\ValidationService. Did you maybe mean validator()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
        }
78
79
        return $validator;
80
    }
81
82
    /**
83
     * Handle a failed validation attempt.
84
     *
85
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
86
     *
87
     * @throws \Illuminate\Validation\ValidationException
88
     */
89
    protected function failedValidation(Validator $validator)
90
    {
91
        throw (new ValidationException($validator))
92
            ->errorBag($this->errorBag)
93
            ->redirectTo($this->getRedirectUrl());
94
    }
95
96
    /**
97
     * Create the default validator instance.
98
     *
99
     * @param  \Illuminate\Contracts\Validation\Factory  $factory
100
     *
101
     * @return \Illuminate\Contracts\Validation\Validator
102
     */
103
    public function validator(ValidationFactory $factory)
104
    {
105
        return $factory->make(
106
            $this->validationData() ?? [],
107
            $this->container->call([$this, 'rules']),
108
            $this->messages(),
109
            $this->attributes()
110
        );
111
    }
112
113
    /**
114
     * Run any needed logic prior to validation.
115
     */
116
    protected function prepareForValidation()
117
    {
118
        $this->sanitizeData();
119
120
        $this->beforeValidation();
121
122
        return $this->validationData();
123
    }
124
125
    /**
126
     * Run any logic needed prior to validation running.
127
     */
128
    protected function beforeValidation()
129
    {
130
    }
131
132
    /**
133
     * Get data to be validated from the request.
134
     *
135
     * @return array
136
     */
137
    protected function validationData()
138
    {
139
        return $this->data;
140
    }
141
142
    /**
143
     * Transform the data if necessary.
144
     *
145
     * @param  array  $data
146
     *
147
     * @return array
148
     */
149
    protected function transform($data)
150
    {
151
        return $data;
152
    }
153
154
    /**
155
     * Set the container implementation.
156
     *
157
     * @param  \Illuminate\Contracts\Container\Container  $container
158
     *
159
     * @return $this
160
     */
161
    public function setContainer(Container $container)
162
    {
163
        $this->container = $container;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Get custom messages for validator errors.
170
     *
171
     * @return array
172
     */
173
    public function messages()
174
    {
175
        return [];
176
    }
177
178
    /**
179
     * Get custom attributes for validator errors.
180
     *
181
     * @return array
182
     */
183
    public function attributes()
184
    {
185
        return [];
186
    }
187
}
188