Created
May 12, 2024 03:11
-
-
Save audinue/116b2666bf9e26fd9830f72bb9062995 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo Tag::p()->text('Hello world!'); | |
// <p>Hello world!</p> | |
echo Tag::p()->align('center'); | |
// <p align="center"></p> | |
$form = Tag::form(); | |
$form->input() | |
->type('search') | |
->name('query'); | |
echo $form; | |
// <form><input type="search" name="query"></form> | |
echo Tag::input()->checked(true); | |
// <input checked> | |
echo Tag::myAwesomeCustomElement(); | |
// <my-awesome-custom-element></my-awesome-custom-element> | |
echo Tag::img()->dataSrc('foo.png'); | |
// <img data-src="foo.png"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @method static Tag a() | |
* @method static Tag audio() | |
* @method static Tag body() | |
* @method static Tag br() | |
* @method static Tag button() | |
* @method static Tag div() | |
* @method static Tag form() | |
* @method static Tag h1() | |
* @method static Tag h2() | |
* @method static Tag h3() | |
* @method static Tag head() | |
* @method static Tag hr() | |
* @method static Tag html() | |
* @method static Tag iframe() | |
* @method static Tag img() | |
* @method static Tag input() | |
* @method static Tag li() | |
* @method static Tag link() | |
* @method static Tag meta() | |
* @method static Tag ol() | |
* @method static Tag p() | |
* @method static Tag script() | |
* @method static Tag span() | |
* @method static Tag table() | |
* @method static Tag tbody() | |
* @method static Tag td() | |
* @method static Tag th() | |
* @method static Tag thead() | |
* @method static Tag title() | |
* @method static Tag tr() | |
* @method static Tag ul() | |
* @method static Tag video() | |
* @method Tag a() | |
* @method Tag audio() | |
* @method Tag body() | |
* @method Tag br() | |
* @method Tag button() | |
* @method Tag div() | |
* @method Tag form() | |
* @method Tag h1() | |
* @method Tag h2() | |
* @method Tag h3() | |
* @method Tag head() | |
* @method Tag hr() | |
* @method Tag html() | |
* @method Tag iframe() | |
* @method Tag img() | |
* @method Tag input() | |
* @method Tag li() | |
* @method Tag link() | |
* @method Tag meta() | |
* @method Tag ol() | |
* @method Tag p() | |
* @method Tag script() | |
* @method Tag span() | |
* @method Tag table() | |
* @method Tag tbody() | |
* @method Tag td() | |
* @method Tag th() | |
* @method Tag thead() | |
* @method Tag title() | |
* @method Tag tr() | |
* @method Tag ul() | |
* @method Tag video() | |
* @method Tag checked(bool $value) | |
* @method Tag class(string $value) | |
* @method Tag disabled(bool $value) | |
* @method Tag height(string $value) | |
* @method Tag hidden(bool $value) | |
* @method Tag href(string $value) | |
* @method Tag id(string $value) | |
* @method Tag loading(string $value) | |
* @method Tag name(string $value) | |
* @method Tag placeholder(string $value) | |
* @method Tag selected(bool $value) | |
* @method Tag src(string $value) | |
* @method Tag style(string $value) | |
* @method Tag title(string $value) | |
* @method Tag value(string $value) | |
* @method Tag width(string $value) | |
*/ | |
class Tag | |
{ | |
private $name; | |
private $attributes; | |
private $children; | |
function __construct($name) | |
{ | |
$this->name = self::camelToKebab($name); | |
$this->attributes = []; | |
$this->children = []; | |
} | |
function html($value) | |
{ | |
$this->children[] = $value; | |
return $this; | |
} | |
function text($value) | |
{ | |
$this->children[] = htmlspecialchars($value); | |
return $this; | |
} | |
function __call($name, $arguments) | |
{ | |
if (empty($arguments)) { | |
$child = new Tag($name); | |
$this->children[] = $child; | |
return $child; | |
} else { | |
$name = self::camelToKebab($name); | |
$value = $arguments[0]; | |
if (is_bool($value)) { | |
$this->attributes[$name] = $value; | |
} else { | |
$this->attributes[$name] = htmlspecialchars($arguments[0]); | |
} | |
return $this; | |
} | |
} | |
function __toString() | |
{ | |
global $__tag_void; | |
$tag = "<$this->name"; | |
foreach ($this->attributes as $name => $value) { | |
if (is_bool($value)) { | |
if ($value) { | |
$tag .= " $name"; | |
} | |
} else { | |
$tag .= " $name=\"$value\""; | |
} | |
} | |
$tag .= '>'; | |
if (in_array($this->name, $__tag_void)) { | |
return $tag; | |
} | |
foreach ($this->children as $child) { | |
$tag .= $child; | |
} | |
$tag .= "</$this->name>"; | |
return $tag; | |
} | |
static function __callStatic($name, $args) | |
{ | |
return new Tag($name); | |
} | |
private static function camelToKebab($string) | |
{ | |
return strtolower(preg_replace('/[A-Z]/', '-$0', $string)); | |
} | |
} | |
$__tag_void = [ | |
'area', | |
'base', | |
'br', | |
'col', | |
'embed', | |
'hr', | |
'img', | |
'input', | |
'link', | |
'meta', | |
'param', | |
'source', | |
'track', | |
'wbr' | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment