1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* components |
4
|
|
|
* |
5
|
|
|
* @author Wolfy-J |
6
|
|
|
*/ |
7
|
|
|
namespace Spiral\ORM\Schemas\Definitions; |
8
|
|
|
|
9
|
|
|
use Spiral\ORM\Exceptions\SchemaException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Defines relation in schema. |
13
|
|
|
*/ |
14
|
|
|
final class RelationDefinition |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Relation name. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $type; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $target; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $options = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Name of relation to be inversed to. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $inverse = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Defines where relation comes from. |
47
|
|
|
* |
48
|
|
|
* @var RelationContext |
49
|
|
|
*/ |
50
|
|
|
private $sourceContext; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Defines where relation points to (if any). |
54
|
|
|
* |
55
|
|
|
* @var RelationContext|null |
56
|
|
|
*/ |
57
|
|
|
private $targetContext; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name |
61
|
|
|
* @param string $type |
62
|
|
|
* @param string $target |
63
|
|
|
* @param array $options |
64
|
|
|
* @param mixed $inverse Name of relation to inversed to. |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
string $name, |
68
|
|
|
string $type, |
69
|
|
|
string $target, |
70
|
|
|
array $options, |
71
|
|
|
string $inverse = null |
72
|
|
|
) { |
73
|
|
|
$this->name = $name; |
74
|
|
|
$this->type = $type; |
75
|
|
|
$this->target = $target; |
76
|
|
|
$this->options = $options; |
77
|
|
|
$this->inverse = $inverse; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getName(): string |
84
|
|
|
{ |
85
|
|
|
return $this->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getType(): string |
92
|
|
|
{ |
93
|
|
|
return $this->type; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Target class name, see more information about target context via targetContext() method. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getTarget(): string |
102
|
|
|
{ |
103
|
|
|
return $this->target; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getOptions(): array |
110
|
|
|
{ |
111
|
|
|
return $this->options; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Source context (where relation comes from). |
116
|
|
|
* |
117
|
|
|
* @return RelationContext |
118
|
|
|
*/ |
119
|
|
|
public function sourceContext(): RelationContext |
120
|
|
|
{ |
121
|
|
|
if (empty($this->sourceContext)) { |
122
|
|
|
throw new SchemaException("Source context not set"); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->sourceContext; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Target context if any. |
130
|
|
|
* |
131
|
|
|
* @return null|RelationContext |
132
|
|
|
*/ |
133
|
|
|
public function targetContext() |
134
|
|
|
{ |
135
|
|
|
return $this->targetContext; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set relation contexts. |
140
|
|
|
* |
141
|
|
|
* @param RelationContext $source |
142
|
|
|
* @param RelationContext|null $target |
143
|
|
|
* |
144
|
|
|
* @return RelationDefinition |
145
|
|
|
*/ |
146
|
|
|
public function withContext(RelationContext $source, RelationContext $target = null): self |
147
|
|
|
{ |
148
|
|
|
$definition = clone $this; |
149
|
|
|
$definition->sourceContext = $source; |
150
|
|
|
$definition->targetContext = $target; |
151
|
|
|
|
152
|
|
|
return $definition; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Create version of definition with different set of options. |
157
|
|
|
* |
158
|
|
|
* @param array $options |
159
|
|
|
* |
160
|
|
|
* @return RelationDefinition |
161
|
|
|
*/ |
162
|
|
|
public function withOptions(array $options): self |
163
|
|
|
{ |
164
|
|
|
$definition = clone $this; |
165
|
|
|
$definition->options = $options; |
166
|
|
|
|
167
|
|
|
return $definition; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Checks if inversion if required. |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
public function needInversion(): bool |
176
|
|
|
{ |
177
|
|
|
return !empty($this->inverse); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Name of relation to be inversed to. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getInverse(): string |
186
|
|
|
{ |
187
|
|
|
return $this->inverse; |
188
|
|
|
} |
189
|
|
|
} |