| 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 7.2.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 |
namespace League\Csv\Config; |
| 14 |
|
| 15 |
use DomDocument; |
| 16 |
use InvalidArgumentException; |
| 17 |
use Iterator; |
| 18 |
use League\Csv\Modifier\MapIterator; |
| 19 |
use SplFileObject; |
| 20 |
|
| 21 |
/** |
| 22 |
* A trait to output CSV |
| 23 |
* |
| 24 |
* @package League.csv |
| 25 |
* @since 6.3.0 |
| 26 |
* |
| 27 |
*/ |
| 28 |
trait Output |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Charset Encoding for the CSV |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $encodingFrom = 'UTF-8'; |
| 36 |
|
| 37 |
/** |
| 38 |
* The Input file BOM character |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
protected $input_bom; |
| 42 |
|
| 43 |
/** |
| 44 |
* The Output file BOM character |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
protected $output_bom; |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns the CSV Iterator |
| 51 |
* |
| 52 |
* @return Iterator |
| 53 |
*/ |
| 54 |
abstract protected function getConversionIterator(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the CSV Iterator |
| 58 |
* |
| 59 |
* @return Iterator |
| 60 |
*/ |
| 61 |
abstract public function getIterator(); |
| 62 |
|
| 63 |
/** |
| 64 |
* Sets the CSV encoding charset |
| 65 |
* |
| 66 |
* @param string $str |
| 67 |
* |
| 68 |
* @return static |
| 69 |
*/ |
| 70 |
public function setEncodingFrom($str) |
| 71 |
{ |
| 72 |
$str = str_replace('_', '-', $str); |
| 73 |
$str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH]); |
| 74 |
if (empty($str)) { |
| 75 |
throw new InvalidArgumentException('you should use a valid charset'); |
| 76 |
} |
| 77 |
$this->encodingFrom = strtoupper($str); |
| 78 |
|
| 79 |
return $this; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Gets the source CSV encoding charset |
| 84 |
* |
| 85 |
* @return string |
| 86 |
*/ |
| 87 |
public function getEncodingFrom() |
| 88 |
{ |
| 89 |
return $this->encodingFrom; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Sets the BOM sequence to prepend the CSV on output |
| 94 |
* |
| 95 |
* @param string $str The BOM sequence |
| 96 |
* |
| 97 |
* @return static |
| 98 |
*/ |
| 99 |
public function setOutputBOM($str = null) |
| 100 |
{ |
| 101 |
if (empty($str)) { |
| 102 |
$this->output_bom = null; |
| 103 |
|
| 104 |
return $this; |
| 105 |
} |
| 106 |
|
| 107 |
$this->output_bom = (string) $str; |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns the BOM sequence in use on Output methods |
| 114 |
* |
| 115 |
* @return string |
| 116 |
*/ |
| 117 |
public function getOutputBOM() |
| 118 |
{ |
| 119 |
return $this->output_bom; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the BOM sequence of the given CSV |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
public function getInputBOM() |
| 128 |
{ |
| 129 |
if (! $this->input_bom) { |
| 130 |
$bom = [ |
| 131 |
self::BOM_UTF32_BE, self::BOM_UTF32_LE, |
| 132 |
self::BOM_UTF16_BE, self::BOM_UTF16_LE, self::BOM_UTF8, |
| 133 |
]; |
| 134 |
$csv = $this->getIterator(); |
| 135 |
$csv->setFlags(SplFileObject::READ_CSV); |
| 136 |
$csv->rewind(); |
| 137 |
$line = $csv->fgets(); |
| 138 |
$res = array_filter($bom, function ($sequence) use ($line) { |
| 139 |
return strpos($line, $sequence) === 0; |
| 140 |
}); |
| 141 |
|
| 142 |
$this->input_bom = array_shift($res); |
| 143 |
} |
| 144 |
|
| 145 |
return $this->input_bom; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Outputs all data on the CSV file |
| 150 |
* |
| 151 |
* @param string $filename CSV downloaded name if present adds extra headers |
| 152 |
* |
| 153 |
* @return int Returns the number of characters read from the handle |
| 154 |
* and passed through to the output. |
| 155 |
*/ |
| 156 |
public function output($filename = null) |
| 157 |
{ |
| 158 |
if (!is_null($filename)) { |
| 159 |
$filename = filter_var($filename, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); |
| 160 |
header('Content-Type: application/octet-stream'); |
| 161 |
header('Content-Transfer-Encoding: binary'); |
| 162 |
header("Content-Disposition: attachment; filename=\"$filename\""); |
| 163 |
} |
| 164 |
|
| 165 |
return $this->fpassthru(); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Outputs all data from the CSV |
| 170 |
* |
| 171 |
* @return int Returns the number of characters read from the handle |
| 172 |
* and passed through to the output. |
| 173 |
*/ |
| 174 |
protected function fpassthru() |
| 175 |
{ |
| 176 |
$bom = ''; |
| 177 |
$input_bom = $this->getInputBOM(); |
| 178 |
if ($this->output_bom && $input_bom != $this->output_bom) { |
| 179 |
$bom = $this->output_bom; |
| 180 |
} |
| 181 |
$csv = $this->getIterator(); |
| 182 |
$csv->setFlags(SplFileObject::READ_CSV); |
| 183 |
$csv->rewind(); |
| 184 |
if (!empty($bom)) { |
| 185 |
$csv->fseek(mb_strlen($input_bom)); |
| 186 |
} |
| 187 |
echo $bom; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 188 |
$res = $csv->fpassthru(); |
| 189 |
|
| 190 |
return $res + strlen($bom); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Retrieves the CSV content |
| 195 |
* |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
public function __toString() |
| 199 |
{ |
| 200 |
ob_start(); |
| 201 |
$this->fpassthru(); |
| 202 |
|
| 203 |
return ob_get_clean(); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* JsonSerializable Interface |
| 208 |
* |
| 209 |
* @return array |
| 210 |
*/ |
| 211 |
#[\ReturnTypeWillChange] |
| 212 |
public function jsonSerialize() |
| 213 |
{ |
| 214 |
return iterator_to_array($this->convertToUtf8($this->getConversionIterator()), false); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Convert Csv file into UTF-8 |
| 219 |
* |
| 220 |
* @param Iterator $iterator |
| 221 |
* |
| 222 |
* @return Iterator |
| 223 |
*/ |
| 224 |
protected function convertToUtf8(Iterator $iterator) |
| 225 |
{ |
| 226 |
if (strpos($this->encodingFrom, 'UTF-8') !== false) { |
| 227 |
return $iterator; |
| 228 |
} |
| 229 |
|
| 230 |
return new MapIterator($iterator, function ($row) { |
| 231 |
foreach ($row as &$value) { |
| 232 |
$value = mb_convert_encoding($value, 'UTF-8', $this->encodingFrom); |
| 233 |
} |
| 234 |
unset($value); |
| 235 |
|
| 236 |
return $row; |
| 237 |
}); |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Returns a HTML table representation of the CSV Table |
| 242 |
* |
| 243 |
* @param string $class_name optional classname |
| 244 |
* |
| 245 |
* @return string |
| 246 |
*/ |
| 247 |
public function toHTML($class_name = 'table-csv-data') |
| 248 |
{ |
| 249 |
$doc = $this->toXML('table', 'tr', 'td'); |
| 250 |
$doc->documentElement->setAttribute('class', $class_name); |
| 251 |
|
| 252 |
return $doc->saveHTML($doc->documentElement); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Transforms a CSV into a XML |
| 257 |
* |
| 258 |
* @param string $root_name XML root node name |
| 259 |
* @param string $row_name XML row node name |
| 260 |
* @param string $cell_name XML cell node name |
| 261 |
* |
| 262 |
* @return DomDocument |
| 263 |
*/ |
| 264 |
public function toXML($root_name = 'csv', $row_name = 'row', $cell_name = 'cell') |
| 265 |
{ |
| 266 |
$doc = new DomDocument('1.0', 'UTF-8'); |
| 267 |
$root = $doc->createElement($root_name); |
| 268 |
$iterator = $this->convertToUtf8($this->getConversionIterator()); |
| 269 |
foreach ($iterator as $row) { |
| 270 |
$item = $doc->createElement($row_name); |
| 271 |
array_walk($row, function ($value) use (&$item, $doc, $cell_name) { |
| 272 |
$content = $doc->createTextNode($value); |
| 273 |
$cell = $doc->createElement($cell_name); |
| 274 |
$cell->appendChild($content); |
| 275 |
$item->appendChild($cell); |
| 276 |
}); |
| 277 |
$root->appendChild($item); |
| 278 |
} |
| 279 |
$doc->appendChild($root); |
| 280 |
|
| 281 |
return $doc; |
| 282 |
} |
| 283 |
} |
| 284 |
|