Completed
Pull Request — master (#138)
by
unknown
02:51
created

IssueRequest::getValidatorInstance()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 4
nop 0
1
<?php
2
/**
3
 * GitScrum v0.1.
4
 *
5
 * @author  Renato Marinho <[email protected]>
6
 * @license http://opensource.org/licenses/GPL-3.0 GPLv3
7
 */
8
9
namespace GitScrum\Http\Requests;
10
11
use Illuminate\Foundation\Http\FormRequest;
12
use GitScrum\Models\Sprint;
13
14
class IssueRequest extends FormRequest
15
{
16
    /**
17
     * Determine if the user is authorized to make this request.
18
     *
19
     * @return bool
20
     */
21
    public function authorize()
22
    {
23
        return true;
24
    }
25
26
    /**
27
     * Get the validation rules that apply to the request.
28
     *
29
     * @return array
30
     */
31
    public function rules()
32
    {
33
        return [
34
            'title' => 'required|min:2|max:255',
35
        ];
36
    }
37
    /**
38
     * Get the error messages for the defined validation rules.
39
     *
40
     * @return array
41
     */
42
    public function messages()
43
    {
44
        return [
45
            'title.required' => trans('Issue cannot be blank'),
46
            'title.min' => trans('Issue must be at least 2 characters'),
47
            'title.max' => trans('Issue must be between 2 and 255 characters'),
48
        ];
49
    }
50
51
    protected function getValidatorInstance()
52
    {
53
54
        $data = $this->all();
55
        if (isset($data['slug_sprint']) && !empty($data['slug_sprint'])) {
56
            $data['sprint_id'] = Sprint::slug($data['slug_sprint'])->first()->id;
57
            unset($data['slug_sprint']);
58
        }
59
60
        if (isset($data['sprint_id']) && empty($data['sprint_id'])) {
61
            $data['sprint_id'] = null;
62
        }
63
64
        $this->getInputSource()->replace($data);
65
66
        return parent::getValidatorInstance();
67
    }
68
}
69