Completed
Pull Request — master (#19)
by Evan
02:29
created

QueryBuilder::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
cc 1
eloc 3
nc 1
nop 2
rs 9.4285
1
<?php
2
3
namespace Silk\Post;
4
5
use WP_Query;
6
use Illuminate\Support\Collection;
7
use Silk\Contracts\BuildsQueries;
8
9
class QueryBuilder implements BuildsQueries
10
{
11
    /**
12
     * WP_Query instance
13
     *
14
     * @var WP_Query
15
     */
16
    protected $query;
17
18
    /**
19
     * Post Model instance
20
     *
21
     * @var Model
22
     */
23
    protected $model;
24
25
    /**
26
     * Builder constructor.
27
     *
28
     * @param WP_Query $query
29
     */
30
    public function __construct(WP_Query $query)
31
    {
32
        $this->query = $query;
33
    }
34
35
    /**
36
     * Limit the number of returned results
37
     *
38
     * @param integer $limit  The maximum number of results to return
39
     *                        use -1 for no limit
40
     *
41
     * @return $this
42
     */
43
    public function limit($limit)
44
    {
45
        $this->query->set('posts_per_page', (int) $limit);
46
47
        return $this;
48
    }
49
50
    /**
51
     * Return an unlimited number of results.
52
     *
53
     * @return $this
54
     */
55
    public function all()
56
    {
57
        return $this->limit(-1);
58
    }
59
60
    /**
61
     * Set the order for the query
62
     *
63
     * @param  string $order
64
     *
65
     * @return $this
66
     */
67
    public function order($order)
68
    {
69
        $this->query->set('order', strtoupper($order));
70
71
        return $this;
72
    }
73
74
    /**
75
     * Query by post status
76
     *
77
     * @param  string|array $status  the post status or stati to match
78
     *
79
     * @return $this
80
     */
81
    public function whereStatus($status)
82
    {
83
        $this->query->set('post_status', $status);
84
85
        return $this;
86
    }
87
88
    /**
89
     * Query by slug
90
     *
91
     * @param  string $slug  the post slug to query by
92
     *
93
     * @return $this
94
     */
95
    public function whereSlug($slug)
96
    {
97
        $this->query->set('name', $slug);
98
99
        return $this;
100
    }
101
102
    /**
103
     * Get the results as a collection
104
     *
105
     * @return Collection
106
     */
107
    public function results()
108
    {
109
        if ($this->model) {
110
            return $this->collectModels();
111
        }
112
113
        return Collection::make($this->query->get_posts());
114
    }
115
116
    /**
117
     * Get the results as a collection of post model instances
118
     *
119
     * @return Collection
120
     */
121 View Code Duplication
    protected function collectModels()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123
        $this->query->set('post_type', $this->model->post_type);
124
        $this->query->set('fields', ''); // as WP_Post objects
125
        $modelClass = get_class($this->model);
126
127
        return Collection::make($this->query->get_posts())
128
            ->map(function ($post) use ($modelClass) {
129
                return new $modelClass($post);
130
            });
131
    }
132
133
    /**
134
     * Set a query variable on the query
135
     *
136
     * @param string $var   Query variable key
137
     * @param mixed  $value Query value for key
138
     *
139
     * @return $this
140
     */
141
    public function set($var, $value)
142
    {
143
        $this->query->set($var, $value);
144
145
        return $this;
146
    }
147
148
    /**
149
     * Set the model for this query.
150
     *
151
     * @param Model $model
152
     *
153
     * @return $this
154
     */
155
    public function setModel(Model $model)
156
    {
157
        $this->model = $model;
158
159
        return $this;
160
    }
161
162
    /**
163
     * Get the model
164
     *
165
     * @return Model
166
     */
167
    public function getModel()
168
    {
169
        return $this->model;
170
    }
171
172
}
173