forked from livewire/volt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
322 lines (274 loc) · 7.52 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
namespace Livewire\Volt;
use Closure;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Livewire\Form;
use Livewire\Volt\Exceptions\TraitOrInterfaceNotFound;
use Livewire\Volt\Methods\ActionMethod;
use Livewire\Volt\Methods\ComputedMethod;
use Livewire\Volt\Methods\JsMethod;
use Livewire\Volt\Methods\ProtectedMethod;
use Livewire\Volt\Options\RuleOptions;
use Livewire\Volt\Options\StateOptions;
use Livewire\Volt\Options\UsesOptions;
/**
* Define the component's state.
*/
function state(mixed ...$properties): StateOptions
{
if (array_key_exists(0, $properties)) {
if (count($properties) === 1) {
if (is_string($properties[0])) {
$properties = [$properties[0] => null];
} elseif (Arr::isAssoc($properties[0])) {
$properties = array_map(
static fn ($key, $value) => is_numeric($key) ? [$value => null] : [$key => $value],
array_keys($properties[0]),
$properties[0]
);
$properties = array_merge(...$properties);
} else {
$properties = array_fill_keys($properties[0], null);
}
} elseif (count($properties) === 2) {
$properties = [$properties[0] => $properties[1]];
}
}
CompileContext::instance()->state = array_merge(
CompileContext::instance()->state,
$properties = array_map(fn ($value) => Property::make($value), $properties)
);
return new StateOptions($properties);
}
/**
* Define a new "protected" method on the component.
*/
function protect(Closure $closure): ProtectedMethod
{
return ProtectedMethod::make($closure);
}
/**
* Define a new "placeholder" method on the component.
*/
function placeholder(Closure|Renderable|string $closure): void
{
if (! $closure instanceof Closure) {
$closure = fn () => $closure;
}
CompileContext::instance()->placeholder = $closure;
}
/**
* Define a new "action" method on the component.
*/
function action(Closure $closure): ActionMethod
{
return ActionMethod::make($closure);
}
/**
* Define a "computed" property for the component.
*/
function computed(Closure $closure): ComputedMethod
{
return ComputedMethod::make($closure);
}
/**
* Define the component's layout.
*/
function layout(string $layout): void
{
CompileContext::instance()->layout = $layout;
}
/**
* Define the component's title.
*/
function title(Closure|string $title): void
{
CompileContext::instance()->title = $title;
}
/**
* Define the component's view data.
*/
function with(mixed ...$data): void
{
if (array_key_exists(0, $data)) {
if (count($data) === 1) {
if ($data[0] instanceof Closure) {
CompileContext::instance()->viewData = $data[0];
return;
}
if (is_string($data[0])) {
$data = [$data[0] => null];
} elseif (Arr::isAssoc($data[0])) {
$data = $data[0];
} else {
$data = array_fill_keys($data[0], null);
}
} elseif (count($data) === 2) {
$data = [$data[0] => $data[1]];
}
}
CompileContext::instance()->viewData = fn () => $data;
}
/**
* Define a "form" property for the component.
*
* @param class-string<Form> $class
*
* @throws \InvalidArgumentException
*/
function form(string $class, string $propertyName = 'form'): void
{
if (! is_subclass_of($class, Form::class)) {
throw new InvalidArgumentException('The given class must be a Livewire form object.');
}
state($propertyName)->type($class);
}
/**
* Define a new "js" method on the component.
*/
function js(Closure|string $closureOrCode): JsMethod
{
return JsMethod::make($closureOrCode);
}
/**
* Register a component's boot hook.
*/
function boot(Closure $hook): void
{
CompileContext::instance()->boot = $hook;
}
/**
* Register a component's booted hook.
*/
function booted(Closure $hook): void
{
CompileContext::instance()->booted = $hook;
}
/**
* Register a component's mount hook.
*/
function mount(Closure $hook): void
{
CompileContext::instance()->mount = $hook;
}
/**
* Register a component's hydrate hook.
*/
function hydrate(Closure|array ...$properties): void
{
if (count($properties) === 1 && array_key_exists(0, $properties)) {
if ($properties[0] instanceof Closure) {
CompileContext::instance()->hydrate = $properties[0];
return;
}
$properties = $properties[0];
}
CompileContext::instance()->hydrateProperty = array_merge(
CompileContext::instance()->hydrateProperty,
$properties,
);
}
/**
* Register a component's dehydrate hook.
*/
function dehydrate(Closure|array ...$properties): void
{
if (count($properties) === 1 && array_key_exists(0, $properties)) {
if ($properties[0] instanceof Closure) {
CompileContext::instance()->dehydrate = $properties[0];
return;
}
$properties = $properties[0];
}
CompileContext::instance()->dehydrateProperty = array_merge(
CompileContext::instance()->dehydrateProperty,
$properties,
);
}
/**
* Register a component's updating hook.
*/
function updating(Closure|array ...$properties): void
{
if (count($properties) === 1 && array_key_exists(0, $properties)) {
$properties = $properties[0];
}
CompileContext::instance()->updating = array_merge(
CompileContext::instance()->updating,
$properties,
);
}
/**
* Register a component's updated hook.
*/
function updated(Closure|array ...$properties): void
{
if (count($properties) === 1 && array_key_exists(0, $properties)) {
$properties = $properties[0];
}
CompileContext::instance()->updated = array_merge(
CompileContext::instance()->updated,
$properties,
);
}
/**
* Register an event listener on the component.
*/
function on(Closure|array|string ...$listeners): void
{
if (count($listeners) === 1 && array_key_exists(0, $listeners)) {
$listeners = $listeners[0];
}
CompileContext::instance()->listen($listeners);
}
/**
* Define the component's validation rules.
*/
function rules(mixed ...$rules): RuleOptions
{
if (count($rules) === 1 && array_key_exists(0, $rules)) {
if ($rules[0] instanceof Closure) {
CompileContext::instance()->rules = $rules[0];
return new RuleOptions;
}
$rules = $rules[0];
}
CompileContext::instance()->rules = array_merge(
CompileContext::instance()->rules,
$rules,
);
return new RuleOptions;
}
/**
* Indicate that the component supports file uploads.
*/
function usesFileUploads(): UsesOptions
{
return (new UsesOptions)->usesFileUploads();
}
/**
* Indicate that the component supports pagination.
*/
function usesPagination(?string $view = null, ?string $theme = null): UsesOptions
{
return (new UsesOptions)->usesPagination($view, $theme);
}
/**
* Indicate that the component should use the given trait or interface.
*/
function uses(array|string $name): UsesOptions
{
$uses = Arr::wrap($name);
foreach ($uses as $use) {
if (! trait_exists($use) && ! interface_exists($use)) {
throw new TraitOrInterfaceNotFound($use);
}
}
CompileContext::instance()->uses = array_values(array_unique(array_merge(
CompileContext::instance()->uses,
Arr::wrap($name),
)));
return new UsesOptions;
}