Completed
Branch master (bcc3a6)
by Evan
04:36
created

QueryBuilder::includeEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
cc 1
eloc 3
nc 1
nop 0
rs 9.4285
1
<?php
2
3
namespace Silk\Term;
4
5
use WP_Term;
6
use Silk\Contracts\BuildsQueries;
7
use Silk\Exception\WP_ErrorException;
8
use Illuminate\Support\Collection;
9
10
class QueryBuilder implements BuildsQueries
11
{
12
    /**
13
     * The term model
14
     * @var Model
15
     */
16
    protected $model;
17
18
    /**
19
     * Collection of arguments
20
     * @var Collection
21
     */
22
    protected $args;
23
24
    /**
25
     * Taxonomy Identifier
26
     * @var string
27
     */
28
    protected $taxonomy;
29
30
    /**
31
     * TermQueryBuilder Constructor.
32
     *
33
     * @param array $args
34
     */
35
    public function __construct(array $args = [])
36
    {
37
        $this->args = Collection::make($args);
38
    }
39
40
    /**
41
     * Restrict the query to terms of the provided Taxonomy.
42
     *
43
     * @param  string $taxonomy
44
     *
45
     * @return $this
46
     */
47
    public function forTaxonomy($taxonomy)
48
    {
49
        $this->taxonomy = $taxonomy;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Include terms that have no related objects in the results.
56
     *
57
     * @return $this
58
     */
59
    public function includeEmpty()
60
    {
61
        $this->args->put('hide_empty', false);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Limit the maximum number of results returned.
68
     *
69
     * @param  int $max_results  Maximum number to return. 0 or 'all' for unlimited.
70
     *
71
     * @return $this
72
     */
73
    public function limit($max_results)
74
    {
75
        $this->args->put('number', intval($max_results));
76
77
        return $this;
78
    }
79
80
    /**
81
     * Get the query results.
82
     *
83
     * @throws WP_ErrorException
84
     *
85
     * @return Collection
86
     */
87
    public function results()
88
    {
89
        if ($this->model) {
90
            return $this->collectModels();
91
        }
92
93
        if ($this->taxonomy) {
94
            $this->args->put('taxonomy', $this->taxonomy);
95
        }
96
97
        return Collection::make($this->fetchTerms());
98
    }
99
100
    /**
101
     * Get the results as a collection of models.
102
     *
103
     * @return Collection
104
     */
105 View Code Duplication
    protected function collectModels()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $this->args->put('taxonomy', $this->model->taxonomy);
108
        $this->args->put('fields', 'all');
109
110
        $modelClass = get_class($this->model);
111
112
        return Collection::make($this->fetchTerms())
113
            ->map(function (WP_Term $term) use ($modelClass) {
114
                return new $modelClass($term);
115
            });
116
    }
117
118
    /**
119
     * Set the model for this query.
120
     *
121
     * @param mixed $model
122
     *
123
     * @return $this
124
     */
125
    public function setModel($model)
126
    {
127
        $this->model = $model;
128
129
        return $this;
130
    }
131
132
    /**
133
     * Get the model.
134
     *
135
     * @return mixed Model
136
     */
137
    public function getModel()
138
    {
139
        return $this->model;
140
    }
141
142
    /**
143
     * Perform the term query and return the results.
144
     *
145
     * @throws WP_ErrorException
146
     *
147
     * @return array
148
     */
149
    protected function fetchTerms()
150
    {
151
        if (is_wp_error($terms = get_terms($this->args->toArray()))) {
152
            throw new WP_ErrorException($terms);
153
        }
154
155
        return $terms;
156
    }
157
}
158