1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Builders; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Types\Type; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use LaravelDoctrine\Fluent\Fluent; |
8
|
|
|
use LogicException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @method $this array($name, callable $callback = null) |
12
|
|
|
*/ |
13
|
|
|
class Builder extends AbstractBuilder implements Fluent |
14
|
|
|
{ |
15
|
|
|
use Traits\Fields; |
16
|
|
|
use Traits\Dates; |
17
|
|
|
use Traits\Aliases; |
18
|
|
|
use Traits\Relations; |
19
|
|
|
use Traits\Macroable; |
20
|
|
|
use Traits\Queueable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string|callable $name |
24
|
|
|
* @param callable|null $callback |
25
|
|
|
* |
26
|
|
|
* @return Table |
27
|
|
|
*/ |
28
|
|
|
public function table($name, callable $callback = null) |
29
|
2 |
|
{ |
30
|
|
|
$this->disallowInEmbeddedClasses(); |
31
|
2 |
|
|
32
|
1 |
|
$table = new Table($this->builder); |
33
|
|
|
|
34
|
|
|
if (is_callable($name)) { |
35
|
1 |
|
$name($table); |
36
|
|
|
} else { |
37
|
1 |
|
$table->setName($name); |
38
|
1 |
|
} |
39
|
1 |
|
|
40
|
1 |
|
if (is_callable($callback)) { |
41
|
|
|
$callback($table); |
42
|
|
|
} |
43
|
1 |
|
|
44
|
1 |
|
return $table; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
1 |
|
/** |
48
|
|
|
* @param callable|null $callback |
49
|
|
|
* |
50
|
|
|
* @return Entity |
51
|
|
|
*/ |
52
|
|
|
public function entity(callable $callback = null) |
53
|
|
|
{ |
54
|
|
|
$this->disallowInEmbeddedClasses(); |
55
|
3 |
|
|
56
|
|
|
$entity = new Entity($this->builder, $this->namingStrategy); |
57
|
3 |
|
|
58
|
1 |
|
if (is_callable($callback)) { |
59
|
|
|
$callback($entity); |
60
|
|
|
} |
61
|
2 |
|
|
62
|
|
|
return $entity; |
63
|
2 |
|
} |
64
|
1 |
|
|
65
|
1 |
|
/** |
66
|
|
|
* @param string $type |
67
|
2 |
|
* @param callable|null $callback |
68
|
|
|
* |
69
|
|
|
* @return Inheritance\Inheritance |
70
|
|
|
*/ |
71
|
|
|
public function inheritance($type, callable $callback = null) |
72
|
|
|
{ |
73
|
|
|
$inheritance = Inheritance\InheritanceFactory::create($type, $this->builder); |
74
|
|
|
|
75
|
|
|
if (is_callable($callback)) { |
76
|
3 |
|
$callback($inheritance); |
77
|
|
|
} |
78
|
3 |
|
|
79
|
|
|
return $inheritance; |
80
|
3 |
|
} |
81
|
3 |
|
|
82
|
3 |
|
/** |
83
|
|
|
* @param callable|null $callback |
84
|
3 |
|
* |
85
|
|
|
* @return Inheritance\Inheritance |
86
|
|
|
*/ |
87
|
|
|
public function singleTableInheritance(callable $callback = null) |
88
|
|
|
{ |
89
|
|
|
return $this->inheritance(Inheritance\Inheritance::SINGLE, $callback); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
/** |
93
|
|
|
* @param callable|null $callback |
94
|
1 |
|
* |
95
|
|
|
* @return Inheritance\Inheritance |
96
|
|
|
*/ |
97
|
|
|
public function joinedTableInheritance(callable $callback = null) |
98
|
|
|
{ |
99
|
|
|
return $this->inheritance(Inheritance\Inheritance::JOINED, $callback); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
/** |
103
|
|
|
* @param array|string $columns |
104
|
1 |
|
* |
105
|
|
|
* @return Index |
106
|
|
|
*/ |
107
|
|
|
public function index($columns) |
108
|
|
|
{ |
109
|
|
|
return $this->constraint( |
|
|
|
|
110
|
|
|
Index::class, |
111
|
|
|
is_array($columns) ? $columns : func_get_args() |
112
|
3 |
|
); |
113
|
|
|
} |
114
|
3 |
|
|
115
|
3 |
|
/** |
116
|
3 |
|
* @param array|string $fields |
117
|
3 |
|
* |
118
|
|
|
* @return Primary |
119
|
|
|
*/ |
120
|
|
|
public function primary($fields) |
121
|
|
|
{ |
122
|
|
|
return $this->constraint( |
|
|
|
|
123
|
|
|
Primary::class, |
124
|
|
|
is_array($fields) ? $fields : func_get_args() |
125
|
3 |
|
); |
126
|
|
|
} |
127
|
3 |
|
|
128
|
3 |
|
/** |
129
|
3 |
|
* @param array|string $columns |
130
|
3 |
|
* |
131
|
|
|
* @return UniqueConstraint |
132
|
|
|
*/ |
133
|
|
|
public function unique($columns) |
134
|
|
|
{ |
135
|
|
|
return $this->constraint( |
|
|
|
|
136
|
|
|
UniqueConstraint::class, |
137
|
|
|
is_array($columns) ? $columns : func_get_args() |
138
|
3 |
|
); |
139
|
|
|
} |
140
|
3 |
|
|
141
|
3 |
|
/** |
142
|
3 |
|
* @param string $class |
143
|
3 |
|
* @param array $columns |
144
|
|
|
* |
145
|
|
|
* @return Index|Primary|UniqueConstraint |
146
|
|
|
*/ |
147
|
|
|
protected function constraint($class, array $columns) |
148
|
|
|
{ |
149
|
|
|
$constraint = new $class($this->builder, $columns); |
150
|
|
|
|
151
|
|
|
$this->queue($constraint); |
152
|
9 |
|
|
153
|
|
|
return $constraint; |
154
|
9 |
|
} |
155
|
|
|
|
156
|
9 |
|
/** |
157
|
|
|
* @param string $embeddable |
158
|
9 |
|
* @param string|null $field |
159
|
|
|
* @param callable|null $callback |
160
|
|
|
* |
161
|
|
|
* @return Embedded |
162
|
|
|
*/ |
163
|
|
|
public function embed($embeddable, $field = null, callable $callback = null) |
164
|
|
|
{ |
165
|
|
|
$embedded = new Embedded( |
166
|
|
|
$this->builder, |
167
|
|
|
$this->namingStrategy, |
168
|
2 |
|
$this->guessSingularField($embeddable, $field), |
169
|
|
|
$embeddable |
170
|
2 |
|
); |
171
|
2 |
|
|
172
|
2 |
|
$this->callbackAndQueue($embedded, $callback); |
173
|
2 |
|
|
174
|
|
|
return $embedded; |
175
|
2 |
|
} |
176
|
|
|
|
177
|
2 |
|
/** |
178
|
|
|
* @param string $name |
179
|
2 |
|
* @param callable $callback |
180
|
|
|
* |
181
|
|
|
* @return Overrides\Override |
182
|
|
|
*/ |
183
|
|
|
public function override($name, callable $callback) |
184
|
|
|
{ |
185
|
|
|
$override = new Overrides\Override( |
186
|
|
|
$this->getBuilder(), |
187
|
|
|
$this->getNamingStrategy(), |
188
|
3 |
|
$name, |
189
|
|
|
$callback |
190
|
3 |
|
); |
191
|
3 |
|
|
192
|
3 |
|
$this->queue($override); |
193
|
3 |
|
|
194
|
|
|
return $override; |
195
|
3 |
|
} |
196
|
|
|
|
197
|
3 |
|
/** |
198
|
|
|
* @param callable|null $callback |
199
|
3 |
|
* |
200
|
|
|
* @return LifecycleEvents |
201
|
|
|
*/ |
202
|
|
|
public function events(callable $callback = null) |
203
|
|
|
{ |
204
|
|
|
$events = new LifecycleEvents($this->builder); |
205
|
|
|
|
206
|
|
|
$this->callbackAndQueue($events, $callback); |
207
|
2 |
|
|
208
|
|
|
return $events; |
209
|
2 |
|
} |
210
|
|
|
|
211
|
2 |
|
/** |
212
|
|
|
* @return bool |
213
|
2 |
|
*/ |
214
|
|
|
public function isEmbeddedClass() |
215
|
|
|
{ |
216
|
|
|
return $this->builder->getClassMetadata()->isEmbeddedClass; |
217
|
|
|
} |
218
|
|
|
|
219
|
14 |
|
/** |
220
|
|
|
* @param string $name |
221
|
14 |
|
* @param callable|null $callback |
222
|
|
|
* |
223
|
|
|
* @return Field |
224
|
|
|
*/ |
225
|
|
|
protected function setArray($name, callable $callback = null) |
226
|
|
|
{ |
227
|
|
|
return $this->field(Type::TARRAY, $name, $callback); |
228
|
|
|
} |
229
|
|
|
|
230
|
7 |
|
/** |
231
|
|
|
* @param string $method |
232
|
|
|
* @param array $params |
233
|
7 |
|
* |
234
|
3 |
|
* @return mixed |
235
|
|
|
*/ |
236
|
|
|
public function __call($method, $params) |
237
|
4 |
|
{ |
238
|
3 |
|
// Workaround for reserved keywords |
239
|
|
|
if ($method === 'array') { |
240
|
|
|
return call_user_func_array([$this, 'setArray'], $params); |
241
|
1 |
|
} |
242
|
|
|
|
243
|
|
|
if ($this->hasMacro($method)) { |
244
|
|
|
return $this->callMacro($method, $params); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
throw new InvalidArgumentException('Fluent builder method [' . $method . '] does not exist'); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param string $message |
252
|
|
|
* @throws LogicException |
253
|
|
|
*/ |
254
|
|
|
protected function disallowInEmbeddedClasses($message = "") |
255
|
|
|
{ |
256
|
|
|
if ($this->isEmbeddedClass()) { |
257
|
|
|
throw new LogicException($message); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|