Completed
Pull Request — master (#348)
by
unknown
12:57
created

HTMLConverter::headers()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
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 6
     * New Instance.
48
     */
49 6
    public function __construct()
50 6
    {
51 6
        $this->xml_converter = (new XMLConverter())
52 6
            ->rootElement('table')
53
            ->recordElement('tr')
54 6
            ->fieldElement('td')
55
        ;
56
    }
57
58
    /**
59
     * Convert an Record collection into a DOMDocument.
60
     *
61 3
     * @param array|Traversable $records the tabular data collection
62
     */
63 3
    public function convert($records): string
64 3
    {
65 3
        /** @var DOMDocument $doc */
66
        $doc = $this->xml_converter->convert($records);
67 3
68
        /** @var DOMElement $table */
69
        $table = $doc->getElementsByTagName('table')->item(0);
70
        $table->setAttribute('class', $this->class_name);
71
        $table->setAttribute('id', $this->id_value);
72
73
        return $doc->saveHTML($table);
74
    }
75 6
76
    /**
77 6
     * HTML table class name setter.
78 3
     *
79
     * @throws DOMException if the id_value contains any type of whitespace
80 3
     */
81 3
    public function table(string $class_name, string $id_value = ''): self
82 3
    {
83
        if (preg_match(",\s,", $id_value)) {
84 3
            throw new DOMException("the id attribute's value must not contain whitespace (spaces, tabs etc.)");
85
        }
86
        $clone = clone $this;
87
        $clone->class_name = $class_name;
88
        $clone->id_value = $id_value;
89
90 3
        return $clone;
91
    }
92 3
93 3
    /**
94
     * HTML tr record offset attribute setter.
95 3
     */
96
    public function tr(string $record_offset_attribute_name): self
97
    {
98
        $clone = clone $this;
99
        $clone->xml_converter = $this->xml_converter->recordElement('tr', $record_offset_attribute_name);
100
101 3
        return $clone;
102
    }
103 3
104 3
    /**
105
     * HTML td field name attribute setter.
106 3
     */
107
    public function td(string $fieldname_attribute_name): self
108
    {
109
        $clone = clone $this;
110
        $clone->xml_converter = $this->xml_converter->fieldElement('td', $fieldname_attribute_name);
111
112
        return $clone;
113
    }
114
115
    /**
116
     * Enable headers to be output with th tags instead of td
117
     *
118
     * @param bool $enabled
119
     *
120
     * @return HTMLConverter
121
     *
122
     * @throws DOMException
123
     */
124
    public function headers(bool $enabled = true): self
125
    {
126
        return $this->overrideRecordFieldElement(0, $enabled ? 'th' : 'td');
127
    }
128
129
    /**
130
     * Override the field type to use for the first record
131
     *
132
     * @param int $element_index
133
     * @param string $node_name
134
     *
135
     * @return HTMLConverter
136
     *
137
     * @throws DOMException
138
     */
139
    public function overrideRecordFieldElement(int $element_index, string $node_name): self
140
    {
141
        $clone = clone $this;
142
        $clone->xml_converter = $this->xml_converter->overrideRecordFieldElement($element_index, $node_name);
143
        return $clone;
144
    }
145
}
146