|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Foreign_Key definition |
|
7
|
|
|
* |
|
8
|
|
|
* Extracted from Table_Index from 0.2.* |
|
9
|
|
|
* |
|
10
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
11
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
12
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
13
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
14
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
15
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
16
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
17
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
18
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
19
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
20
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 0.3.0 |
|
23
|
|
|
* @author Glynn Quelch <[email protected]> |
|
24
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
25
|
|
|
* @package PinkCrab\Table_Builder |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace PinkCrab\Table_Builder; |
|
29
|
|
|
|
|
30
|
|
|
use PinkCrab\Table_Builder\Index; |
|
31
|
|
|
|
|
32
|
|
|
class Foreign_Key { |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Index name |
|
36
|
|
|
* |
|
37
|
|
|
* @since 0.1.0 |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $key_name; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Column referenced |
|
44
|
|
|
* |
|
45
|
|
|
* @since 0.1.0 |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $column; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Sets the reference column |
|
52
|
|
|
* |
|
53
|
|
|
* @since 0.1.0 |
|
54
|
|
|
* @var string|null |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $reference_column; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The table used. |
|
60
|
|
|
* |
|
61
|
|
|
* @since 0.1.0 |
|
62
|
|
|
* @var string|null |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $reference_table; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Action to execute on update. |
|
68
|
|
|
* |
|
69
|
|
|
* @since 0.1.0 |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $on_update = ''; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Action to execute on delete. |
|
76
|
|
|
* |
|
77
|
|
|
* @since 0.1.0 |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $on_delete = ''; |
|
81
|
|
|
|
|
82
|
|
|
public function __construct( string $column, ?string $key_name = null ) { |
|
83
|
|
|
$this->key_name = $key_name ?? 'fk_' . $column; |
|
84
|
|
|
$this->column = $column; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Shortcut for definig the reference table and column. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $reference_table |
|
91
|
|
|
* @param string $reference_column |
|
92
|
|
|
* @return self |
|
93
|
|
|
*/ |
|
94
|
|
|
public function reference( string $reference_table, string $reference_column ): self { |
|
95
|
|
|
$this->reference_column = $reference_column; |
|
96
|
|
|
$this->reference_table = $reference_table; |
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set the reference table |
|
102
|
|
|
* |
|
103
|
|
|
* @since 0.1.0 |
|
104
|
|
|
* @param string $reference_table |
|
105
|
|
|
* @return self |
|
106
|
|
|
*/ |
|
107
|
|
|
public function reference_table( string $reference_table ): self { |
|
108
|
|
|
$this->reference_table = $reference_table; |
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Add reference_column to the key. |
|
114
|
|
|
* |
|
115
|
|
|
* @since 0.1.0 |
|
116
|
|
|
* @param string $reference_column |
|
117
|
|
|
* @return self |
|
118
|
|
|
*/ |
|
119
|
|
|
public function reference_column( string $reference_column ): self { |
|
120
|
|
|
$this->reference_column = $reference_column; |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Sets the on update action. |
|
126
|
|
|
* |
|
127
|
|
|
* @since 0.1.0 |
|
128
|
|
|
* @param string $action |
|
129
|
|
|
* @return self |
|
130
|
|
|
*/ |
|
131
|
|
|
public function on_update( string $action ): self { |
|
132
|
|
|
$this->on_update = $action; |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Sets the on update action. |
|
139
|
|
|
* |
|
140
|
|
|
* @since 0.1.0 |
|
141
|
|
|
* @param string $action |
|
142
|
|
|
* @return self |
|
143
|
|
|
*/ |
|
144
|
|
|
public function on_delete( string $action ): self { |
|
145
|
|
|
$this->on_delete = $action; |
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get index name |
|
151
|
|
|
* |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
public function get_key_name(): string { |
|
155
|
|
|
return $this->key_name; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get column referenced |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
public function get_column(): string { |
|
164
|
|
|
return $this->column; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get the table used. |
|
169
|
|
|
* |
|
170
|
|
|
* @return string|null |
|
171
|
|
|
*/ |
|
172
|
|
|
public function get_reference_table(): ?string { |
|
173
|
|
|
return $this->reference_table; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get the table used. |
|
178
|
|
|
* |
|
179
|
|
|
* @return string|null |
|
180
|
|
|
*/ |
|
181
|
|
|
public function get_reference_column(): ?string { |
|
182
|
|
|
return $this->reference_column; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get action to execute on update. |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function get_on_update(): string { |
|
191
|
|
|
return $this->on_update; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Get action to execute on delete. |
|
196
|
|
|
* |
|
197
|
|
|
* @return string |
|
198
|
|
|
*/ |
|
199
|
|
|
public function get_on_delete(): string { |
|
200
|
|
|
return $this->on_delete; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Exports the index as a stdClass |
|
205
|
|
|
* |
|
206
|
|
|
* @return object |
|
207
|
|
|
*/ |
|
208
|
|
|
public function export() { |
|
209
|
|
|
return (object) array( |
|
210
|
|
|
'key_name' => $this->key_name, |
|
211
|
|
|
'column' => $this->column, |
|
212
|
|
|
'reference_column' => $this->reference_column, |
|
213
|
|
|
'reference_table' => $this->reference_table, |
|
214
|
|
|
'on_update' => $this->on_update, |
|
215
|
|
|
'on_delete' => $this->on_delete, |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|