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