Completed
Push — master ( da1a11...3ae0ef )
by ignace nyamagana
05:14 queued 04:15
created

HTMLConverter::addHTMLAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * League.Csv (https://csv.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace League\Csv;
15
16
use DOMDocument;
17
use DOMElement;
18
use DOMException;
19
use Traversable;
20
use function preg_match;
21
22
/**
23
 * Converts tabular data into an HTML Table string.
24
 */
25
class HTMLConverter
26
{
27
    /**
28
     * table class attribute value.
29
     *
30
     * @var string
31
     */
32
    protected $class_name = 'table-csv-data';
33
34
    /**
35
     * table id attribute value.
36
     *
37
     * @var string
38
     */
39
    protected $id_value = '';
40
41
    /**
42
     * @var XMLConverter
43
     */
44
    protected $xml_converter;
45
46
    /**
47
     * New Instance.
48
     */
49 6
    public function __construct()
50
    {
51 6
        $this->xml_converter = (new XMLConverter())
52 6
            ->rootElement('table')
53 6
            ->recordElement('tr')
54 6
            ->fieldElement('td')
55
        ;
56 6
    }
57
58
    /**
59
     * Converts a tabular data collection into a HTML table string.
60
     *
61
     * @param array|Traversable $records       The tabular data collection
62
     * @param string[]          $header_record An optional array of headers outputted using the`<thead>` section
63
     * @param string[]          $footer_record An optional array of footers to output to the table using `<tfoot>` and `<th>` elements
64
     */
65 12
    public function convert($records, array $header_record = [], array $footer_record = []): string
66
    {
67 12
        $doc = new DOMDocument('1.0');
68 12
        if ([] === $header_record && [] === $footer_record) {
69 3
            $table = $this->xml_converter->import($records, $doc);
70 3
            $this->addHTMLAttributes($table);
71 3
            $doc->appendChild($table);
72
73 3
            return $doc->saveHTML();
74
        }
75
76 9
        $table = $doc->createElement('table');
77 9
        $this->addHTMLAttributes($table);
78 9
        $this->appendHeaderSection('thead', $header_record, $table);
79 9
        $this->appendHeaderSection('tfoot', $footer_record, $table);
80 9
        $table->appendChild($this->xml_converter->rootElement('tbody')->import($records, $doc));
81 9
        $doc->appendChild($table);
82
83 9
        return $doc->saveHTML();
84
    }
85
86
    /**
87
     * Creates a DOMElement representing a HTML table heading section.
88
     */
89 9
    protected function appendHeaderSection(string $node_name, array $record, DOMElement $table)
90
    {
91 9
        if ([] === $record) {
92 6
            return;
93
        }
94
95 9
        $node = $this->xml_converter
96 9
            ->rootElement($node_name)
97 9
            ->recordElement('tr')
98 9
            ->fieldElement('th')
99 9
            ->import([$record], $table->ownerDocument)
100
        ;
101
102
        /** @var DOMElement $element */
103 9
        foreach ($node->getElementsByTagName('th') as $element) {
104 9
            $element->setAttribute('scope', 'col');
105
        }
106
107 9
        $table->appendChild($node);
108 9
    }
109
110
    /**
111
     * Adds class and id attributes to an HTML tag.
112
     */
113 9
    protected function addHTMLAttributes(DOMElement $node)
114
    {
115 9
        $node->setAttribute('class', $this->class_name);
116 9
        $node->setAttribute('id', $this->id_value);
117 9
    }
118
119
    /**
120
     * HTML table class name setter.
121
     *
122
     * @throws DOMException if the id_value contains any type of whitespace
123
     */
124 6
    public function table(string $class_name, string $id_value = ''): self
125
    {
126 6
        if (1 === preg_match(",\s,", $id_value)) {
127 3
            throw new DOMException("the id attribute's value must not contain whitespace (spaces, tabs etc.)");
128
        }
129 3
        $clone = clone $this;
130 3
        $clone->class_name = $class_name;
131 3
        $clone->id_value = $id_value;
132
133 3
        return $clone;
134
    }
135
136
    /**
137
     * HTML tr record offset attribute setter.
138
     */
139 3
    public function tr(string $record_offset_attribute_name): self
140
    {
141 3
        $clone = clone $this;
142 3
        $clone->xml_converter = $this->xml_converter->recordElement('tr', $record_offset_attribute_name);
143
144 3
        return $clone;
145
    }
146
147
    /**
148
     * HTML td field name attribute setter.
149
     */
150 3
    public function td(string $fieldname_attribute_name): self
151
    {
152 3
        $clone = clone $this;
153 3
        $clone->xml_converter = $this->xml_converter->fieldElement('td', $fieldname_attribute_name);
154
155 3
        return $clone;
156
    }
157
}
158