|
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
|
|
|
* |
|
15
|
|
|
* @var WP_Query |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $query; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Post Model instance |
|
21
|
|
|
* |
|
22
|
|
|
* @var Model |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $model; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Builder constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param WP_Query $query |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(WP_Query $query) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->query = $query; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Limit the number of returned results |
|
38
|
|
|
* |
|
39
|
|
|
* @param integer $limit The maximum number of results to return |
|
40
|
|
|
* use -1 for no limit |
|
41
|
|
|
* |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function limit($limit) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->query->set('posts_per_page', (int) $limit); |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set the order for the query |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $order |
|
55
|
|
|
* |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
|
|
public function order($order) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->query->set('order', strtoupper($order)); |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Query by post status |
|
67
|
|
|
* |
|
68
|
|
|
* @param string|array $status the post status or stati to match |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function whereStatus($status) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->query->set('post_status', $status); |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the results as a collection |
|
81
|
|
|
* |
|
82
|
|
|
* @return Collection |
|
83
|
|
|
*/ |
|
84
|
|
|
public function results() |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->model) { |
|
87
|
|
|
return $this->collectModels(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return collect($this->query->get_posts()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the results as a collection of post model instances |
|
95
|
|
|
* |
|
96
|
|
|
* @return Collection |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function collectModels() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->query->set('post_type', $this->model->post_type); |
|
|
|
|
|
|
101
|
|
|
$this->query->set('fields', ''); // as WP_Post objects |
|
102
|
|
|
$modelClass = get_class($this->model); |
|
103
|
|
|
|
|
104
|
|
|
return collect($this->query->get_posts()) |
|
105
|
|
|
->map(function ($post) use ($modelClass) { |
|
106
|
|
|
return new $modelClass($post); |
|
107
|
|
|
}); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set a query variable on the query |
|
112
|
|
|
* |
|
113
|
|
|
* @param [type] $var [description] |
|
|
|
|
|
|
114
|
|
|
* @param [type] $value [description] |
|
|
|
|
|
|
115
|
|
|
* |
|
116
|
|
|
* @return $this |
|
117
|
|
|
*/ |
|
118
|
|
|
public function set($var, $value) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->query->set($var, $value); |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set the model for this query. |
|
127
|
|
|
* |
|
128
|
|
|
* @param Model $model |
|
129
|
|
|
* |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
|
|
public function setModel(Model $model) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->model = $model; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the model |
|
141
|
|
|
* |
|
142
|
|
|
* @return Model |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getModel() |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->model; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.