1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Models\Sql\Relations\Maps; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Reactor\Models\Sql\Model; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Inverse |
22
|
|
|
* |
23
|
|
|
* @package O2System\Reactor\Models\Sql\Relations\Maps |
24
|
|
|
*/ |
25
|
|
|
class Inverse |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Relation Model |
29
|
|
|
* |
30
|
|
|
* @var Model |
31
|
|
|
*/ |
32
|
|
|
public $relationModel; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Relation Table |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $relationTable; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Relation Foreign Key |
43
|
|
|
* |
44
|
|
|
* Foreign Key of Reference Table |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $relationForeignKey; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Reference Model |
52
|
|
|
* |
53
|
|
|
* @var Model |
54
|
|
|
*/ |
55
|
|
|
public $referenceModel; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Reference Table |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
public $referenceTable; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Reference Primary Key |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
public $referencePrimaryKey; |
70
|
|
|
|
71
|
|
|
// ------------------------------------------------------------------------ |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* InverseMapper constructor. |
75
|
|
|
* |
76
|
|
|
* @param Model $relationModel |
77
|
|
|
* @param string|Model $referenceModel |
78
|
|
|
* @param string|null $relationForeignKey |
79
|
|
|
* @param string|null $referencePrimaryKey |
80
|
|
|
*/ |
81
|
|
|
public function __construct( |
82
|
|
|
Model $relationModel, |
83
|
|
|
$referenceModel, |
84
|
|
|
$relationForeignKey = null, |
85
|
|
|
$referencePrimaryKey = null |
|
|
|
|
86
|
|
|
) { |
87
|
|
|
// Map Relation Model |
88
|
|
|
$this->relationModel =& $relationModel; |
89
|
|
|
|
90
|
|
|
// Map Relation Table |
91
|
|
|
$this->relationTable = $relationModel->table; |
92
|
|
|
|
93
|
|
|
// Map Reference Model |
94
|
|
|
$this->mapReferenceModel($referenceModel); |
95
|
|
|
|
96
|
|
|
// Map Relation Primary Key |
97
|
|
|
$this->relationForeignKey = (isset($relationForeignKey) ? $relationForeignKey |
98
|
|
|
: $this->mapRelationForeignKey()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// ------------------------------------------------------------------------ |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Map Relation Model |
105
|
|
|
* |
106
|
|
|
* @param string|Model $referenceModel |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
private function mapReferenceModel($referenceModel) |
111
|
|
|
{ |
112
|
|
|
if ($referenceModel instanceof Model) { |
113
|
|
|
$this->referenceModel = $referenceModel; |
114
|
|
|
$this->referenceTable = $this->referenceModel->table; |
115
|
|
|
$this->referencePrimaryKey = $this->referenceModel->primaryKey; |
116
|
|
|
} elseif (class_exists($referenceModel)) { |
117
|
|
|
$this->referenceModel = new $referenceModel(); |
118
|
|
|
$this->referenceTable = $this->referenceModel->table; |
119
|
|
|
$this->referencePrimaryKey = $this->referenceModel->primaryKey; |
120
|
|
|
} else { |
121
|
|
|
$this->referenceTable = $referenceModel; |
122
|
|
|
$this->referencePrimaryKey = 'id'; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// ------------------------------------------------------------------------ |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Map Relation Foreign Key |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
private function mapRelationForeignKey() |
134
|
|
|
{ |
135
|
|
|
$tablePrefixes = [ |
136
|
|
|
't_', |
137
|
|
|
'tm_', |
138
|
|
|
'tr_', |
139
|
|
|
'tb_', |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
return $this->referencePrimaryKey . '_' . str_replace($tablePrefixes, '', $this->referenceTable); |
143
|
|
|
} |
144
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.