1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
<?php
/**
* League.csv - A lightweight CSV Coder/Decoder library
*
* @author Ignace Nyamagana Butera <nyamsprod@gmail.com>
* @copyright 2014 Ignace Nyamagana Butera
* @link https://github.com/nyamsprod/League.csv
* @license http://opensource.org/licenses/MIT
* @version 5.0.0
* @package League.csv
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace League\Csv;
use DomDocument;
use SplTempFileObject;
use League\Csv\Iterator\MapIterator;
/**
* A abstract class to enable basic CSV manipulation
*
* @package League.csv
* @since 4.2.0
*
*/
trait ConverterTrait
{
/**
* Convert Csv file into UTF-8
*
* @return \Iterator
*/
protected function convert2Utf8()
{
if ('UTF-8' == $this->encoding) {
return $this->getIterator();
}
return new MapIterator($this->getIterator(), function ($row) {
foreach ($row as &$value) {
$value = mb_convert_encoding($value, 'UTF-8', $this->encoding);
}
unset($value);
return $row;
});
}
/**
* Output all data on the CSV file
*
* @param string $filename CSV downloaded name if present adds extra headers
*/
public function output($filename = null)
{
$iterator = $this->getIterator();
//@codeCoverageIgnoreStart
if (! is_null($filename) && AbstractCsv::isValidString($filename)) {
header('Content-Type: text/csv; charset="'.$this->encoding.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
if (! $iterator instanceof SplTempFileObject) {
header('Content-Length: '.$iterator->getSize());
}
}
//@codeCoverageIgnoreEnd
$iterator->rewind();
$iterator->fpassthru();
}
/**
* Retrieves the CSV content
*
* @return string
*/
public function __toString()
{
ob_start();
$this->output();
return ob_get_clean();
}
/**
* transform a CSV into a XML
*
* @param string $root_name XML root node name
* @param string $row_name XML row node name
* @param string $cell_name XML cell node name
*
* @return \DomDocument
*/
public function toXML($root_name = 'csv', $row_name = 'row', $cell_name = 'cell')
{
$doc = new DomDocument('1.0', 'UTF-8');
$root = $doc->createElement($root_name);
foreach ($this->convert2Utf8() as $row) {
$item = $doc->createElement($row_name);
foreach ($row as $value) {
$content = $doc->createTextNode($value);
$cell = $doc->createElement($cell_name);
$cell->appendChild($content);
$item->appendChild($cell);
}
$root->appendChild($item);
}
$doc->appendChild($root);
return $doc;
}
/**
* Return a HTML table representation of the CSV Table
*
* @param string $classname optional classname
*
* @return string
*/
public function toHTML($classname = 'table-csv-data')
{
$doc = $this->toXML('table', 'tr', 'td');
$doc->documentElement->setAttribute('class', $classname);
return $doc->saveHTML($doc->documentElement);
}
/**
* JsonSerializable Interface
*
* @return array
*/
public function jsonSerialize()
{
$iterator = $this->convert2Utf8();
return iterator_to_array($iterator, false);
}
}
|