1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Term; |
4
|
|
|
|
5
|
|
|
use Silk\Query\Builder as BaseBuilder; |
6
|
|
|
use Silk\Exception\WP_ErrorException; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
class QueryBuilder extends BaseBuilder |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Query arguments |
13
|
|
|
* @var Collection |
14
|
|
|
*/ |
15
|
|
|
protected $query; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Taxonomy Identifier |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $taxonomy; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* QueryBuilder Constructor. |
25
|
|
|
* |
26
|
|
|
* @param array $args |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $args = []) |
29
|
|
|
{ |
30
|
|
|
$this->query = new Collection($args); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new instance. |
35
|
|
|
* |
36
|
|
|
* @return static |
37
|
|
|
*/ |
38
|
|
|
public static function make() |
39
|
|
|
{ |
40
|
|
|
return new static; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Restrict the query to terms of the provided Taxonomy. |
45
|
|
|
* |
46
|
|
|
* @param string $taxonomy |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function forTaxonomy($taxonomy) |
51
|
|
|
{ |
52
|
|
|
$this->taxonomy = $taxonomy; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get all terms. |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function all() |
63
|
|
|
{ |
64
|
|
|
return $this->includeEmpty() |
65
|
|
|
->limit('all'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Include terms that have no related objects in the results. |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function includeEmpty() |
74
|
|
|
{ |
75
|
|
|
return $this->set('hide_empty', false); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Limit the maximum number of results returned. |
80
|
|
|
* |
81
|
|
|
* @param int $max_results Maximum number to return. 0 or 'all' for unlimited. |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function limit($max_results) |
86
|
|
|
{ |
87
|
|
|
return $this->set('number', intval($max_results)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Execute the query and return the raw results. |
92
|
|
|
* |
93
|
|
|
* @throws WP_ErrorException |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
protected function query() |
98
|
|
|
{ |
99
|
|
|
if ($this->model) { |
100
|
|
|
$this->set('taxonomy', $this->model->taxonomy) |
|
|
|
|
101
|
|
|
->set('fields', 'all'); |
102
|
|
|
} elseif ($this->taxonomy) { |
103
|
|
|
$this->set('taxonomy', $this->taxonomy); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (is_wp_error($terms = get_terms($this->query->toArray()))) { |
107
|
|
|
throw new WP_ErrorException($terms); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $terms; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set an arbitrary query parameter. |
115
|
|
|
* |
116
|
|
|
* @param $parameter |
117
|
|
|
* @param $value |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function set($parameter, $value) |
122
|
|
|
{ |
123
|
|
|
$this->query->put($parameter, $value); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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@property
annotation 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.