1 | <?php |
||
29 | trait Output |
||
30 | { |
||
31 | /** |
||
32 | * Charset Encoding for the CSV |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $encodingFrom = 'UTF-8'; |
||
37 | |||
38 | /** |
||
39 | * The Input file BOM character |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $input_bom = ''; |
||
43 | |||
44 | /** |
||
45 | * The Output file BOM character |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $output_bom = ''; |
||
49 | |||
50 | /** |
||
51 | * Sets the CSV encoding charset |
||
52 | * |
||
53 | * @param string $str |
||
54 | * |
||
55 | * @return static |
||
56 | */ |
||
57 | 6 | public function setEncodingFrom($str) |
|
68 | |||
69 | /** |
||
70 | * Gets the source CSV encoding charset |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 3 | public function getEncodingFrom() |
|
78 | |||
79 | /** |
||
80 | * Sets the BOM sequence to prepend the CSV on output |
||
81 | * |
||
82 | * @param string $str The BOM sequence |
||
83 | * |
||
84 | * @return static |
||
85 | */ |
||
86 | 9 | public function setOutputBOM($str) |
|
87 | { |
||
88 | 9 | if (empty($str)) { |
|
89 | 3 | $this->output_bom = ''; |
|
90 | |||
91 | 3 | return $this; |
|
92 | } |
||
93 | |||
94 | 9 | $this->output_bom = (string) $str; |
|
95 | |||
96 | 9 | return $this; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Returns the BOM sequence in use on Output methods |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | 3 | public function getOutputBOM() |
|
108 | |||
109 | /** |
||
110 | * Returns the BOM sequence of the given CSV |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 51 | public function getInputBOM() |
|
134 | |||
135 | /** |
||
136 | * @inheritdoc |
||
137 | */ |
||
138 | abstract public function getIterator(); |
||
139 | |||
140 | /** |
||
141 | * Outputs all data on the CSV file |
||
142 | * |
||
143 | * @param string $filename CSV downloaded name if present adds extra headers |
||
144 | * |
||
145 | * @return int Returns the number of characters read from the handle |
||
146 | * and passed through to the output. |
||
147 | */ |
||
148 | 6 | public function output($filename = null) |
|
159 | |||
160 | /** |
||
161 | * Outputs all data from the CSV |
||
162 | * |
||
163 | * @return int Returns the number of characters read from the handle |
||
164 | * and passed through to the output. |
||
165 | */ |
||
166 | 18 | protected function fpassthru() |
|
184 | |||
185 | /** |
||
186 | * Retrieves the CSV content |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | 12 | public function __toString() |
|
197 | |||
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | public function jsonSerialize() |
||
205 | 6 | ||
206 | /** |
||
207 | * Returns the CSV Iterator |
||
208 | * |
||
209 | * @return Iterator |
||
210 | */ |
||
211 | abstract protected function getQueryIterator(); |
||
212 | |||
213 | /** |
||
214 | * Convert Csv file into UTF-8 |
||
215 | * |
||
216 | * @param Iterator $iterator |
||
217 | * |
||
218 | * @return Iterator |
||
219 | */ |
||
220 | protected function convertToUtf8(Iterator $iterator) |
||
236 | 3 | ||
237 | /** |
||
238 | * Returns a HTML table representation of the CSV Table |
||
239 | * |
||
240 | * @param string $class_attr optional classname |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function toHTML($class_attr = 'table-csv-data') |
||
251 | 6 | ||
252 | /** |
||
253 | * Transforms a CSV into a XML |
||
254 | * |
||
255 | * @param string $root_name XML root node name |
||
256 | * @param string $row_name XML row node name |
||
257 | * @param string $cell_name XML cell node name |
||
258 | * |
||
259 | * @return DomDocument |
||
260 | */ |
||
261 | public function toXML($root_name = 'csv', $row_name = 'row', $cell_name = 'cell') |
||
279 | } |
||
280 |