<?php
namespace PHPFUI;
class Input extends \PHPFUI\HTML5Element
{
protected bool $disabled = false;
protected string $placeholder = '';
protected string $type = '';
private static array $validInputs = [
'button',
'checkbox',
'color',
'date',
'datetime-local',
'email',
'file',
'hidden',
'image',
'month',
'number',
'password',
'radio',
'range',
'reset',
'search',
'submit',
'tel',
'text',
'time',
'url',
'week',
];
public function __construct(string $type, protected string $name, protected ?string $value = '')
{
parent::__construct('input');
$this->type = \strtolower($type);
if (! \in_array($type, self::$validInputs))
{
throw new \Exception("{$type} is not a valid HTML input type.");
}
}
public function getDisabled() : bool
{
return null !== $this->getAttribute('disabled');
}
public function getName() : string
{
return $this->name;
}
public function getPlaceholder() : string
{
return $this->placeholder;
}
public function getType() : string
{
return $this->type;
}
public function getValue() : string
{
return (string)$this->value;
}
public function setDisabled(bool $disabled = true) : static
{
if ($disabled)
{
$this->setAttribute('disabled');
}
else
{
$this->deleteAttribute('disabled');
}
return $this;
}
public function setName(string $name) : static
{
$this->name = $name;
return $this;
}
public function setPlaceholder(string $placeholder) : static
{
$this->placeholder = $placeholder;
return $this;
}
public function setValue(string $value) : static
{
$this->value = $value;
return $this;
}
protected function getStart() : string
{
if ($this->name)
{
$this->setAttribute('name', $this->name);
}
$this->setAttribute('type', $this->type);
if (null !== $this->value)
{
$this->setAttribute('value', \str_replace("'", ''', $this->value));
}
if ($this->placeholder)
{
$this->setAttribute('placeholder', \str_replace("'", ''', $this->placeholder));
}
return parent::getStart();
}
}