|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* League.Csv (https://csv.thephpleague.com). |
|
5
|
|
|
* |
|
6
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
7
|
|
|
* @license https://github.com/thephpleague/csv/blob/master/LICENSE (MIT License) |
|
8
|
|
|
* @version 9.1.5 |
|
9
|
|
|
* @link https://github.com/thephpleague/csv |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types=1); |
|
16
|
|
|
|
|
17
|
|
|
namespace League\Csv; |
|
18
|
|
|
|
|
19
|
|
|
use DOMException; |
|
20
|
|
|
use Traversable; |
|
21
|
|
|
use function preg_match; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A class to convert tabular data into an HTML Table string. |
|
25
|
|
|
* |
|
26
|
|
|
* @package League.csv |
|
27
|
|
|
* @since 9.0.0 |
|
28
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class HTMLConverter |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* table class attribute value. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $class_name = 'table-csv-data'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* table id attribute value. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $id_value = ''; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var XMLConverter |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $xml_converter; |
|
50
|
|
|
|
|
51
|
4 |
|
/** |
|
52
|
|
|
* New Instance. |
|
53
|
4 |
|
*/ |
|
54
|
4 |
|
public function __construct() |
|
55
|
4 |
|
{ |
|
56
|
4 |
|
$this->xml_converter = (new XMLConverter()) |
|
57
|
|
|
->rootElement('table') |
|
58
|
4 |
|
->recordElement('tr') |
|
59
|
|
|
->fieldElement('td') |
|
60
|
|
|
; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Convert an Record collection into a DOMDocument. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array|Traversable $records the tabular data collection |
|
67
|
2 |
|
*/ |
|
68
|
|
|
public function convert($records): string |
|
69
|
2 |
|
{ |
|
70
|
2 |
|
$doc = $this->xml_converter->convert($records); |
|
71
|
2 |
|
$doc->documentElement->setAttribute('class', $this->class_name); |
|
72
|
|
|
$doc->documentElement->setAttribute('id', $this->id_value); |
|
73
|
2 |
|
|
|
74
|
|
|
return $doc->saveHTML($doc->documentElement); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* HTML table class name setter. |
|
79
|
|
|
* |
|
80
|
|
|
* @throws DOMException if the id_value contains any type of whitespace |
|
81
|
|
|
*/ |
|
82
|
|
|
public function table(string $class_name, string $id_value = ''): self |
|
83
|
|
|
{ |
|
84
|
|
|
if (preg_match(",\s,", $id_value)) { |
|
85
|
|
|
throw new DOMException("the id attribute's value must not contain whitespace (spaces, tabs etc.)"); |
|
86
|
4 |
|
} |
|
87
|
|
|
$clone = clone $this; |
|
88
|
4 |
|
$clone->class_name = $class_name; |
|
89
|
2 |
|
$clone->id_value = $id_value; |
|
90
|
|
|
|
|
91
|
2 |
|
return $clone; |
|
92
|
2 |
|
} |
|
93
|
2 |
|
|
|
94
|
|
|
/** |
|
95
|
2 |
|
* HTML tr record offset attribute setter. |
|
96
|
|
|
*/ |
|
97
|
|
|
public function tr(string $record_offset_attribute_name): self |
|
98
|
|
|
{ |
|
99
|
|
|
$clone = clone $this; |
|
100
|
|
|
$clone->xml_converter = $this->xml_converter->recordElement('tr', $record_offset_attribute_name); |
|
101
|
|
|
|
|
102
|
|
|
return $clone; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
/** |
|
106
|
|
|
* HTML td field name attribute setter. |
|
107
|
2 |
|
*/ |
|
108
|
2 |
|
public function td(string $fieldname_attribute_name): self |
|
109
|
|
|
{ |
|
110
|
2 |
|
$clone = clone $this; |
|
111
|
|
|
$clone->xml_converter = $this->xml_converter->fieldElement('td', $fieldname_attribute_name); |
|
112
|
|
|
|
|
113
|
|
|
return $clone; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|