From 3ae0efd15c3d13effbeac474fe205e23c5c86648 Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Tue, 16 Jul 2019 12:23:56 +0200 Subject: [PATCH] Prepare 9.3.0 release --- CHANGELOG.md | 7 +++- src/HTMLConverter.php | 78 ++++++++++++++++++------------------- src/Stream.php | 26 ++++++------- tests/HTMLConverterTest.php | 12 +++--- 4 files changed, 63 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8309f08c..762652ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ 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 @@ -15,7 +16,9 @@ All Notable changes to `Csv` will be documented in this file ### 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 diff --git a/src/HTMLConverter.php b/src/HTMLConverter.php index b0a4b4b7..8d841800 100644 --- a/src/HTMLConverter.php +++ b/src/HTMLConverter.php @@ -56,10 +56,10 @@ 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 `` and `` elements + * @param string[] $header_record An optional array of headers outputted using the`` section * @param string[] $footer_record An optional array of footers to output to the table using `` and `` elements */ public function convert($records, array $header_record = [], array $footer_record = []): string @@ -67,22 +67,55 @@ public function convert($records, array $header_record = [], array $footer_recor $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. * @@ -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); - } } diff --git a/src/Stream.php b/src/Stream.php index cc5d2962..e3b5d8de 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -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; @@ -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)); } /** @@ -423,11 +423,11 @@ public function seek($position) $this->current(); $this->next(); } - + if (0 !== $position) { $this->offset--; } - + $this->current(); } diff --git a/tests/HTMLConverterTest.php b/tests/HTMLConverterTest.php index 9ec8038e..4ecbbf44 100644 --- a/tests/HTMLConverterTest.php +++ b/tests/HTMLConverterTest.php @@ -61,8 +61,8 @@ public function testToHTML() /** * @covers ::convert - * @covers ::appendTableHeaderSection - * @covers ::styleTableElement + * @covers ::appendHeaderSection + * @covers ::addHTMLAttributes */ public function testToHTMLWithTHeadTableSection() { @@ -97,8 +97,8 @@ public function testToHTMLWithTHeadTableSection() /** * @covers ::convert - * @covers ::appendTableHeaderSection - * @covers ::styleTableElement + * @covers ::appendHeaderSection + * @covers ::addHTMLAttributes */ public function testToHTMLWithTFootTableSection() { @@ -133,8 +133,8 @@ public function testToHTMLWithTFootTableSection() /** * @covers ::convert - * @covers ::appendTableHeaderSection - * @covers ::styleTableElement + * @covers ::appendHeaderSection + * @covers ::addHTMLAttributes */ public function testToHTMLWithBothTableHeaderSection() {