QueryBuilder::make()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silk\Post;
4
5
use WP_Query;
6
use Silk\Query\Builder as BaseBuilder;
7
8
/**
9
 * @property Model $model
10
 */
11
class QueryBuilder extends BaseBuilder
12
{
13
    /**
14
     * WP_Query instance
15
     *
16
     * @var WP_Query
17
     */
18
    protected $query;
19
20
    /**
21
     * Builder constructor.
22
     *
23
     * @param WP_Query $query
24
     */
25
    public function __construct(WP_Query $query = null)
26
    {
27
        if (! $query) {
28
            $query = new WP_Query();
29
        }
30
31
        $this->query = $query;
32
    }
33
34
    /**
35
     * Create a new instance.
36
     *
37
     * @param WP_Query $query
38
     *
39
     * @return static
40
     */
41
    public static function make(WP_Query $query = null)
42
    {
43
        return new static($query);
44
    }
45
46
    /**
47
     * Limit the number of returned results
48
     *
49
     * @param integer $limit  The maximum number of results to return
50
     *                        use -1 for no limit
51
     *
52
     * @return $this
53
     */
54
    public function limit($limit)
55
    {
56
        return $this->set('posts_per_page', (int) $limit);
57
    }
58
59
    /**
60
     * Return an unlimited number of results.
61
     *
62
     * @return $this
63
     */
64
    public function all()
65
    {
66
        return $this->limit(-1);
67
    }
68
69
    /**
70
     * Set the order for the query
71
     *
72
     * @param  string $order
73
     *
74
     * @return $this
75
     */
76
    public function order($order)
77
    {
78
        return $this->set('order', strtoupper($order));
79
    }
80
81
    /**
82
     * Query by post status
83
     *
84
     * @param  string|array $status  the post status or stati to match
85
     *
86
     * @return $this
87
     */
88
    public function whereStatus($status)
89
    {
90
        return $this->set('post_status', $status);
91
    }
92
93
    /**
94
     * Query by slug
95
     *
96
     * @param  string $slug  the post slug to query by
97
     *
98
     * @return $this
99
     */
100
    public function whereSlug($slug)
101
    {
102
        return $this->set('name', $slug);
103
    }
104
105
    /**
106
     * Set a query variable on the query
107
     *
108
     * @param string $var   Query variable key
109
     * @param mixed  $value Query value for key
110
     *
111
     * @return $this
112
     */
113
    public function set($var, $value)
114
    {
115
        $this->query->set($var, $value);
116
117
        return $this;
118
    }
119
120
    /**
121
     * Execute the query and return the raw results.
122
     *
123
     * @return array
124
     */
125
    protected function query()
126
    {
127
        if ($this->model) {
128
            $this->set('post_type', $this->model->post_type)
129
                 ->set('fields', ''); // as WP_Post objects
130
        }
131
132
        return $this->query->get_posts();
133
    }
134
}
135