PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.2
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.2
2.4.0 2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 All 67 releases
better-payment / vendor / league / csv / src / HTMLConverter.php

HTMLConverter.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.2.2, at vendor/league/csv/src/HTMLConverter.php

162 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 function preg_match;
20
21 /**
22 * Converts tabular data into an HTML Table string.
23 */
24 class HTMLConverter
25 {
26 /** table class attribute value. */
27 protected string $class_name = 'table-csv-data';
28 /** table id attribute value. */
29 protected string $id_value = '';
30 protected XMLConverter $xml_converter;
31
32 public static function create(): self
33 {
34 return new self();
35 }
36
37 /**
38 * DEPRECATION WARNING! This method will be removed in the next major point release.
39 *
40 * @deprecated since version 9.7.0
41 * @see HTMLConverterTest::create()
42 */
43 public function __construct()
44 {
45 $this->xml_converter = XMLConverter::create()
46 ->rootElement('table')
47 ->recordElement('tr')
48 ->fieldElement('td')
49 ;
50 }
51
52 /**
53 * Converts a tabular data collection into a HTML table string.
54 *
55 * @param string[] $header_record An optional array of headers outputted using the`<thead>` section
56 * @param string[] $footer_record An optional array of footers to output to the table using `<tfoot>` and `<th>` elements
57 */
58 public function convert(iterable $records, array $header_record = [], array $footer_record = []): string
59 {
60 $doc = new DOMDocument('1.0');
61 if ([] === $header_record && [] === $footer_record) {
62 $table = $this->xml_converter->import($records, $doc);
63 $this->addHTMLAttributes($table);
64 $doc->appendChild($table);
65
66 /** @var string $content */
67 $content = $doc->saveHTML();
68
69 return $content;
70 }
71
72 $table = $doc->createElement('table');
73
74 $this->addHTMLAttributes($table);
75 $this->appendHeaderSection('thead', $header_record, $table);
76 $this->appendHeaderSection('tfoot', $footer_record, $table);
77
78 $table->appendChild($this->xml_converter->rootElement('tbody')->import($records, $doc));
79
80 $doc->appendChild($table);
81
82 /** @var string $content */
83 $content = $doc->saveHTML();
84
85 return $content;
86 }
87
88 /**
89 * Creates a DOMElement representing a HTML table heading section.
90 */
91 protected function appendHeaderSection(string $node_name, array $record, DOMElement $table): void
92 {
93 if ([] === $record) {
94 return;
95 }
96
97 /** @var DOMDocument $ownerDocument */
98 $ownerDocument = $table->ownerDocument;
99 $node = $this->xml_converter
100 ->rootElement($node_name)
101 ->recordElement('tr')
102 ->fieldElement('th')
103 ->import([$record], $ownerDocument)
104 ;
105
106 /** @var DOMElement $element */
107 foreach ($node->getElementsByTagName('th') as $element) {
108 $element->setAttribute('scope', 'col');
109 }
110
111 $table->appendChild($node);
112 }
113
114 /**
115 * Adds class and id attributes to an HTML tag.
116 */
117 protected function addHTMLAttributes(DOMElement $node): void
118 {
119 $node->setAttribute('class', $this->class_name);
120 $node->setAttribute('id', $this->id_value);
121 }
122
123 /**
124 * HTML table class name setter.
125 *
126 * @throws DOMException if the id_value contains any type of whitespace
127 */
128 public function table(string $class_name, string $id_value = ''): self
129 {
130 if (1 === preg_match(",\s,", $id_value)) {
131 throw new DOMException("the id attribute's value must not contain whitespace (spaces, tabs etc.)");
132 }
133 $clone = clone $this;
134 $clone->class_name = $class_name;
135 $clone->id_value = $id_value;
136
137 return $clone;
138 }
139
140 /**
141 * HTML tr record offset attribute setter.
142 */
143 public function tr(string $record_offset_attribute_name): self
144 {
145 $clone = clone $this;
146 $clone->xml_converter = $this->xml_converter->recordElement('tr', $record_offset_attribute_name);
147
148 return $clone;
149 }
150
151 /**
152 * HTML td field name attribute setter.
153 */
154 public function td(string $fieldname_attribute_name): self
155 {
156 $clone = clone $this;
157 $clone->xml_converter = $this->xml_converter->fieldElement('td', $fieldname_attribute_name);
158
159 return $clone;
160 }
161 }
162