Completed
Push — master ( 1ddffe...f4872f )
by ARCANEDEV
03:37
created

PostRequest::getValidatedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 2
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
            'title'        => ['required', 'string', 'max:255'],
32
            'excerpt'      => ['required', 'string', 'max:200'],
33
            'content'      => ['required', 'string'],
34
            'category'     => static::getCategoryRule(),
35
            'tags'         => static::getTagsRule(),
36
            'published_at' => ['required', 'date_format:Y-m-d'],
37
            'status'       => static::getPostStatusRule(),
38
        ];
39
    }
40
41
    /* -----------------------------------------------------------------
42
     |  Other Methods
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Get the validated inputs.
48
     *
49
     * @return array
50
     */
51
    public function getValidatedData()
52
    {
53
        return array_merge([
54
            'author_id'   => $this->user()->getAuthIdentifier(),
55
            'category_id' => $this->get('category')
56
        ], $this->only([
57
            // POST inputs
58
            'title', 'excerpt', 'content', 'tags', 'published_at', 'status',
59
60
            // SEO inputs
61
            'seo_title', 'seo_description', 'seo_keywords',
62
        ]));
63
    }
64
65
    /**
66
     * Get the slug rule.
67
     *
68
     * @param  string  $column
69
     *
70
     * @return \Illuminate\Validation\Rules\Unique
71
     */
72
    protected static function getSlugRule($column = 'slug')
73
    {
74
        $prefix = config('arcanesoft.blog.database.prefix', 'blog_');
75
76
        return Rule::unique("{$prefix}posts", $column);
77
    }
78
79
    /**
80
     * Get the category rule.
81
     *
82
     * @return array
83
     */
84
    protected static function getCategoryRule()
85
    {
86
        return ['required', 'integer', 'in:'.Category::getSelectOptions(false)->keys()->implode(',')];
87
    }
88
89
    /**
90
     * Get the tags rule.
91
     *
92
     * @return array
93
     */
94
    protected static function getTagsRule()
95
    {
96
        return ['required', 'array', 'min:1', 'in:'.Tag::getSelectOptions()->keys()->implode(',')];
97
    }
98
99
    /**
100
     * Get the post status rule.
101
     *
102
     * @return array
103
     */
104
    protected static function getPostStatusRule()
105
    {
106
        return ['required', 'string', 'in:'.Post::getStatuses()->keys()->implode(',')];
107
    }
108
}
109