These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | $this->styleTableElement($table); |
|
76 | |||
77 | 6 | return $doc->saveHTML($table); |
|
78 | 3 | }; |
|
79 | |||
80 | 3 | $doc = new DOMDocument('1.0'); |
|
81 | 3 | ||
82 | 3 | $tbody = $this->xml_converter->rootElement('tbody')->import($records, $doc); |
|
0 ignored issues
–
show
|
|||
83 | $table = $doc->createElement('table'); |
||
84 | 3 | $this->styleTableElement($table); |
|
85 | if (!empty($headerRecord)) { |
||
86 | $table->appendChild( |
||
87 | $this->createRecordRow('thead', 'th', $headerRecord, $doc) |
||
88 | ); |
||
89 | } |
||
90 | 3 | $table->appendChild($tbody); |
|
91 | if (!empty($footerRecord)) { |
||
92 | 3 | $table->appendChild( |
|
93 | 3 | $this->createRecordRow('tfoot', 'th', $footerRecord, $doc) |
|
94 | ); |
||
95 | 3 | } |
|
96 | |||
97 | $doc->appendChild($table); |
||
98 | |||
99 | return $doc->saveHTML(); |
||
100 | } |
||
101 | 3 | ||
102 | /** |
||
103 | 3 | * HTML table class name setter. |
|
104 | 3 | * |
|
105 | * @throws DOMException if the id_value contains any type of whitespace |
||
106 | 3 | */ |
|
107 | public function table(string $class_name, string $id_value = ''): self |
||
108 | { |
||
109 | if (preg_match(",\s,", $id_value)) { |
||
110 | throw new DOMException("the id attribute's value must not contain whitespace (spaces, tabs etc.)"); |
||
111 | } |
||
112 | $clone = clone $this; |
||
113 | $clone->class_name = $class_name; |
||
114 | $clone->id_value = $id_value; |
||
115 | |||
116 | return $clone; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * HTML tr record offset attribute setter. |
||
121 | */ |
||
122 | public function tr(string $record_offset_attribute_name): self |
||
123 | { |
||
124 | $clone = clone $this; |
||
125 | $clone->xml_converter = $this->xml_converter->recordElement('tr', $record_offset_attribute_name); |
||
126 | |||
127 | return $clone; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * HTML td field name attribute setter. |
||
132 | */ |
||
133 | public function td(string $fieldname_attribute_name): self |
||
134 | { |
||
135 | $clone = clone $this; |
||
136 | $clone->xml_converter = $this->xml_converter->fieldElement('td', $fieldname_attribute_name); |
||
137 | |||
138 | return $clone; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Create a DOMElement representing a single record of data |
||
143 | * |
||
144 | * @param string $recordTagName |
||
145 | * @param string $fieldTagName |
||
146 | * @param array $record |
||
147 | * @param DOMDocument $doc |
||
148 | * |
||
149 | * @return DOMElement |
||
150 | */ |
||
151 | private function createRecordRow(string $recordTagName, string $fieldTagName, array $record, DOMDocument $doc) : DOMElement |
||
152 | { |
||
153 | $node = $this->xml_converter->rootElement($recordTagName)->fieldElement($fieldTagName)->import([$record], $doc); |
||
0 ignored issues
–
show
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...
|
|||
154 | |||
155 | /** @var DOMElement $element */ |
||
156 | foreach ($node->getElementsByTagName($fieldTagName) as $element) { |
||
157 | $element->setAttribute('scope', 'col'); |
||
158 | } |
||
159 | |||
160 | return $node; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Style the table dom element |
||
165 | * |
||
166 | * @param DOMElement $table_element |
||
167 | */ |
||
168 | private function styleTableElement(DOMElement $table_element) |
||
169 | { |
||
170 | $table_element->setAttribute('class', $this->class_name); |
||
171 | $table_element->setAttribute('id', $this->id_value); |
||
172 | } |
||
173 | } |
||
174 |
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: