Texy\HtmlElement::startTag PHP Method

startTag() public method

Returns element's start tag.
public startTag ( ) : string
return string
    public function startTag()
    {
        if (!$this->name) {
            return '';
        }
        $s = '<' . $this->name;
        if (is_array($this->attrs)) {
            foreach ($this->attrs as $key => $value) {
                // skip NULLs and false boolean attributes
                if ($value === NULL || $value === FALSE) {
                    continue;
                }
                // true boolean attribute
                if ($value === TRUE) {
                    // in XHTML must use unminimized form
                    if (self::$xhtml) {
                        $s .= ' ' . $key . '="' . $key . '"';
                    } else {
                        $s .= ' ' . $key;
                    }
                    continue;
                } elseif (is_array($value)) {
                    // prepare into temporary array
                    $tmp = NULL;
                    foreach ($value as $k => $v) {
                        // skip NULLs & empty string; composite 'style' vs. 'others'
                        if ($v == NULL) {
                            continue;
                        } elseif (is_string($k)) {
                            $tmp[] = $k . ':' . $v;
                        } else {
                            $tmp[] = $v;
                        }
                    }
                    if (!$tmp) {
                        continue;
                    }
                    $value = implode($key === 'style' ? ';' : ' ', $tmp);
                } else {
                    $value = (string) $value;
                }
                // add new attribute
                $value = str_replace(['&', '"', '<', '>', '@'], ['&amp;', '&quot;', '&lt;', '&gt;', '&#64;'], $value);
                $s .= ' ' . $key . '="' . Helpers::freezeSpaces($value) . '"';
            }
        }
        // finish start tag
        if (self::$xhtml && $this->isEmpty) {
            return $s . ' />';
        }
        return $s . '>';
    }