Completed
Push — master ( 8ffcad...85e41d )
by Evan
02:47
created

Builder::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
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
     * @var WP_Query
15
     */
16
    protected $query;
17
18
    /**
19
     * Post Model instance
20
     * @var Model
21
     */
22
    protected $model;
23
24
    /**
25
     * Builder constructor.
26
     *
27
     * @param WP_Query $query
28
     */
29
    public function __construct(WP_Query $query)
30
    {
31
        $this->query = $query;
32
    }
33
34
    /**
35
     * Limit the number of returned results
36
     *
37
     * @param integer $limit  The maximum number of results to return
38
     *                        use -1 for no limit
39
     *
40
     * @return $this
41
     */
42
    public function limit($limit)
43
    {
44
        $this->query->set('posts_per_page', (int) $limit);
45
46
        return $this;
47
    }
48
49
    /**
50
     * [order description]
51
     * @param  [type] $order [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...
52
     * @return [type]        [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...
53
     */
54
    public function order($order)
55
    {
56
        $this->query->set('order', strtoupper($order));
57
58
        return $this;
59
    }
60
61
    /**
62
     * Query by post status
63
     *
64
     * @param  string|array $status  the post status or stati to match
65
     * 
66
     * @return $this
67
     */
68
    public function whereStatus($status)
69
    {
70
        $this->query->set('post_status', $status);
71
72
        return $this;
73
    }
74
75
    /**
76
     * Get the results as a collection of post model instances
77
     *
78
     * @return Collection
79
     */
80
    public function results()
81
    {
82
        if ($this->model) {
83
            $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...
84
            $this->query->set('fields', ''); // return objects
85
        }
86
87
        $modelClass = $this->model ? get_class($this->model) : Post::class;
88
89
        return Collection::make($this->query->get_posts())
90
            ->map(function ($post) use ($modelClass) {
91
                return new $modelClass($post);
92
            });
93
    }
94
95
    /**
96
     * Set a query variable on the query
97
     *
98
     * @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...
99
     * @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...
100
     *
101
     * @return $this
102
     */
103
    public function set($var, $value)
104
    {
105
        $this->query->set($var, $value);
106
107
        return $this;
108
    }
109
110
    /**
111
     * Set the model for this query.
112
     *
113
     * @param Model $model
114
     *
115
     * @return $this
116
     */
117
    public function setModel(Model $model)
118
    {
119
        $this->model = $model;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Get the model
126
     *
127
     * @return Model
128
     */
129
    public function getModel()
130
    {
131
        return $this->model;
132
    }
133
134
}
135