Gajus\Dora\Input::__construct PHP Method

__construct() public method

public __construct ( string $name, array $attributes = null, array $properties = null, null | string $template = null )
$name string Input name.
$attributes array HTML attributes.
$properties array Input properties, e.g. input name.
$template null | string
    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.');
        }
    }