1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Builders; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Types\Type; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use LaravelDoctrine\Fluent\Extensions\Gedmo\GedmoBuilderHints; |
8
|
|
|
use LaravelDoctrine\Fluent\Fluent; |
9
|
|
|
use LogicException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method Field array($name, callable $callback = null) |
13
|
|
|
*/ |
14
|
|
|
class Builder extends AbstractBuilder implements Fluent |
15
|
|
|
{ |
16
|
|
|
use Traits\Fields; |
17
|
|
|
use Traits\Dates; |
18
|
|
|
use Traits\Aliases; |
19
|
|
|
use Traits\Relations; |
20
|
|
|
use Traits\Constraints; |
21
|
|
|
use Traits\Macroable; |
22
|
|
|
use Traits\Queueable; |
23
|
|
|
use Traits\QueuesMacros; |
24
|
|
|
use GedmoBuilderHints; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
8 |
|
public function table($name, callable $callback = null) |
30
|
|
|
{ |
31
|
8 |
|
$this->disallowInEmbeddedClasses(); |
32
|
|
|
|
33
|
4 |
|
$table = new Table($this->builder, $name); |
34
|
|
|
|
35
|
4 |
|
$this->callbackAndQueue($table, $callback); |
36
|
|
|
|
37
|
4 |
|
return $table; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
156 |
|
public function entity(callable $callback = null) |
44
|
|
|
{ |
45
|
156 |
|
$this->disallowInEmbeddedClasses(); |
46
|
|
|
|
47
|
152 |
|
$entity = new Entity($this->builder, $this->namingStrategy); |
48
|
|
|
|
49
|
152 |
|
$this->callIfCallable($callback, $entity); |
50
|
|
|
|
51
|
152 |
|
return $entity; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
12 |
|
public function inheritance($type, callable $callback = null) |
58
|
|
|
{ |
59
|
12 |
|
$inheritance = Inheritance\InheritanceFactory::create($type, $this->builder); |
60
|
|
|
|
61
|
12 |
|
$this->callIfCallable($callback, $inheritance); |
62
|
|
|
|
63
|
12 |
|
return $inheritance; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
4 |
|
public function singleTableInheritance(callable $callback = null) |
70
|
|
|
{ |
71
|
4 |
|
return $this->inheritance(Inheritance\Inheritance::SINGLE, $callback); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
4 |
|
public function joinedTableInheritance(callable $callback = null) |
78
|
|
|
{ |
79
|
4 |
|
return $this->inheritance(Inheritance\Inheritance::JOINED, $callback); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
8 |
|
public function embed($embeddable, $field = null, callable $callback = null) |
86
|
|
|
{ |
87
|
8 |
|
$embedded = new Embedded( |
88
|
8 |
|
$this->builder, |
89
|
8 |
|
$this->namingStrategy, |
90
|
8 |
|
$this->guessSingularField($embeddable, $field), |
91
|
6 |
|
$embeddable |
92
|
2 |
|
); |
93
|
|
|
|
94
|
8 |
|
$this->callbackAndQueue($embedded, $callback); |
95
|
|
|
|
96
|
8 |
|
return $embedded; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
12 |
|
public function override($name, callable $callback) |
103
|
|
|
{ |
104
|
12 |
|
$override = new Overrides\Override( |
105
|
12 |
|
$this->getBuilder(), |
106
|
12 |
|
$this->getNamingStrategy(), |
107
|
12 |
|
$name, |
108
|
9 |
|
$callback |
109
|
3 |
|
); |
110
|
|
|
|
111
|
12 |
|
$this->queue($override); |
112
|
|
|
|
113
|
12 |
|
return $override; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
8 |
|
public function events(callable $callback = null) |
120
|
|
|
{ |
121
|
8 |
|
$events = new LifecycleEvents($this->builder); |
122
|
|
|
|
123
|
8 |
|
$this->callbackAndQueue($events, $callback); |
124
|
|
|
|
125
|
8 |
|
return $events; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
8 |
|
public function listen(callable $callback = null) |
132
|
|
|
{ |
133
|
8 |
|
$events = new EntityListeners($this->builder); |
134
|
|
|
|
135
|
8 |
|
$this->callbackAndQueue($events, $callback); |
136
|
|
|
|
137
|
8 |
|
return $events; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
200 |
|
public function isEmbeddedClass() |
144
|
|
|
{ |
145
|
200 |
|
return $this->builder->getClassMetadata()->isEmbeddedClass; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $name |
150
|
|
|
* @param callable|null $callback |
151
|
|
|
* |
152
|
|
|
* @return Field |
153
|
|
|
*/ |
154
|
12 |
|
protected function setArray($name, callable $callback = null) |
155
|
|
|
{ |
156
|
12 |
|
return $this->field(Type::TARRAY, $name, $callback); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $method |
161
|
|
|
* @param array $params |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
456 |
|
public function __call($method, $params) |
166
|
|
|
{ |
167
|
|
|
// Workaround for reserved keywords |
168
|
456 |
|
if ($method === 'array') { |
169
|
12 |
|
return call_user_func_array([$this, 'setArray'], $params); |
170
|
|
|
} |
171
|
|
|
|
172
|
444 |
|
if ($this->hasMacro($method)) { |
173
|
440 |
|
return $this->queueMacro($method, $params); |
174
|
|
|
} |
175
|
|
|
|
176
|
4 |
|
throw new InvalidArgumentException('Fluent builder method ['.$method.'] does not exist'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
200 |
|
protected function disallowInEmbeddedClasses($message = '') |
183
|
|
|
{ |
184
|
200 |
|
if ($this->isEmbeddedClass()) { |
185
|
20 |
|
throw new LogicException($message); |
186
|
|
|
} |
187
|
180 |
|
} |
188
|
|
|
} |
189
|
|
|
|