Completed
Push — master ( 798f47...8bf665 )
by Evan
02:13
created

Builder::order()   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 1
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Silk\Query;
4
5
use WP_Query;
6
use Silk\Models\Post;
7
use Illuminate\Support\Collection;
8
9
class Builder
10
{
11
    /**
12
     * WP_Query instance
13
     * @var \WP_Query
14
     */
15
    protected $query;
16
17
    /**
18
     * Post Model instance
19
     * @var \Silk\Models\Post
20
     */
21
    protected $model;
22
23
    /**
24
     * Builder constructor.
25
     *
26
     * @param WP_Query $query
27
     */
28
    public function __construct(WP_Query $query)
29
    {
30
        $this->query = $query;
31
    }
32
33
    /**
34
     * Limit the number of returned results
35
     *
36
     * @param integer $limit  The maximum number of results to return
37
     *                        use -1 for no limit
38
     *
39
     * @return $this
40
     */
41
    public function limit($limit)
42
    {
43
        $this->query->set('posts_per_page', (int) $limit);
44
45
        return $this;
46
    }
47
48
    /**
49
     * [order description]
50
     * @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...
51
     * @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...
52
     */
53
    public function order($order)
54
    {
55
        $this->query->set('order', strtoupper($order));
56
57
        return $this;
58
    }
59
60
    /**
61
     * Get the results as a collection of post model instances
62
     *
63
     * @return Collection
64
     */
65
    public function results()
66
    {
67
        if ($this->model) {
68
            $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\Models\Post>. 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...
69
            $this->query->set('fields', ''); // return objects
70
        }
71
72
        $collection = Collection::make($this->query->get_posts());
73
        $modelClass = $this->model ? get_class($this->model) : Post::class;
74
75
        return $collection->transform(function($post) use ($modelClass) {
76
            return new $modelClass($post);
77
        });
78
    }
79
80
    /**
81
     * Set the model for this query.
82
     *
83
     * @param Post $model
84
     *
85
     * @return $this
86
     */
87
    public function setModel(Post $model)
88
    {
89
        $this->model = $model;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Get the model
96
     *
97
     * @return Post
98
     */
99
    public function getModel()
100
    {
101
        return $this->model;
102
    }
103
104
}
105