Completed
Pull Request — master (#348)
by
unknown
15:39 queued 32s
created

HTMLConverter::td()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
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
     * @param array             $headerRecord An optional array of headers to output to the table using `<thead>` and `<th>` elements
63 3
     * @param array             $footerRecord An optional array of footers to output to the table using `<tfoot>` and `<th>` elements
64 3
     *
65 3
     * @return string
66
     */
67 3
    public function convert($records, array $headerRecord = [], array $footerRecord = []): string
68
    {
69
        if ([] === $headerRecord && [] === $footerRecord) {
70
            /** @var DOMDocument $doc */
71
            $doc = $this->xml_converter->convert($records);
72
73
            /** @var DOMElement $table */
74
            $table = $doc->getElementsByTagName('table')->item(0);
75 6
            $table = $this->styleTableElement($table);
76
77 6
            return $doc->saveHTML($table);
78 3
        };
79
80 3
        $doc = new DOMDocument('1.0');
81 3
82 3
        $thead = $this->createRecordRow('thead', 'th', $headerRecord, $doc);
83
        $tfoot = $this->createRecordRow('tfoot', 'th', $footerRecord, $doc);
84 3
        $tbody = $this->xml_converter->rootElement('tbody')->import($records, $doc);
0 ignored issues
show
Documentation introduced by
$records is of type array|object<Traversable>, but the function expects a object<League\Csv\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
        $table = $doc->createElement('table');
86
        $table = $this->styleTableElement($table);
87
88
        $table->appendChild($thead);
89
        $table->appendChild($tbody);
90 3
        $table->appendChild($tfoot);
91
92 3
        $doc->appendChild($table);
93 3
94
        return $doc->saveHTML();
95 3
    }
96
97
    /**
98
     * HTML table class name setter.
99
     *
100
     * @throws DOMException if the id_value contains any type of whitespace
101 3
     */
102
    public function table(string $class_name, string $id_value = ''): self
103 3
    {
104 3
        if (preg_match(",\s,", $id_value)) {
105
            throw new DOMException("the id attribute's value must not contain whitespace (spaces, tabs etc.)");
106 3
        }
107
        $clone = clone $this;
108
        $clone->class_name = $class_name;
109
        $clone->id_value = $id_value;
110
111
        return $clone;
112
    }
113
114
    /**
115
     * HTML tr record offset attribute setter.
116
     */
117
    public function tr(string $record_offset_attribute_name): self
118
    {
119
        $clone = clone $this;
120
        $clone->xml_converter = $this->xml_converter->recordElement('tr', $record_offset_attribute_name);
121
122
        return $clone;
123
    }
124
125
    /**
126
     * HTML td field name attribute setter.
127
     */
128
    public function td(string $fieldname_attribute_name): self
129
    {
130
        $clone = clone $this;
131
        $clone->xml_converter = $this->xml_converter->fieldElement('td', $fieldname_attribute_name);
132
133
        return $clone;
134
    }
135
136
    /**
137
     * Create a DOMElement representing a single record of data
138
     *
139
     * @param string $recordTagName
140
     * @param string $fieldTagName
141
     * @param array $record
142
     * @param DOMDocument $doc
143
     *
144
     * @return DOMElement
145
     */
146
    private function createRecordRow(string $recordTagName, string $fieldTagName, array $record, DOMDocument $doc) : DOMElement
147
    {
148
        $node = $this->xml_converter->rootElement($recordTagName)->fieldElement($fieldTagName)->import([$record], $doc);
0 ignored issues
show
Documentation introduced by
array($record) is of type array<integer,array,{"0":"array"}>, but the function expects a object<League\Csv\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
150
        /** @var DOMElement $element */
151
        foreach ($node->getElementsByTagName($fieldTagName) as $element) {
152
            $element->setAttribute('scope', 'col');
153
        }
154
155
        return $node;
156
    }
157
158
    /**
159
     * Style the table dom element
160
     *
161
     * @param DOMElement $table_element
162
     *
163
     * @return DOMElement
164
     */
165
    private function styleTableElement(DOMElement $table_element) : DOMElement
166
    {
167
        $table_element->setAttribute('class', $this->class_name);
168
        $table_element->setAttribute('id', $this->id_value);
169
        return $table_element;
170
    }
171
}
172