attr[$method] = $parameters[0] ?? NULL; return $this; } /** * Magic Constructor */ public function __construct() { $this->html = Singleton::class('ZN\Hypertext\Html'); } /** * Sets attributes * * @param array $attributes * * @return Table */ public function attr(array $attributes) : Table { foreach( $attributes as $att => $val ) { $this->attr[$att] = $val; } return $this; } /** * Sets table cell * * @param int $spacing * @param int $padding * * @return Table */ public function cell(int $spacing, int $padding) : Table { $this->attr['cellspacing'] = $spacing; $this->attr['cellpadding'] = $padding; return $this; } /** * Sets table border * * @param int $border * @param string $color = NULL * * @return Table */ public function border(int $border, ?string $color = NULL) : Table { $this->attr['border'] = $border; if( ! empty($color) ) { $this->attr['bordercolor'] = $color; } return $this; } /** * Sets table size * * @param int $width * @param int $height * * @return Table */ public function size(int $width, int $height) : Table { $this->attr['width'] = $width; $this->attr['height'] = $height; return $this; } /** * Sets table style attribute * * @param array $attributes * * @return Table */ public function style(array $attributes) : Table { $attribute = ''; foreach( $attributes as $key => $values ) { if( is_numeric($key) ) { $key = $values; } $attribute .= ' '.$key.':'.$values.';'; } $this->attr['style'] = $attribute; return $this; } /** * Creates table * * @param string ...$elements * * @return string */ public function create(...$elements) : string { $table = 'html->attributes($this->attr).'>'; $table .= $this->_content(...$elements); $table .= ''; if( ! empty($this->attr)) $this->attr = []; return $table; } /** * Protected Content */ protected function _content(...$elements) { $colNo = 1; $rowNo = 1; $table = ''; $eol = EOL; if( isset($elements[0][0]) && is_array($elements[0][0])) { $elements = $elements[0]; // @codeCoverageIgnore } foreach( $elements as $key => $element ) { $table .= $eol."\t".''.$eol; if(is_array($element))foreach($element as $k => $v) { $val = $v; $attr = ""; if(is_array($v)) { $attr = $this->html->attributes($v); $val = $k; } if( strpos($val, 'th:') === 0 ) { $rowType = 'th'; // @codeCoverageIgnore $val = substr($val, 3); // @codeCoverageIgnore } else { $rowType = 'td'; } $table .= "\t\t".'<'.$rowType.$attr.'>'.$val.''.$eol; $colNo++; } $table .= "\t".''.$eol; $rowNo++; } return $table; } }