-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathQueryBuilder.php
More file actions
207 lines (153 loc) · 4.77 KB
/
QueryBuilder.php
File metadata and controls
207 lines (153 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace Rareloop\Lumberjack;
use Rareloop\Lumberjack\Contracts\QueryBuilder as QueryBuilderContract;
use Rareloop\Lumberjack\Exceptions\InvalidMetaRelationshipException;
use Rareloop\Lumberjack\Post;
use Spatie\Macroable\Macroable;
use Tightenco\Collect\Support\Collection;
use Timber\PostQuery;
use Timber\Timber;
class QueryBuilder implements QueryBuilderContract
{
use Macroable;
protected $postClass = Post::class;
private $params = [];
// Order Directions
const DESC = 'DESC';
const ASC = 'ASC';
// Field Types
const NUMERIC = 'numeric';
// Logical Operators
const OR = 'OR';
const AND = 'AND';
public function getParameters(): array
{
return $this->params;
}
public function wherePostType($postType): QueryBuilderContract
{
$this->params['post_type'] = $postType;
return $this;
}
public function limit($limit): QueryBuilderContract
{
$this->params['posts_per_page'] = $limit;
return $this;
}
public function offset($offset): QueryBuilderContract
{
$this->params['offset'] = $offset;
return $this;
}
public function orderBy($orderBy, string $order = QueryBuilder::ASC): QueryBuilderContract
{
$order = strtoupper($order);
$this->params['orderby'] = $orderBy;
$this->params['order'] = $order;
return $this;
}
public function orderByMeta($metaKey, string $order = QueryBuilder::ASC, string $type = null): QueryBuilderContract
{
$order = strtoupper($order);
$this->params['orderby'] = ($type === QueryBuilder::NUMERIC ? true : false) ? 'meta_value_num' : 'meta_value';
$this->params['order'] = $order;
$this->params['meta_key'] = $metaKey;
return $this;
}
public function whereIdIn(array $ids): QueryBuilderContract
{
$this->params['post__in'] = $ids;
return $this;
}
public function whereIdNotIn(array $ids): QueryBuilderContract
{
$this->params['post__not_in'] = $ids;
return $this;
}
public function whereStatus(): QueryBuilderContract
{
$args = func_get_args();
if (count($args) === 0) {
throw new \InvalidArgumentException('`whereStatus` must be called with at least one argument');
}
$this->params['post_status'] = count($args) > 1 ? $args : $args[0];
return $this;
}
protected function initialiseMetaQuery()
{
$this->params['meta_query'] = $this->params['meta_query'] ?? [];
}
public function whereMeta($key, $value, $compare = '=', $type = null): QueryBuilderContract
{
$meta = [
'key' => $key,
'value' => $value,
'compare' => $compare,
];
if ($type) {
$meta['type'] = $type;
}
$this->initialiseMetaQuery();
$this->params['meta_query'][] = $meta;
return $this;
}
public function whereMetaRelationshipIs(string $relation): QueryBuilderContract
{
$relation = strtoupper($relation);
if (!in_array($relation, [QueryBuilder::AND, QueryBuilder::OR])) {
throw new InvalidMetaRelationshipException(
'`whereMetaRelationshipIs` must be passed QueryBuilder::AND or QueryBuilder::OR'
);
}
$this->initialiseMetaQuery();
$this->params['meta_query']['relation'] = $relation;
return $this;
}
public function as($postClass): QueryBuilderContract
{
$this->postClass = $postClass;
return $this;
}
public function get(): Collection
{
$posts = Timber::get_posts($this->getParameters(), $this->postClass);
if (!is_array($posts)) {
$posts = [];
}
return collect($posts);
}
public function paginate($perPage = 10, $page = 1): PostQuery
{
global $paged;
if (isset($page)) {
$paged = $page;
}
if (!isset($paged) || !$paged) {
$paged = 1;
}
$this->limit($perPage);
$this->params['paged'] = $paged;
return new PostQuery($this->getParameters(), $this->postClass);
}
/**
* Get the first Post that matches the current query. If no Post matches then return `null`.
*
* @return \Rareloop\Lumberjack\Post|null
*/
public function first(): ?Post
{
$params = array_merge($this->getParameters(), [
'limit' => 1,
]);
$posts = Timber::get_posts($params, $this->postClass);
if (!is_array($posts)) {
return null;
}
return collect($posts)->first();
}
public function clone(): QueryBuilderContract
{
$clone = clone $this;
return $clone;
}
}