Skip to content

Commit

Permalink
Prepare 9.3.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 16, 2019
1 parent da1a11b commit 3ae0efd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 60 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ All Notable changes to `Csv` will be documented in this file
### Added

- `XMLConverter::import` see [#348](https://github.com/thephpleague/csv/pull/348) thanks [@kusabi](https://github.com/kusabi)
- support for `thead`, `tfoot` and `tbody` in footer `HTMLConverter::convert` see [#348](https://github.com/thephpleague/csv/pull/348) thanks [@kusabi](https://github.com/kusabi)
- Support for `thead`, `tfoot` and `tbody` in `HTMLConverter::convert` via the addition of
protected methods `HTMLConverter::addHTMLAttributes` and `HTMLConverter::appendHeaderSection` [#348](https://github.com/thephpleague/csv/pull/348) thanks [@kusabi](https://github.com/kusabi)

### Deprecated

- Nothing

### Fixed

- Internal improvement in `Reader` dockblock thanks [@ghobaty](https://github.com/ghobaty).
- Internal improvement in `Reader` dockblock thanks [@ghobaty](https://github.com/ghobaty).
- Improve strict comparison when using `preg_match`.
- Improve CSV control in `Stream`.

### Removed

Expand Down
78 changes: 39 additions & 39 deletions src/HTMLConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,66 @@ public function __construct()
}

/**
* Convert an Record collection into a DOMDocument.
* Converts a tabular data collection into a HTML table string.
*
* @param array|Traversable $records The tabular data collection
* @param string[] $header_record An optional array of headers to output to the table using `<thead>` and `<th>` elements
* @param string[] $header_record An optional array of headers outputted using the`<thead>` section
* @param string[] $footer_record An optional array of footers to output to the table using `<tfoot>` and `<th>` elements
*/
public function convert($records, array $header_record = [], array $footer_record = []): string
{
$doc = new DOMDocument('1.0');
if ([] === $header_record && [] === $footer_record) {
$table = $this->xml_converter->import($records, $doc);
$this->styleTableElement($table);
$this->addHTMLAttributes($table);
$doc->appendChild($table);

return $doc->saveHTML();
}

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

return $doc->saveHTML();
}

/**
* Creates a DOMElement representing a HTML table heading section.
*/
protected function appendHeaderSection(string $node_name, array $record, DOMElement $table)
{
if ([] === $record) {
return;
}

$node = $this->xml_converter
->rootElement($node_name)
->recordElement('tr')
->fieldElement('th')
->import([$record], $table->ownerDocument)
;

/** @var DOMElement $element */
foreach ($node->getElementsByTagName('th') as $element) {
$element->setAttribute('scope', 'col');
}

$table->appendChild($node);
}

/**
* Adds class and id attributes to an HTML tag.
*/
protected function addHTMLAttributes(DOMElement $node)
{
$node->setAttribute('class', $this->class_name);
$node->setAttribute('id', $this->id_value);
}

/**
* HTML table class name setter.
*
Expand Down Expand Up @@ -121,37 +154,4 @@ public function td(string $fieldname_attribute_name): self

return $clone;
}

/**
* Create a DOMElement representing a single record of data.
*/
private function appendTableHeaderSection(string $node_name, array $record, DOMElement $table)
{
if ([] === $record) {
return;
}

$node = $this->xml_converter
->rootElement($node_name)
->recordElement('tr')
->fieldElement('th')
->import([$record], $table->ownerDocument)
;

/** @var DOMElement $element */
foreach ($node->getElementsByTagName('th') as $element) {
$element->setAttribute('scope', 'col');
}

$table->appendChild($node);
}

/**
* Style the table dom element.
*/
private function styleTableElement(DOMElement $node)
{
$node->setAttribute('class', $this->class_name);
$node->setAttribute('id', $this->id_value);
}
}
26 changes: 13 additions & 13 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use SplFileObject;
use TypeError;
use function array_keys;
use function array_values;
use function array_walk_recursive;
use function fclose;
use function feof;
Expand Down Expand Up @@ -264,18 +263,19 @@ public function setCsvControl(string $delimiter = ',', string $enclosure = '"',
*/
protected function filterControl(string $delimiter, string $enclosure, string $escape, string $caller): array
{
$controls = ['delimiter' => $delimiter, 'enclosure' => $enclosure, 'escape' => $escape];
foreach ($controls as $type => $control) {
if ('escape' === $type && '' === $control && 70400 <= PHP_VERSION_ID) {
continue;
}

if (1 !== strlen($control)) {
throw new Exception(sprintf('%s() expects %s to be a single character', $caller, $type));
}
if (1 !== strlen($delimiter)) {
throw new Exception(sprintf('%s() expects delimiter to be a single character', $caller));
}

return array_values($controls);
if (1 !== strlen($enclosure)) {
throw new Exception(sprintf('%s() expects enclosure to be a single character', $caller));
}

if (1 === strlen($escape) || ('' === $escape && 70400 <= PHP_VERSION_ID)) {
return [$delimiter, $enclosure, $escape];
}

throw new Exception(sprintf('%s() expects escape to be a single character', $caller));
}

/**
Expand Down Expand Up @@ -423,11 +423,11 @@ public function seek($position)
$this->current();
$this->next();
}

if (0 !== $position) {
$this->offset--;
}

$this->current();
}

Expand Down
12 changes: 6 additions & 6 deletions tests/HTMLConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function testToHTML()

/**
* @covers ::convert
* @covers ::appendTableHeaderSection
* @covers ::styleTableElement
* @covers ::appendHeaderSection
* @covers ::addHTMLAttributes
*/
public function testToHTMLWithTHeadTableSection()
{
Expand Down Expand Up @@ -97,8 +97,8 @@ public function testToHTMLWithTHeadTableSection()

/**
* @covers ::convert
* @covers ::appendTableHeaderSection
* @covers ::styleTableElement
* @covers ::appendHeaderSection
* @covers ::addHTMLAttributes
*/
public function testToHTMLWithTFootTableSection()
{
Expand Down Expand Up @@ -133,8 +133,8 @@ public function testToHTMLWithTFootTableSection()

/**
* @covers ::convert
* @covers ::appendTableHeaderSection
* @covers ::styleTableElement
* @covers ::appendHeaderSection
* @covers ::addHTMLAttributes
*/
public function testToHTMLWithBothTableHeaderSection()
{
Expand Down

0 comments on commit 3ae0efd

Please sign in to comment.