label(fn (StartupVariable $component) => $component->getVariableName()); $this->prefix(fn (StartupVariable $component) => '{{' . $component->getVariableEnv() . '}}'); $this->hintIcon('tabler-code'); $this->hintIconTooltip(fn (StartupVariable $component) => implode('|', $component->getVariableRules())); $this->helperText(fn (StartupVariable $component) => !$component->getVariableDesc() ? '—' : $component->getVariableDesc()); $this->rules(fn (StartupVariable $component) => $component->getVariableRules()); $this->placeholder(fn (StartupVariable $component) => $component->getVariableDefault()); $this->live(onBlur: true); } /** * @return StateCast[] */ public function getDefaultStateCasts(): array { return match ($this->getType()) { StartupVariableType::Number => [ ...parent::getDefaultStateCasts(), app(NumberStateCast::class, ['isNullable' => false]), ], StartupVariableType::Toggle => [ ...parent::getDefaultStateCasts(), app(BooleanStateCast::class, ['isNullable' => false]), ], default => parent::getDefaultStateCasts() }; } public function fromForm(): static { $this->variableName(fn (Get $get) => $get('name')); $this->variableDesc(fn (Get $get) => $get('description')); $this->variableEnv(fn (Get $get) => $get('env_variable')); $this->variableDefault(fn (Get $get) => $get('default_value')); $this->variableRules(fn (Get $get) => $get('rules')); return $this; } public function fromRecord(): static { $this->variableName(fn (ServerVariable $record) => $record->variable->name); $this->variableDesc(fn (ServerVariable $record) => $record->variable->description); $this->variableEnv(fn (ServerVariable $record) => $record->variable->env_variable); $this->variableDefault(fn (ServerVariable $record) => $record->variable->default_value); $this->variableRules(fn (ServerVariable $record) => $record->variable->rules); return $this; } public function variableName(string|Closure|null $name): static { $this->variableName = $name; return $this; } public function variableDesc(string|Closure|null $desc): static { $this->variableDesc = $desc; return $this; } public function variableEnv(string|Closure|null $envVariable): static { $this->variableEnv = $envVariable; return $this; } public function variableDefault(string|Closure|null $default): static { $this->variableDefault = $default; return $this; } /** @param string[]|Closure|null $rules */ public function variableRules(array|Closure|null $rules): static { $this->variableRules = $rules; return $this; } public function getVariableName(): ?string { return $this->evaluate($this->variableName); } public function getVariableDesc(): ?string { return $this->evaluate($this->variableDesc); } public function getVariableEnv(): ?string { return $this->evaluate($this->variableEnv); } public function getVariableDefault(): ?string { return $this->evaluate($this->variableDefault); } /** @return string[] */ public function getVariableRules(): array { return (array) ($this->evaluate($this->variableRules) ?? []); } public function isRequired(): bool { $rules = $this->getVariableRules(); return in_array('required', $rules); } public function getMinValue(): ?int { $rules = $this->getVariableRules(); $minRule = Arr::first($rules, fn ($value) => str($value)->startsWith('min:')); if ($minRule) { return str($minRule) ->after('min:') ->trim() ->toInteger(); } return null; } public function getMaxValue(): ?int { $rules = $this->getVariableRules(); $maxRule = Arr::first($rules, fn ($value) => str($value)->startsWith('max:')); if ($maxRule) { return str($maxRule) ->after('max:') ->trim() ->toInteger(); } return null; } public function getType(): StartupVariableType { $rules = $this->getVariableRules(); if (Arr::first($rules, fn ($value) => str($value)->startsWith('in:'), false)) { return StartupVariableType::Select; } if (in_array('boolean', $rules)) { return StartupVariableType::Toggle; } if (in_array('numeric', $rules) || in_array('integer', $rules)) { return StartupVariableType::Number; } return StartupVariableType::Text; } /** @return string[] */ public function getSelectOptions(): array { $rules = $this->getVariableRules(); $inRule = Arr::first($rules, fn ($value) => str($value)->startsWith('in:')); if ($inRule) { return str($inRule) ->after('in:') ->explode(',') ->each(fn ($value) => Str::trim($value)) ->all(); } return []; } }