caption = Element::create('caption')->addContent( $content ); return $this; } /** * Static helper creating a tr element * * @param Attributes|array $attributes * @param Html|array|string $content * @return Element */ public static function tr($content = null, $attributes = null) { return Element::create('tr', $attributes, $content); } /** * Static helper creating a th element * * @param Attributes|array $attributes * @param Html|array|string $content * @return Element */ public static function th($content = null, $attributes = null) { return Element::create('th', $attributes, $content); } /** * Static helper creating a td element * * @param Attributes|array $attributes * @param Html|array|string $content * @return Element */ public static function td($content = null, $attributes = null) { return Element::create('td', $attributes, $content); } public function generateHeader() { return Element::create('thead')->add( $this->addHeaderColumnsTo(static::tr()) ); } public function generateFooter() { return Element::create('tfoot')->add( $this->addHeaderColumnsTo(static::tr()) ); } protected function addHeaderColumnsTo(Element $parent) { foreach ($this->getColumnsToBeRendered() as $column) { $parent->add( Element::create('th')->setContent($column) ); } return $parent; } /** * @return null|array */ public function getColumnsToBeRendered() { return $this->columnsToBeRendered; } public function setColumnsToBeRendered(array $columns) { $this->columnsToBeRendered = $columns; return $this; } public function renderRow($row) { $tr = $this->addRowClasses(Element::create('tr'), $row); $columns = $this->getColumnsToBeRendered(); if ($columns === null) { $this->setColumnsToBeRendered(array_keys((array) $row)); $columns = $this->getColumnsToBeRendered(); } foreach ($columns as $column) { $td = static::td(); if (property_exists($row, $column)) { $td->setContent($row->$column); } $tr->add($td); } return $tr; } public function addRowClasses(Element $tr, $row) { $classes = $this->getRowClasses($row); if (! empty($classes)) { $tr->attributes()->add('class', $classes); } return $tr; } public function renderRows(Traversable $rows) { $body = $this->body(); foreach ($rows as $row) { $body->add($this->renderRow($row)); } return $body; } public function body() { if ($this->body === null) { $this->body = Element::create('tbody')->setSeparator("\n"); } return $this->body; } public function header() { if ($this->header === null) { $this->header = $this->generateHeader(); } return $this->header; } public function footer() { if ($this->footer === null) { $this->footer = $this->generateFooter(); } return $this->footer; } public function renderContent() { if (null !== $this->caption) { $this->add($this->caption); } if (null !== $this->header) { $this->add($this->header); } if (null !== $this->body) { $this->add($this->body()); } if (null !== $this->footer) { $this->add($this->footer); } return parent::renderContent(); } }