Completed
Push — master ( a06757...18d394 )
by ARCANEDEV
04:53
created

PostRequest::prepareForValidation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php namespace Arcanesoft\Blog\Http\Requests\Admin\Posts;
2
3
use Arcanesoft\Blog\Http\Requests\Admin\FormRequest;
4
use Arcanesoft\Blog\Models\Category;
5
use Arcanesoft\Blog\Models\Post;
6
use Arcanesoft\Blog\Models\Tag;
7
use Illuminate\Validation\Rule;
8
9
/**
10
 * Class     PostRequest
11
 *
12
 * @package  Arcanesoft\Blog\Http\Requests\Admin\Posts
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class PostRequest extends FormRequest
16
{
17
    /* -----------------------------------------------------------------
18
     |  Main Methods
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Get the validation rules that apply to the request.
24
     *
25
     * @return array
26
     */
27
    public function rules()
28
    {
29
        // TODO: Adding seo rules
30
        return [
31
            'locale'       => $this->getLocaleRule(),
32
            'title'        => ['required', 'string', 'max:255'],
33
            'excerpt'      => ['required', 'string', 'max:200'],
34
            'thumbnail'    => ['nullable', 'string', 'url'],
35
            'content'      => ['required', 'string'],
36
            'category'     => static::getCategoryRule(),
37
            'tags'         => static::getTagsRule(),
38
            'published_at' => ['required', 'date_format:Y-m-d'],
39
            'status'       => static::getPostStatusRule(),
40
        ];
41
    }
42
43
    /* -----------------------------------------------------------------
44
     |  Other Methods
45
     | -----------------------------------------------------------------
46
     */
47
48
    /**
49
     * Prepare the data for validation.
50
     *
51
     * @return void
52
     */
53
    protected function prepareForValidation()
54
    {
55
        parent::prepareForValidation();
56
57
        if ( ! $this->isTranslatable()) {
58
            $this->merge(['locale' => config('app.locale')]);
59
        }
60
    }
61
62
    /**
63
     * Get the validated inputs.
64
     *
65
     * @return array
66
     */
67
    public function getValidatedData()
68
    {
69
        return array_merge([
70
            'author_id'   => $this->user()->getAuthIdentifier(),
71
            'category_id' => $this->get('category')
72
        ], $this->only([
73
            // POST inputs
74
            'locale', 'title', 'excerpt', 'thumbnail', 'content', 'tags', 'published_at', 'status',
75
76
            // SEO inputs
77
            'seo_title', 'seo_description', 'seo_keywords',
78
        ]));
79
    }
80
81
    /**
82
     * Get the slug rule.
83
     *
84
     * @param  string  $column
85
     *
86
     * @return \Illuminate\Validation\Rules\Unique
87
     */
88
    protected static function getSlugRule($column = 'slug')
89
    {
90
        $prefix = config('arcanesoft.blog.database.prefix', 'blog_');
91
92
        return Rule::unique("{$prefix}posts", $column);
93
    }
94
95
    /**
96
     * Get the category rule.
97
     *
98
     * @return array
99
     */
100
    protected static function getCategoryRule()
101
    {
102
        return ['required', 'integer', 'in:'.Category::getSelectOptions(false)->keys()->implode(',')];
103
    }
104
105
    /**
106
     * Get the tags rule.
107
     *
108
     * @return array
109
     */
110
    protected static function getTagsRule()
111
    {
112
        return ['required', 'array', 'min:1', 'in:'.Tag::getSelectOptions()->keys()->implode(',')];
113
    }
114
115
    /**
116
     * Get the post status rule.
117
     *
118
     * @return array
119
     */
120
    protected static function getPostStatusRule()
121
    {
122
        return ['required', 'string', 'in:'.Post::getStatuses()->keys()->implode(',')];
123
    }
124
125
    /**
126
     * Get the post local rule.
127
     *
128
     * @return array
129
     */
130
    protected function getLocaleRule()
131
    {
132
        return ['required', 'string', 'in:'.implode(',', $this->getSupportedLocales())];
133
    }
134
}
135