Completed
Push — master ( 5a2e90...9a86e3 )
by Evan
03:43
created

Builder::results()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 8
c 2
b 0
f 1
nc 4
nop 0
dl 0
loc 14
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
     * Get the results as a collection of post model instances
50
     *
51
     * @return Collection
52
     */
53
    public function results()
54
    {
55
        if ($this->model) {
56
            $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...
57
            $this->query->set('fields', ''); // return objects
58
        }
59
60
        $collection = Collection::make($this->query->get_posts());
61
        $modelClass = $this->model ? get_class($this->model) : Post::class;
62
63
        return $collection->transform(function($post) use ($modelClass) {
64
            return new $modelClass($post);
65
        });
66
    }
67
68
    /**
69
     * Set the model for this query.
70
     *
71
     * @param Post $model
72
     *
73
     * @return $this
74
     */
75
    public function setModel(Post $model)
76
    {
77
        $this->model = $model;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Get the model
84
     *
85
     * @return Post
86
     */
87
    public function getModel()
88
    {
89
        return $this->model;
90
    }
91
92
}
93