Completed
Push — master ( a3416c...a17b49 )
by Jared
02:25
created

Relation::getRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @see http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
12
namespace Pulsar\Relation;
13
14
use Pulsar\Exception\ModelException;
15
use Pulsar\Model;
16
use Pulsar\Query;
17
18
abstract class Relation
19
{
20
    /**
21
     * @var \Pulsar\Model
22
     */
23
    protected $localModel;
24
25
    /**
26
     * @var string
27
     */
28
    protected $localKey;
29
30
    /**
31
     * @var string
32
     */
33
    protected $foreignModel;
34
35
    /**
36
     * @var string
37
     */
38
    protected $foreignKey;
39
40
    /**
41
     * @var \Pulsar\Query
42
     */
43
    protected $query;
44
45
    /**
46
     * @var bool
47
     */
48
    protected $empty;
49
50
    /**
51
     * @param Model  $localModel
52
     * @param string $localKey     identifying key on local model
53
     * @param string $foreignModel foreign model class
54
     * @param string $foreignKey   identifying key on foreign model
55
     */
56
    public function __construct(Model $localModel, $localKey, $foreignModel, $foreignKey)
57
    {
58
        $this->localModel = $localModel;
59
        $this->localKey = $localKey;
60
61
        $this->foreignModel = $foreignModel;
62
        $this->foreignKey = $foreignKey;
63
64
        $this->query = new Query(new $foreignModel());
0 ignored issues
show
Documentation introduced by
new $foreignModel() is of type object, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
        $this->initQuery();
66
    }
67
68
    /**
69
     * Gets the local model of the relationship.
70
     *
71
     * @return \Pulsar\Model
72
     */
73
    public function getLocalModel()
74
    {
75
        return $this->localModel;
76
    }
77
78
    /**
79
     * Gets the name of the foreign key of the relation model.
80
     *
81
     * @return string
82
     */
83
    public function getLocalKey()
84
    {
85
        return $this->localKey;
86
    }
87
88
    /**
89
     * Gets the foreign model of the relationship.
90
     *
91
     * @return string
92
     */
93
    public function getForeignModel()
94
    {
95
        return $this->foreignModel;
96
    }
97
98
    /**
99
     * Gets the name of the foreign key of the foreign model.
100
     *
101
     * @return string
102
     */
103
    public function getForeignKey()
104
    {
105
        return $this->foreignKey;
106
    }
107
108
    /**
109
     * Returns the query instance for this relation.
110
     *
111
     * @return \Pulsar\Query
112
     */
113
    public function getQuery()
114
    {
115
        return $this->query;
116
    }
117
118
    /**
119
     * Called to initialize the query.
120
     */
121
    abstract protected function initQuery();
122
123
    /**
124
     * Called to get the results of the relation query.
125
     *
126
     * @return mixed
127
     */
128
    abstract public function getResults();
129
130
    /**
131
     * Saves a new relationship model and attaches it to
132
     * the owning model.
133
     *
134
     * @param Model $model
135
     *
136
     * @throws ModelException when the operation fails
137
     *
138
     * @return Model
139
     */
140
    abstract public function save(Model $model);
141
142
    /**
143
     * Creates a new relationship model and attaches it to
144
     * the owning model.
145
     *
146
     * @param array $values
147
     *
148
     * @throws ModelException when the operation fails
149
     *
150
     * @return Model
151
     */
152
    abstract public function create(array $values = []);
153
154
    public function __call($method, $arguments)
155
    {
156
        // try calling any unkown methods on the query
157
        return call_user_func_array([$this->query, $method], $arguments);
158
    }
159
}
160