Completed
Push — develop ( d77dc3...4e0099 )
by Evan
02:28
created

QueryBuilder::whereSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 1
rs 10
1
<?php
2
3
namespace Silk\Post;
4
5
use WP_Query;
6
use Silk\Query\Builder as BaseBuilder;
7
8
class QueryBuilder extends BaseBuilder
9
{
10
    /**
11
     * WP_Query instance
12
     *
13
     * @var WP_Query
14
     */
15
    protected $query;
16
17
    /**
18
     * Builder constructor.
19
     *
20
     * @param WP_Query $query
21
     */
22
    public function __construct(WP_Query $query = null)
23
    {
24
        if (! $query) {
25
            $query = new WP_Query();
26
        }
27
28
        $this->query = $query;
29
    }
30
31
    /**
32
     * Create a new instance.
33
     *
34
     * @param WP_Query $query
35
     *
36
     * @return static
37
     */
38
    public static function make(WP_Query $query = null)
39
    {
40
        return new static($query);
41
    }
42
43
    /**
44
     * Limit the number of returned results
45
     *
46
     * @param integer $limit  The maximum number of results to return
47
     *                        use -1 for no limit
48
     *
49
     * @return $this
50
     */
51
    public function limit($limit)
52
    {
53
        return $this->set('posts_per_page', (int) $limit);
54
    }
55
56
    /**
57
     * Return an unlimited number of results.
58
     *
59
     * @return $this
60
     */
61
    public function all()
62
    {
63
        return $this->limit(-1);
64
    }
65
66
    /**
67
     * Set the order for the query
68
     *
69
     * @param  string $order
70
     *
71
     * @return $this
72
     */
73
    public function order($order)
74
    {
75
        return $this->set('order', strtoupper($order));
76
    }
77
78
    /**
79
     * Query by post status
80
     *
81
     * @param  string|array $status  the post status or stati to match
82
     *
83
     * @return $this
84
     */
85
    public function whereStatus($status)
86
    {
87
        return $this->set('post_status', $status);
88
    }
89
90
    /**
91
     * Query by slug
92
     *
93
     * @param  string $slug  the post slug to query by
94
     *
95
     * @return $this
96
     */
97
    public function whereSlug($slug)
98
    {
99
        return $this->set('name', $slug);
100
    }
101
102
    /**
103
     * Set a query variable on the query
104
     *
105
     * @param string $var   Query variable key
106
     * @param mixed  $value Query value for key
107
     *
108
     * @return $this
109
     */
110
    public function set($var, $value)
111
    {
112
        $this->query->set($var, $value);
113
114
        return $this;
115
    }
116
117
    /**
118
     * Execute the query and return the raw results.
119
     *
120
     * @return array
121
     */
122
    protected function query()
123
    {
124
        if ($this->model) {
125
            $this->set('post_type', $this->model->post_type)
0 ignored issues
show
Documentation introduced by
The property post_type does not exist on object<Silk\Type\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...
126
                 ->set('fields', ''); // as WP_Post objects
127
        }
128
129
        return $this->query->get_posts();
130
    }
131
}
132