default = $default; return $this; } public function rules(array $rules): static { $this->rules = $rules; return $this; } public function options(array|Closure $options): static { $this->options = $options; return $this; } public function attributes(array $attributes): static { $this->attributes = array_merge($this->attributes, $attributes); return $this; } public function setContextKey(array|string $contextKey): static { $this->contextKey = array_merge($this->contextKey, (array) $contextKey); return $this; } public function withContext(array $context): static { $this->context = array_merge($this->context, $context); return $this; } public function select2(array $config = []): static { $this->attributes(['x-select2' => json_encode($config)]); return $this; } public function live(): static { $this->live = true; return $this; } public function dependsOn(string $depends): static { $this->depends = $depends; return $this; } public function isLive(): bool { return $this->live; } public function resolveOptions(array $extraContext = []): array|Collection { if ($this->options instanceof Closure) { $mergedContext = array_merge($this->context, $extraContext); if(!empty($this->contextKey)) { $temporaryContext = []; foreach($this->contextKey as $key) { $temporaryContext[$key] = !empty($mergedContext[$key]) ? $mergedContext[$key] : null; } $mergedContext = $temporaryContext; } return ($this->options)($mergedContext); } return $this->options; } public function getAttributes(array $extraContext = []): array { $attributes = $this->attributes; if ($this->type->isSelect()) { $attributes['options'] = $this->resolveOptions($extraContext); } return $attributes; } }