1 | <?php |
||
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 | 2 | public function table($name, callable $callback = null) |
|
29 | 2 | { |
|
30 | 2 | $this->disallowInEmbeddedClasses(); |
|
31 | |||
32 | 1 | $table = new Table($this->builder); |
|
33 | |||
34 | 1 | if (is_callable($name)) { |
|
35 | 1 | $name($table); |
|
36 | 1 | } else { |
|
37 | 1 | $table->setName($name); |
|
38 | } |
||
39 | |||
40 | 1 | if (is_callable($callback)) { |
|
41 | 1 | $callback($table); |
|
42 | 1 | } |
|
43 | |||
44 | 1 | return $table; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param callable|null $callback |
||
49 | * |
||
50 | * @return Entity |
||
51 | */ |
||
52 | 3 | public function entity(callable $callback = null) |
|
53 | { |
||
54 | 3 | $this->disallowInEmbeddedClasses(); |
|
55 | |||
56 | 2 | $entity = new Entity($this->builder, $this->namingStrategy); |
|
57 | |||
58 | 2 | if (is_callable($callback)) { |
|
59 | 1 | $callback($entity); |
|
60 | 1 | } |
|
61 | |||
62 | 2 | return $entity; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param string $type |
||
67 | * @param callable|null $callback |
||
68 | * |
||
69 | * @return Inheritance\Inheritance |
||
70 | */ |
||
71 | 3 | public function inheritance($type, callable $callback = null) |
|
72 | { |
||
73 | 3 | $inheritance = Inheritance\InheritanceFactory::create($type, $this->builder); |
|
74 | |||
75 | 3 | if (is_callable($callback)) { |
|
76 | 3 | $callback($inheritance); |
|
77 | 3 | } |
|
78 | |||
79 | 3 | return $inheritance; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param callable|null $callback |
||
84 | * |
||
85 | * @return Inheritance\Inheritance |
||
86 | */ |
||
87 | 1 | public function singleTableInheritance(callable $callback = null) |
|
91 | |||
92 | /** |
||
93 | * @param callable|null $callback |
||
94 | * |
||
95 | * @return Inheritance\Inheritance |
||
96 | */ |
||
97 | 1 | public function joinedTableInheritance(callable $callback = null) |
|
101 | |||
102 | /** |
||
103 | * @param array|string $columns |
||
104 | * |
||
105 | * @return Index |
||
106 | */ |
||
107 | 3 | public function index($columns) |
|
108 | { |
||
109 | 3 | return $this->constraint( |
|
110 | 3 | Index::class, |
|
111 | 3 | is_array($columns) ? $columns : func_get_args() |
|
112 | 3 | ); |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param array|string $fields |
||
117 | * |
||
118 | * @return Primary |
||
119 | */ |
||
120 | 3 | public function primary($fields) |
|
121 | { |
||
122 | 3 | return $this->constraint( |
|
123 | 3 | Primary::class, |
|
124 | 3 | is_array($fields) ? $fields : func_get_args() |
|
125 | 3 | ); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param array|string $columns |
||
130 | * |
||
131 | * @return UniqueConstraint |
||
132 | */ |
||
133 | 3 | public function unique($columns) |
|
134 | { |
||
135 | 3 | return $this->constraint( |
|
136 | 3 | UniqueConstraint::class, |
|
137 | 3 | is_array($columns) ? $columns : func_get_args() |
|
138 | 3 | ); |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param string $class |
||
143 | * @param array $columns |
||
144 | * |
||
145 | * @return mixed |
||
146 | */ |
||
147 | 9 | protected function constraint($class, array $columns) |
|
148 | { |
||
149 | 9 | $constraint = new $class($this->builder, $columns); |
|
150 | |||
151 | 9 | $this->queue($constraint); |
|
152 | |||
153 | 9 | return $constraint; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param string $embeddable |
||
158 | * @param string|null $field |
||
159 | * @param callable|null $callback |
||
160 | * |
||
161 | * @return Embedded |
||
162 | */ |
||
163 | 2 | public function embed($embeddable, $field = null, callable $callback = null) |
|
164 | { |
||
165 | 2 | $embedded = new Embedded( |
|
166 | 2 | $this->builder, |
|
167 | 2 | $this->namingStrategy, |
|
168 | 2 | $this->guessSingularField($embeddable, $field), |
|
169 | $embeddable |
||
170 | 2 | ); |
|
171 | |||
172 | 2 | $this->callbackAndQueue($embedded, $callback); |
|
173 | |||
174 | 2 | return $embedded; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param string $name |
||
179 | * @param callable $callback |
||
180 | * |
||
181 | * @return Overrides\Override |
||
182 | */ |
||
183 | 3 | public function override($name, callable $callback) |
|
184 | { |
||
185 | 3 | $override = new Overrides\Override( |
|
186 | 3 | $this->getBuilder(), |
|
187 | 3 | $this->getNamingStrategy(), |
|
188 | 3 | $name, |
|
189 | $callback |
||
190 | 3 | ); |
|
191 | |||
192 | 3 | $this->queue($override); |
|
193 | |||
194 | 3 | return $override; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param callable|null $callback |
||
199 | * |
||
200 | * @return LifecycleEvents |
||
201 | */ |
||
202 | 2 | public function events(callable $callback = null) |
|
203 | { |
||
204 | 2 | $events = new LifecycleEvents($this->builder); |
|
205 | |||
206 | 2 | $this->callbackAndQueue($events, $callback); |
|
207 | |||
208 | 2 | return $events; |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return bool |
||
213 | */ |
||
214 | 14 | public function isEmbeddedClass() |
|
218 | |||
219 | /** |
||
220 | * @param string $name |
||
221 | * @param callable|null $callback |
||
222 | * |
||
223 | * @return Field |
||
224 | */ |
||
225 | 3 | protected function setArray($name, callable $callback = null) |
|
229 | |||
230 | /** |
||
231 | * @param string $method |
||
232 | * @param array $params |
||
233 | * |
||
234 | * @return mixed |
||
235 | */ |
||
236 | 7 | public function __call($method, $params) |
|
237 | { |
||
238 | // Workaround for reserved keywords |
||
239 | 7 | if ($method === 'array') { |
|
249 | |||
250 | /** |
||
251 | * @param string $message |
||
252 | * @throws LogicException |
||
253 | */ |
||
254 | 14 | protected function disallowInEmbeddedClasses($message = "") |
|
260 | } |
||
261 |