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