Completed
Push — master ( 22573e...f9ef9a )
by Evan
02:49
created

Builder::setModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 6
rs 9.4285
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
     * Return an unlimited number of results.
53
     * 
54
     * @return $this
55
     */
56
    public function all()
57
    {
58
        return $this->limit(-1);
59
    }
60
61
    /**
62
     * Set the order for the query
63
     *
64
     * @param  string $order
65
     *
66
     * @return $this
67
     */
68
    public function order($order)
69
    {
70
        $this->query->set('order', strtoupper($order));
71
72
        return $this;
73
    }
74
75
    /**
76
     * Query by post status
77
     *
78
     * @param  string|array $status  the post status or stati to match
79
     *
80
     * @return $this
81
     */
82
    public function whereStatus($status)
83
    {
84
        $this->query->set('post_status', $status);
85
86
        return $this;
87
    }
88
89
    /**
90
     * Query by slug
91
     *
92
     * @param  string $slug  the post slug to query by
93
     *
94
     * @return $this
95
     */
96
    public function whereSlug($slug)
97
    {
98
        $this->query->set('name', $slug);
99
100
        return $this;
101
    }
102
103
    /**
104
     * Get the results as a collection
105
     *
106
     * @return Collection
107
     */
108
    public function results()
109
    {
110
        if ($this->model) {
111
            return $this->collectModels();
112
        }
113
114
        return collect($this->query->get_posts());
115
    }
116
117
    /**
118
     * Get the results as a collection of post model instances
119
     *
120
     * @return Collection
121
     */
122
    protected function collectModels()
123
    {
124
        $this->query->set('post_type', $this->model->post_type);
0 ignored issues
show
Documentation introduced by
The property post_type does not exist on object<Silk\Post\Model>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
125
        $this->query->set('fields', ''); // as WP_Post objects
126
        $modelClass = get_class($this->model);
127
128
        return collect($this->query->get_posts())
129
            ->map(function ($post) use ($modelClass) {
130
                return new $modelClass($post);
131
            });
132
    }
133
134
    /**
135
     * Set a query variable on the query
136
     *
137
     * @param [type] $var   [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

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.

Loading history...
138
     * @param [type] $value [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

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.

Loading history...
139
     *
140
     * @return $this
141
     */
142
    public function set($var, $value)
143
    {
144
        $this->query->set($var, $value);
145
146
        return $this;
147
    }
148
149
    /**
150
     * Set the model for this query.
151
     *
152
     * @param Model $model
153
     *
154
     * @return $this
155
     */
156
    public function setModel(Model $model)
157
    {
158
        $this->model = $model;
159
160
        return $this;
161
    }
162
163
    /**
164
     * Get the model
165
     *
166
     * @return Model
167
     */
168
    public function getModel()
169
    {
170
        return $this->model;
171
    }
172
173
}
174