Completed
Push — master ( 3a2fd6...5b4baf )
by ARCANEDEV
04:23
created

PostRequest::getValidatedInputs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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