Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class T {
- private $html;
- public function __call($tag, $args) {
- $html = '';
- $content = '';
- $attributes = [];
- $childs = [];
- foreach($args as $arg){
- if (is_string($arg)){
- $content = $arg;
- } elseif (is_array($arg)){
- if ($this->is_assoc_array($arg)){
- $attributes = $arg;
- } else {
- $childs = $arg;
- }
- } elseif (is_callable($arg)){
- $childs = $arg();
- }
- }
- $html .= "<{$tag}";
- if ($attributes) {
- $attr_str = ' ';
- foreach($attributes as $name=>$val){
- $attr_str .= "{$name}=\"{$val}\" ";
- }
- $html .= rtrim($attr_str);
- }
- $html .= '>';
- if ($content){
- if ($childs){
- $html .= "\n\t";
- }
- $html .= $content;
- }
- if ($childs){
- foreach($childs as $child){
- $child_rows = explode("\n", $child);
- foreach($child_rows as $row){
- $html .= "\n\t" . $row;
- }
- }
- $html .= "\n";
- }
- if ($content || $childs) {
- $html .= "</{$tag}>";
- }
- return $html;
- }
- public function create($html){
- return $this->html = $html;
- }
- public function dump(){
- return $this->html;
- }
- private function is_assoc_array($array){
- return array_keys($array) !== range(0, count($array) - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement