You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.8 KiB
122 lines
2.8 KiB
<?php
|
|
|
|
namespace App\UI\Support\Settings;
|
|
|
|
use App\UI\Enums\InputType;
|
|
use Closure;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class SettingSchema implements BaseSchema
|
|
{
|
|
public mixed $default = null;
|
|
public array|Closure $options = [];
|
|
public array $attributes = [];
|
|
public array $rules = ['required', 'string'];
|
|
public string $depends = '';
|
|
protected array $context = [];
|
|
protected array $contextKey = [];
|
|
protected bool $live = false;
|
|
|
|
public function __construct(
|
|
public InputType $type,
|
|
) {}
|
|
|
|
public static function make(InputType $type): self
|
|
{
|
|
return new static($type);
|
|
}
|
|
|
|
public function default(mixed $default): static
|
|
{
|
|
$this->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;
|
|
}
|
|
}
|
|
|