Completed
Push — master ( f84666...e52fc0 )
by Evan
01:46
created

Builder::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Silk\Query;
4
5
use Silk\Type\Model;
6
use Silk\Contracts\Query\BuildsQueries;
7
use Silk\Support\Collection;
8
9
abstract class Builder implements BuildsQueries
10
{
11
    /**
12
     * The query instance
13
     * @var object  The query instance
14
     */
15
    protected $query;
16
17
    /**
18
     * @var Model|null  The model instance if set, otherwise null
19
     */
20
    protected $model;
21
22
    /**
23
     * Execute the query and return the raw results.
24
     *
25
     * @return array
26
     */
27
    abstract protected function query();
28
29
    /**
30
     * Get the results as a collection
31
     *
32
     * @return Collection
33
     */
34
    public function results()
35
    {
36
        if ($this->model) {
37
            return $this->collectModels();
38
        }
39
40
        return new Collection($this->query());
41
    }
42
43
    /**
44
     * Get the results as a collection of model instances.
45
     *
46
     * @return Collection
47
     */
48
    protected function collectModels()
49
    {
50
        $modelClass = get_class($this->model);
51
52
        return Collection::make($this->query())
53
                         ->map(function ($result) use ($modelClass) {
54
                             return new $modelClass($result);
55
                         });
56
    }
57
58
    /**
59
     * Set the model for this query.
60
     *
61
     * @param Model $model
62
     *
63
     * @return $this
64
     */
65
    public function setModel(Model $model)
66
    {
67
        $this->model = $model;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Get the model
74
     *
75
     * @return Model
0 ignored issues
show
Documentation introduced by
Should the return type not be Model|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
76
     */
77
    public function getModel()
78
    {
79
        return $this->model;
80
    }
81
82
    /**
83
     * Get the query object.
84
     *
85
     * @return object
86
     */
87
    public function getQuery()
88
    {
89
        return $this->query;
90
    }
91
92
    /**
93
     * Handle dynamic method calls on the builder.
94
     *
95
     * @param string $name      Method name
96
     * @param array  $arguments
97
     *
98
     * @return mixed
99
     */
100
    public function __call($name, $arguments)
101
    {
102
        if (method_exists($this->model, 'scope' . ucfirst($name))) {
103
            return $this->model->{'scope' . ucfirst($name)}($this, ...$arguments);
104
        }
105
106
        throw new \BadMethodCallException("No '$name' method exists on " . static::class);
107
    }
108
}
109