Completed
Push — master ( 29d121...b985cb )
by Evan
02:17
created

Builder::collectModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
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
     * 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);
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...
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]
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...
114
     * @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...
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