Advertisement
Guest User

Untitled

a guest
May 21st, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. <?php
  2. class T {
  3.  
  4.     private $html;  
  5.  
  6.     public function __call($tag, $args) {
  7.            
  8.         $html = '';
  9.         $content = '';
  10.         $attributes = [];
  11.         $childs = [];
  12.        
  13.         foreach($args as $arg){
  14.             if (is_string($arg)){
  15.                 $content = $arg;
  16.             } elseif (is_array($arg)){
  17.                 if ($this->is_assoc_array($arg)){
  18.                     $attributes = $arg;
  19.                 } else {
  20.                     $childs = $arg;
  21.                 }
  22.             } elseif (is_callable($arg)){
  23.                 $childs = $arg();
  24.             }
  25.         }
  26.            
  27.         $html .= "<{$tag}";
  28.        
  29.         if ($attributes) {
  30.             $attr_str = ' ';
  31.             foreach($attributes as $name=>$val){
  32.                 $attr_str .= "{$name}=\"{$val}\" ";
  33.             }
  34.             $html .= rtrim($attr_str);
  35.         }
  36.        
  37.         $html .= '>';
  38.        
  39.         if ($content){
  40.             if ($childs){
  41.                 $html .= "\n\t";
  42.             }
  43.             $html .= $content;
  44.         }
  45.        
  46.         if ($childs){
  47.             foreach($childs as $child){
  48.                 $child_rows = explode("\n", $child);
  49.                 foreach($child_rows as $row){
  50.                     $html .= "\n\t" . $row;
  51.                 }
  52.             }
  53.             $html .= "\n";
  54.         }
  55.        
  56.         if ($content || $childs) {
  57.             $html .= "</{$tag}>";
  58.         }
  59.        
  60.         return $html;
  61.        
  62.     }
  63.    
  64.     public function create($html){
  65.         return $this->html = $html;
  66.     }  
  67.    
  68.     public function dump(){
  69.         return $this->html;
  70.     }
  71.    
  72.     private function is_assoc_array($array){
  73.         return array_keys($array) !== range(0, count($array) - 1);
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement