1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Query; |
4
|
|
|
|
5
|
|
|
use WP_Query; |
6
|
|
|
use Silk\Post\Post; |
7
|
|
|
use Silk\Post\Model; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
class Builder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* WP_Query instance |
14
|
|
|
* @var WP_Query |
15
|
|
|
*/ |
16
|
|
|
protected $query; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Post Model instance |
20
|
|
|
* @var Model |
21
|
|
|
*/ |
22
|
|
|
protected $model; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Builder constructor. |
26
|
|
|
* |
27
|
|
|
* @param WP_Query $query |
28
|
|
|
*/ |
29
|
|
|
public function __construct(WP_Query $query) |
30
|
|
|
{ |
31
|
|
|
$this->query = $query; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Limit the number of returned results |
36
|
|
|
* |
37
|
|
|
* @param integer $limit The maximum number of results to return |
38
|
|
|
* use -1 for no limit |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function limit($limit) |
43
|
|
|
{ |
44
|
|
|
$this->query->set('posts_per_page', (int) $limit); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* [order description] |
51
|
|
|
* @param [type] $order [description] |
|
|
|
|
52
|
|
|
* @return [type] [description] |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
public function order($order) |
55
|
|
|
{ |
56
|
|
|
$this->query->set('order', strtoupper($order)); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Query by post status |
63
|
|
|
* |
64
|
|
|
* @param string|array $status the post status or stati to match |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function whereStatus($status) |
69
|
|
|
{ |
70
|
|
|
$this->query->set('post_status', $status); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the results as a collection of post model instances |
77
|
|
|
* |
78
|
|
|
* @return Collection |
79
|
|
|
*/ |
80
|
|
|
public function results() |
81
|
|
|
{ |
82
|
|
|
if ($this->model) { |
83
|
|
|
$this->query->set('post_type', $this->model->post_type); |
|
|
|
|
84
|
|
|
$this->query->set('fields', ''); // return objects |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$modelClass = $this->model ? get_class($this->model) : Post::class; |
88
|
|
|
|
89
|
|
|
return Collection::make($this->query->get_posts()) |
90
|
|
|
->map(function ($post) use ($modelClass) { |
91
|
|
|
return new $modelClass($post); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set a query variable on the query |
97
|
|
|
* |
98
|
|
|
* @param [type] $var [description] |
|
|
|
|
99
|
|
|
* @param [type] $value [description] |
|
|
|
|
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function set($var, $value) |
104
|
|
|
{ |
105
|
|
|
$this->query->set($var, $value); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the model for this query. |
112
|
|
|
* |
113
|
|
|
* @param Model $model |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function setModel(Model $model) |
118
|
|
|
{ |
119
|
|
|
$this->model = $model; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the model |
126
|
|
|
* |
127
|
|
|
* @return Model |
128
|
|
|
*/ |
129
|
|
|
public function getModel() |
130
|
|
|
{ |
131
|
|
|
return $this->model; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.