Completed
Push — master ( 1c86dd...d36db1 )
by Jared
02:23
created

Relation::getLocalModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace Pulsar\Relation;
12
13
use Pulsar\Model;
14
use Pulsar\Query;
15
16
abstract class Relation
17
{
18
    /**
19
     * @var \Pulsar\Model
20
     */
21
    protected $localModel;
22
23
    /**
24
     * @var string
25
     */
26
    protected $localKey;
27
28
    /**
29
     * @var string
30
     */
31
    protected $foreignModel;
32
33
    /**
34
     * @var string
35
     */
36
    protected $foreignKey;
37
38
    /**
39
     * @var \Pulsar\Query
40
     */
41
    protected $query;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $empty;
47
48
    /**
49
     * @param Model  $localModel
50
     * @param string $localKey     identifying key on local model
51
     * @param string $foreignModel foreign model class
52
     * @param string $foreignKey   identifying key on foreign model
53
     */
54
    public function __construct(Model $localModel, $localKey, $foreignModel, $foreignKey)
55
    {
56
        $this->localModel = $localModel;
57
        $this->localKey = $localKey;
58
59
        $this->foreignModel = $foreignModel;
60
        $this->foreignKey = $foreignKey;
61
62
        $this->query = new Query(new $foreignModel());
63
        $this->initQuery();
64
    }
65
66
    /**
67
     * Gets the local model of the relationship.
68
     *
69
     * @return \Pulsar\Model
70
     */
71
    public function getLocalModel()
72
    {
73
        return $this->localModel;
74
    }
75
76
    /**
77
     * Gets the name of the foreign key of the relation model.
78
     *
79
     * @return string
80
     */
81
    public function getLocalKey()
82
    {
83
        return $this->localKey;
84
    }
85
86
    /**
87
     * Gets the foreign model of the relationship.
88
     *
89
     * @return string
90
     */
91
    public function getForeignModel()
92
    {
93
        return $this->foreignModel;
94
    }
95
96
    /**
97
     * Gets the name of the foreign key of the foreign model.
98
     *
99
     * @return string
100
     */
101
    public function getForeignKey()
102
    {
103
        return $this->foreignKey;
104
    }
105
106
    /**
107
     * Returns the query instance for this relation.
108
     *
109
     * @return \Pulsar\Query
110
     */
111
    public function getQuery()
112
    {
113
        return $this->query;
114
    }
115
116
    /**
117
     * Called to initialize the query.
118
     */
119
    abstract protected function initQuery();
120
121
    /**
122
     * Called to get the results of the relation query.
123
     *
124
     * @return mixed
125
     */
126
    abstract public function getResults();
127
128
    /**
129
     * Creates a new relationship model and attaches it to the owning model.
130
     *
131
     * @param array $values
132
     *
133
     * @return Model
134
     */
135
    abstract public function create(array $values = []);
136
137
    public function __call($method, $arguments)
138
    {
139
        // try calling any unkown methods on the query
140
        return call_user_func_array([$this->query, $method], $arguments);
141
    }
142
}
143