openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Csv2HtmlFilter.class.php (757B)


      1 <?php
      2 
      3 
      4 namespace cms\generator\filter;
      5 
      6 
      7 class Csv2HtmlFilter extends AbstractFilter
      8 {
      9 	public $withHeader = false;
     10 	public $delimiter = ';';
     11 	public $enclosure = '"';
     12 
     13 	public function filter( $value )
     14 	{
     15 		$outputRow = function( $line, $cellTag) {
     16 			return "<tr><$cellTag>".implode("</$cellTag><$cellTag>",str_getcsv( $line,$this->delimiter,$this->enclosure ))."</$cellTag></tr>";
     17 
     18 		};
     19 
     20 		$lines = explode("\n",$value );
     21 
     22 		$out = '';
     23 		$out .= '<table>';
     24 
     25 		if   ( $this->withHeader && $lines )
     26 		{
     27 			$out .= '<thead>';
     28 			$out .= $outputRow( array_shift($lines),'th');
     29 			$out .= '</thead>';
     30 		}
     31 
     32 
     33 		$out .= '<tbody>';
     34 		foreach( $lines as $line )
     35 			$out .= $outputRow( $line,'td');
     36 		$out .= '</tbody>';
     37 
     38 		$out .= '</table>';
     39 
     40 		return $out;
     41 	}
     42 }