args = $args; $this->type = $args['type'] ?? $this::TYPE; $this->name = $args['name'] ?? ''; $this->label = $args['label'] ?? ''; $this->description = $args['description'] ?? ''; $this->id = $args['id'] ?? ''; $this->class = $args['class'] ?? ''; $this->labelClass = $args['label_class'] ?? ''; $this->meta = Arr::isset_and_array($args, 'meta') ? $args['meta'] : []; $this->default = $args['default'] ?? ''; $this->defaultForSave = $args['default_for_save'] ?? ''; $this->sanitize = $args['sanitize'] ?? null; $this->required = isset($args['required']) && $args['required']; } public function type(): string { return $this->type; } public function name(): string { return $this->name; } public function label(): string { return $this->label; } public function description(): string { return $this->description; } public function id(): string { return $this->id; } public function class(): string { return $this->class; } public function get(string $key) { return $this->args[$key] ?? null; } public function args(): array { return $this->args; } public function meta(): array { return $this->meta; } public function key(): string { $meta = $this->meta(); return $meta['key'] ?? $this->name(); } public function single(): bool { $meta = $this->meta(); return !isset($meta['single']) || $meta['single']; } public function required(): bool { return $this->required; } public function sanitize($value) { // Sanitize Callback if ($this->sanitize and is_callable($this->sanitize)) return call_user_func_array($this->sanitize, [$value]); // Sanitize by Type if (in_array($this->type(), ['text', 'tel', 'number', 'date', 'time', 'hidden', 'select', 'radio', 'agreement'])) return sanitize_text_field($value); else if ($this->type() === 'textarea') return sanitize_textarea_field($value); else if ($this->type() === 'url') return sanitize_url($value); else if ($this->type() === 'email') return sanitize_email($value); else if ($this->type() === 'color') return sanitize_hex_color($value); // Do not sanitize by default return $value; } public function assets() { } public function pick(array $request) { $key = $this->key(); $default = $this->default(true); return Arr::ternary($request, $key, $default); } public function default(bool $save = false) { if ($save) return $this->defaultForSave; return $this->default; } public function output(?EntityInterface $entity = null): string { $args = $this->args(); if (Arr::notset($args, 'value')) $args['value'] = $this->current($entity); return $this->html($args, $entity); } public static function html(array $args, ?EntityInterface $entity = null): string { $type = $args['type'] ?? 'text'; $args['class'] = trim('wui-input' . (Html::is_admin_context() ? ' daytidy-input ' . Html::get_daytidy_class('fields', 'admin') : '') . ' ' . (string) ($args['class'] ?? '')); return Form::input($args, $type); } public function current(?EntityInterface $entity = null) { if ($entity instanceof EntityInterface) { $key = $this->key(); $single = $this->single(); $default = $this->default(); if ($key) return $entity->meta_get($key, $default, $single); } return $this->default(); } }