id = 'ars-' . trim((string) preg_replace('/[^A-Za-z0-9_-]+/', '-', $name), '-'); } /** * Cast the value to a string suitable for a single control. * * @return string */ public function scalar(): string { return self::toScalar($this->value); } /** * Return the value as a list; non-arrays become empty. * * @return array */ public function list(): array { return is_array($this->value) ? $this->value : []; } /** * Return the wrapper classes of the field. * * @return string */ public function classes(): string { return Html::classes([ 'ars-field', 'ars-field--' . $this->field->type->value, 'ars-field--w-' . $this->field->width->value, 'is-required' => $this->field->required, 'is-readonly' => $this->field->readonly, 'is-invalid' => $this->error !== '', ]); } /** * Build the escaped attribute string shared by input, textarea and select controls. * * @param array $extra Control specific attributes; null removes a default one. * @return string */ public function attributes(array $extra = []): string { return Html::attributes($this->attributeValues($extra)); } /** * Return unescaped control attributes for rendering. * * @param array $extra Control specific attributes. * @return array */ public function attributeValues(array $extra = []): array { $field = $this->field; return array_merge([ 'id' => $this->id, 'name' => $this->name, 'class' => 'ars-input', 'data-ars-key' => $field->key, 'placeholder' => $field->placeholder !== '' ? $field->placeholder : null, 'aria-describedby' => $field->help !== '' ? $this->id . '-help' : null, 'required' => $field->required, 'readonly' => $field->readonly, ], $extra, $field->attributes); } /** * Cast a stored value to a string suitable for a single control. * * @param mixed $value Stored value. * @return string */ public static function toScalar(mixed $value): string { return is_scalar($value) ? (string) $value : ''; } }