Skip to content

Commit

Permalink
Improve HTMLConverter::convert new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 15, 2019
1 parent c89797b commit 82b6ab5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
32 changes: 14 additions & 18 deletions src/HTMLConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,23 @@ public function __construct()
*/
public function convert($records, array $header_record = [], array $footer_record = []): string
{
$doc = new DOMDocument('1.0');
if ([] === $header_record && [] === $footer_record) {
/** @var DOMDocument $doc */
$doc = $this->xml_converter->convert($records);

/** @var DOMElement $table */
$table = $doc->getElementsByTagName('table')->item(0);
$table = $this->xml_converter->import($records, $doc);
$this->styleTableElement($table);
$doc->appendChild($table);

return $doc->saveHTML($table);
};
return $doc->saveHTML();
}

$doc = new DOMDocument('1.0');
$table = $doc->createElement('table');
$this->styleTableElement($table);
$this->appendTableHeader('thead', $header_record, $table);
$this->appendTableHeader('tfoot', $footer_record, $table);
$tbody = $this->xml_converter->rootElement('tbody')->import($records, $doc);
$table->appendChild($tbody);
$this->appendTableHeaderSection('thead', $header_record, $table);
$this->appendTableHeaderSection('tfoot', $footer_record, $table);
$table->appendChild($this->xml_converter->rootElement('tbody')->import($records, $doc));
$doc->appendChild($table);

return $doc->saveHTML($table);
return $doc->saveHTML();
}

/**
Expand Down Expand Up @@ -129,13 +125,13 @@ public function td(string $fieldname_attribute_name): self
/**
* Create a DOMElement representing a single record of data.
*/
private function appendTableHeader(string $node_name, array $record, DOMElement $table)
private function appendTableHeaderSection(string $node_name, array $record, DOMElement $table)
{
if ([] === $record) {
return;
}

$node = (new XMLConverter())
$node = $this->xml_converter
->rootElement($node_name)
->recordElement('tr')
->fieldElement('th')
Expand All @@ -153,9 +149,9 @@ private function appendTableHeader(string $node_name, array $record, DOMElement
/**
* Style the table dom element.
*/
private function styleTableElement(DOMElement $table_element)
private function styleTableElement(DOMElement $node)
{
$table_element->setAttribute('class', $this->class_name);
$table_element->setAttribute('id', $this->id_value);
$node->setAttribute('class', $this->class_name);
$node->setAttribute('id', $this->id_value);
}
}
6 changes: 3 additions & 3 deletions tests/HTMLConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testToHTML()

/**
* @covers ::convert
* @covers ::appendTableHeader
* @covers ::appendTableHeaderSection
* @covers ::styleTableElement
*/
public function testToHTMLWithTHeadTableSection()
Expand Down Expand Up @@ -97,7 +97,7 @@ public function testToHTMLWithTHeadTableSection()

/**
* @covers ::convert
* @covers ::appendTableHeader
* @covers ::appendTableHeaderSection
* @covers ::styleTableElement
*/
public function testToHTMLWithTFootTableSection()
Expand Down Expand Up @@ -133,7 +133,7 @@ public function testToHTMLWithTFootTableSection()

/**
* @covers ::convert
* @covers ::appendTableHeader
* @covers ::appendTableHeaderSection
* @covers ::styleTableElement
*/
public function testToHTMLWithBothTableHeaderSection()
Expand Down

0 comments on commit 82b6ab5

Please sign in to comment.