* use Netcarver\Textile\Tag; * $img = new Tag('img'); * echo (string) $img->class('big blue')->src('images/elephant.jpg'); * */ class Tag extends DataBag { /** * The name of the tag. * * @var string */ protected $tag; /** * Whether the tag is self-closing. * * @var bool */ protected $selfclose; /** * Constructor. * * @param string $name The tag name * @param array $attributes An array of attributes * @param bool $selfclosing Whether the tag is self-closing */ public function __construct($name, array $attributes = null, $selfclosing = true) { parent::__construct($attributes); $this->tag = $name; $this->selfclose = $selfclosing; } /** * Returns the tag as HTML. * * * $img = new Tag('img'); * $img->src('images/example.jpg')->alt('Example image'); * echo (string) $img; * * * @return string A HTML element */ public function __toString() { $attributes = ''; if ($this->data) { ksort($this->data); foreach ($this->data as $name => $value) { $attributes .= " $name=\"$value\""; } } if ($this->tag) { return '<' . $this->tag . $attributes . (($this->selfclose) ? " />" : '>'); } return $attributes; } }