Gajus\Dora\Input::toString PHP Method

toString() public method

public toString ( )
    public function toString()
    {
        if ($this->is_stringified) {
            throw new Exception\LogicException('Input has already been stringified.');
        }
        if ($this->template) {
            $template = $this->template;
            $this->template = null;
            $template = new $template($this);
            return $template->toString();
        }
        $this->is_stringified = true;
        $value = $this->getValue();
        $attributes_string = $this->stringifyAttributes();
        switch ($this->attributes['type']) {
            case 'select':
                if (!isset($this->properties['options'])) {
                    $this->properties['options'] = [];
                }
                $options_string = '';
                $placeholder = null;
                $has_selected_option = false;
                if (isset($this->properties['options'][0])) {
                    $placeholder = $this->properties['options'][0];
                    unset($this->properties['options'][0]);
                }
                foreach ($this->properties['options'] as $v => $l) {
                    $selected = '';
                    if (is_array($value) && in_array($v, $value) || !is_null($value) && $value == $v) {
                        $selected = ' selected="selected"';
                        $has_selected_option = true;
                    }
                    $options_string .= '<option value="' . $v . '"' . $selected . '>' . $l . '</option>';
                }
                if ($placeholder) {
                    if ($has_selected_option) {
                        $options_string = '<option disabled="disabled">' . $placeholder . '</option>' . $options_string;
                    } else {
                        $options_string = '<option selected="selected" disabled="disabled">' . $placeholder . '</option>' . $options_string;
                    }
                }
                $input = '<select ' . $attributes_string . '>' . $options_string . '</select>';
                break;
            case 'checkbox':
            case 'radio':
            case 'password':
                $input = '<input ' . $attributes_string . '>';
                break;
            default:
                if ($value === null && isset($this->attributes['value'])) {
                    $value = $this->attributes['value'];
                }
                if ($this->attributes['type'] === 'textarea') {
                    $input = '<textarea ' . $attributes_string . '>' . filter_var($value, \FILTER_SANITIZE_SPECIAL_CHARS) . '</textarea>';
                } else {
                    $input = '<input ' . $attributes_string . ' value="' . filter_var($value, \FILTER_SANITIZE_SPECIAL_CHARS) . '">';
                }
                break;
        }
        return $input;
    }