public function __construct($name, array $attributes = null, array $properties = null, $template = null)
{
if ($properties === null) {
$properties = [];
}
$this->template = $template;
if ($this->template) {
if (!class_exists($this->template)) {
throw new Exception\LogicException('Template does not exist.');
} else {
if (!is_subclass_of($this->template, 'Gajus\\Dora\\Template')) {
throw new Exception\LogicException('Template does not extend Gajus\\Dora\\Template.');
}
}
}
$this->attributes['name'] = $name;
if ($attributes) {
foreach ($attributes as $k => $v) {
$this->setAttribute($k, $v);
}
}
foreach ($properties as $k => $v) {
$this->setProperty($k, $v);
}
if (!isset($this->attributes['type']) && isset($this->properties['options'])) {
$this->attributes['type'] = 'select';
} else {
if (!isset($this->attributes['type'])) {
$this->attributes['type'] = 'text';
}
}
if (!isset($this->properties['value'])) {
$this->properties['value'] = null;
}
if (!isset($this->properties['name'])) {
$name_path = $this->getNamePath();
$this->properties['name'] = static::deriveName($name_path);
}
if (isset($this->attributes['type']) && in_array($this->attributes['type'], ['radio', 'checkbox']) && !isset($this->attributes['value'])) {
$this->attributes['value'] = 1;
}
if (isset($this->properties['options']) && $this->attributes['type'] !== 'select') {
throw new Exception\InvalidArgumentException('[input="' . $this->attributes['type'] . '"] does not support "options" property.');
}
}