ConfigRequest::rules()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 19
rs 9.8666
1
<?php
2
3
namespace App\Http\Requests\Admin;
4
5
use App\Rules\JsonStr;
6
use Illuminate\Foundation\Http\FormRequest;
7
use App\Model\Admin\Config;
8
use Illuminate\Validation\Rule;
9
10
class ConfigRequest extends FormRequest
11
{
12
    /**
13
     * Determine if the user is authorized to make this request.
14
     *
15
     * @return bool
16
     */
17
    public function authorize()
18
    {
19
        return true;
20
    }
21
22
    /**
23
     * Get the validation rules that apply to the request.
24
     *
25
     * @return array
26
     */
27
    public function rules()
28
    {
29
        $rules = [
30
            'name' => 'required|max:50',
31
            'key' => ['required', 'max:100', 'regex:/^[\w]+$/'],
32
            'value' => 'max:2048',
33
            'type' => [
34
                Rule::in(array_keys(Config::$types)),
35
            ],
36
        ];
37
        if ((int) request()->input('type') === Config::TYPE_JSON) {
38
            $rules['value'] = [
39
                'required',
40
                'max:2048',
41
                new JsonStr()
42
            ];
43
        }
44
45
        return $rules;
46
    }
47
48
    /**
49
     * Get custom messages for validator errors.
50
     *
51
     * @return array
52
     */
53
    public function messages()
54
    {
55
        return [
56
            'name.required' => '名称不能为空',
57
            'name.max' => '名称长度不能大于50',
58
            'key.regex' => '标识符非法'
59
        ];
60
    }
61
}
62