Romanovich095

Untitled

Dec 26th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Class HtmlTable
  5.  */
  6. class HtmlTable
  7. {
  8.  
  9.     /**
  10.      * Table data array
  11.      *
  12.      * @var
  13.      */
  14.     private $_table = [];
  15.  
  16.     /**
  17.      * Overall header rows count
  18.      *
  19.      * @var int
  20.      */
  21.     private $_headerRowsCount = 0;
  22.  
  23.     /**
  24.      * @var array
  25.      */
  26.     private $_currentRowCells = [];
  27.  
  28.     /**
  29.      * HtmlTable constructor.
  30.      * @param string $w
  31.      * @param bool $class
  32.      */
  33.     public function __construct($w = '100%', $class = false)
  34.     {
  35.         $this->_table[] = "<table style='min-width:{$w};'" . ($class ? " class=\"{$class}\"" : '') . ">";
  36.     }
  37.  
  38.     /**
  39.      * @param array $headerRows
  40.      * @return $this
  41.      */
  42.     public function writeTableHeader($headerRows = [])
  43.     {
  44.         if ($headerRows) {
  45.             $this->_headerRowsCount = count($headerRows);
  46.             array_push($this->_table, "\t<thead>\r\n\t\t<tr>");
  47.  
  48.             foreach ($headerRows as $row) {
  49.                 array_push($this->_table, "\t\t\t<th>{$row}</th>");
  50.             }
  51.             array_push($this->_table, "\t\t</tr>\n\t</thead>");
  52.         }
  53.  
  54.         return $this;
  55.     }
  56.  
  57.     /**
  58.      * @return $this
  59.      */
  60.     public function writeRow()
  61.     {
  62.         if ($this->_currentRowCells != []) {
  63.             $this->closeRow();
  64.         }
  65.  
  66.         $this->_currentRowCells[] = "\t<tr>";
  67.         return $this;
  68.     }
  69.  
  70.     /**
  71.      *
  72.      */
  73.     private function closeRow()
  74.     {
  75.         array_push($this->_currentRowCells, "\t</tr>");
  76.         array_push($this->_table, implode("\r\n", $this->_currentRowCells));
  77.         $this->_currentRowCells = [];
  78.     }
  79.  
  80.     /**
  81.      * @param null $data
  82.      * @return $this
  83.      * @throws Exception
  84.      */
  85.     public function writeCell($data = null)
  86.     {
  87.         if (in_array(count($this->_currentRowCells), range(0, $this->_headerRowsCount)) || $this->_headerRowsCount == 0) {
  88.             $this->_currentRowCells[] = "\t\t<td>$data\n\t\t</td>";
  89.             return $this;
  90.         }
  91.  
  92.         throw new Exception('Cell is out of bounds');
  93.     }
  94.  
  95.     /**
  96.      * @return string
  97.      */
  98.     public function getTable()
  99.     {
  100.         $this->closeRow();
  101.         array_push($this->_table, '</table>');
  102.         return implode("\r\n", $this->_table);
  103.     }
  104. }
  105.  
  106.  
  107. $htmlTableBuilder = (new HtmlTable('100%', 'table table-responsive'))
  108.     ->writeTableHeader(['id', 'name', 'surname', 'salary']);
  109.  
  110. $htmlTableBuilder
  111.     ->writeRow()
  112.     ->writeCell(1)
  113.     ->writeCell('Data 1')
  114.     ->writeCell('Data 2')
  115.     ->writeCell('$ 450');
  116.  
  117. $htmlTableBuilder
  118.     ->writeRow()
  119.     ->writeCell(2)
  120.     ->writeCell('Data 1')
  121.     ->writeCell('Data 2')
  122.     ->writeCell('$ 275');
  123.  
  124. file_put_contents('./html_table.html', $htmlTableBuilder->getTable());
Advertisement
Add Comment
Please, Sign In to add comment