1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260<?php
/**
* Directus – <http://getdirectus.com>
*
* @link The canonical repository – <https://github.com/directus/directus>
* @copyright Copyright 2006-2017 RANGER Studio, LLC – <http://rangerstudio.com>
* @license GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html>
*/
namespace Directus\Database\Object;
use Directus\Collection\Arrayable;
use Directus\Util\Traits\ArrayPropertyAccess;
use Directus\Util\Traits\ArrayPropertyToArray;
use Directus\Util\Traits\ArraySetter;
/**
* Column relationship
*
* @author Welling Guzmán <welling@rngr.org>
*/
class ColumnRelationship implements \ArrayAccess, Arrayable, \JsonSerializable
{
use ArraySetter, ArrayPropertyAccess, ArrayPropertyToArray;
const ONE_TO_MANY = 'ONETOMANY';
const MANY_TO_MANY = 'MANYTOMANY';
const MANY_TO_ONE = 'MANYTOONE';
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $relatedTable;
/**
* @var string
*/
protected $junctionTable;
/**
* @var string
*/
protected $junctionKeyRight;
/**
* @var string
*/
protected $junctionKeyLeft;
/**
* @var array
*/
protected $readableProperty = [];
/**
* @var array
*/
protected $writableProperty = [];
/**
* @var array
*/
protected $avoidSerializing = [
'writableProperty',
'readableProperty'
];
/**
* ColumnRelationship constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
$this->setData($data);
$this->readableProperty = $this->writableProperty = [
'type',
'related_table',
'junction_table',
'junction_key_left',
'junction_key_right'
];
}
/**
* Set the relationship type
*
* @param $type
*
* @return ColumnRelationship
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get the relationship type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set the related table name
*
* @param $table
*
* @return ColumnRelationship
*/
public function setRelatedTable($table)
{
$this->relatedTable = $table;
return $this;
}
/**
* Get the related table name
*
* @return string
*/
public function getRelatedTable()
{
return $this->relatedTable;
}
/**
* Set the junction table name
*
* @param $table
*
* @return ColumnRelationship
*/
public function setJunctionTable($table)
{
$this->junctionTable = $table;
return $this;
}
/**
* Get the junction table name
*
* @return string
*/
public function getJunctionTable()
{
return $this->junctionTable;
}
/**
* Set the junction key left
*
* @param $column
*
* @return ColumnRelationship
*/
public function setJunctionKeyLeft($column)
{
$this->junctionKeyLeft = $column;
return $this;
}
/**
* Get the junction key left
*
* @return string
*/
public function getJunctionKeyLeft()
{
return $this->junctionKeyLeft;
}
/**
* Set the junction key right
*
* @param $column
*
* @return ColumnRelationship
*/
public function setJunctionKeyRight($column)
{
$this->junctionKeyRight = $column;
return $this;
}
/**
* Get the junction key right
*
* @return string
*/
public function getJunctionKeyRight()
{
return $this->junctionKeyRight;
}
/**
* Checks whether the relatiopship is MANY TO ONE
*
* @return bool
*/
public function isManyToOne()
{
return $this->getType() === static::MANY_TO_ONE;
}
/**
* Checks whether the relatiopship is MANY TO MANY
*
* @return bool
*/
public function isManyToMany()
{
return $this->getType() === static::MANY_TO_MANY;
}
/**
* Checks whether the relatiopship is ONE TO MANY
*
* @return bool
*/
public function isOneToMany()
{
return $this->getType() === static::ONE_TO_MANY;
}
/**
* Gets an Array representation
*
* @return array
*/
public function toArray()
{
return $this->propertyArray();
}
/**
* @inheritDoc
*/
function jsonSerialize()
{
return $this->toArray();
}
}