Files
picea-ui/src/Form/UiInput.php
T

79 lines
2.1 KiB
PHP

<?php
namespace Picea\Ui\Form;
use Picea\Ui\Common\UiElement;
use Picea\Extension\{ Extension, FunctionExtension, ExtensionTrait };
class UiInput extends UiElement implements Extension, FunctionExtension {
use ExtensionTrait;
public string $token = "ui:input";
public array $attributes = [
'class' => 'ui-input',
];
public array $options = [
'tag-type' => "single",
];
protected mixed $value;
protected string $name;
public function __construct(
public string $tag = "input",
) {}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
return "<?php echo ( new \\" . static::class . "() )->buildHtml($arguments) ?>";
}
public function exportFunctions(): array
{
if ( null === $type = $this->attributes['type'] ?? null ) {
return [];
}
$key = str_replace('-', '_', 'ui_'. ( $this->attributes['type'] ?? 'text' ));
return [
$key => fn(string $name, mixed $value = null, array $attributes = [], array $options = []) : string => $this->buildHtml($name, $value, $attributes, $options),
];
}
public function buildHtml(string $name, mixed $value = null, array $attributes = [], array $options = []) : string
{
$this->name = $name;
if (null !== $this->value = $value) {
$this->setValue($value);
}
if ($options) {
$this->options += $options;
}
if ($attributes['class'] ?? false) {
$attributes['class'] = implode(" ", array_merge((array) $attributes['class'], (array) $this->attributes['class']));
unset($this->attributes['class']);
}
$this->attributes([ 'name' => $name ] + $attributes + $this->attributes + $this->objectAttribute());
return $this->render();
}
protected function setValue($value) : void
{
$this->attributes['value'] = $value;
}
protected function objectAttribute() : array
{
return [];
}
}