summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/AbstractCsv.php190
-rw-r--r--src/Config/Output.php218
-rw-r--r--test/CsvTest.php4
3 files changed, 226 insertions, 186 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index 66c60da..79a8355 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -12,11 +12,11 @@
*/
namespace League\Csv;
-use DomDocument;
use InvalidArgumentException;
use IteratorAggregate;
use JsonSerializable;
use League\Csv\Config\Controls;
+use League\Csv\Config\Output;
use League\Csv\Config\StreamFilter;
use League\Csv\Iterator\MapIterator;
use SplFileInfo;
@@ -75,22 +75,14 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
protected $open_mode;
/**
- * Charset Encoding for the CSV
- *
- * @var string
- */
- protected $encodingFrom = 'UTF-8';
-
- /**
- * BOM sequence for Outputting the CSV
- * @var string
+ * Csv Controls Trait
*/
- protected $bom;
+ use Controls;
/**
- * Csv Controls Trait
+ * Csv Ouputting Trait
*/
- use Controls;
+ use Output;
/**
* Stream Filter API Trait
@@ -298,50 +290,11 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
}
/**
- * JsonSerializable Interface
- *
- * @return array
- */
- public function jsonSerialize()
- {
- return iterator_to_array($this->convertToUtf8($this->getIterator()), false);
- }
-
- /**
- * Set the CSV encoding charset
- *
- * @param string $str
- *
- * @return static
- */
- public function setEncodingFrom($str)
- {
- $str = str_replace('_', '-', $str);
- $str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH]);
- if (empty($str)) {
- throw new InvalidArgumentException('you should use a valid charset');
- }
- $this->encodingFrom = strtoupper($str);
-
- return $this;
- }
-
- /**
- * Get the source CSV encoding charset
- *
- * @return string
- */
- public function getEncodingFrom()
- {
- return $this->encodingFrom;
- }
-
- /**
* Returns the BOM sequence of the given CSV
*
* @return string
*/
- public function getInputBOM()
+ public function detectInputBOM()
{
$bom = [self::BOM_UTF8, self::BOM_UTF16_BE, self::BOM_UTF16_LE, self::BOM_UTF32_BE, self::BOM_UTF32_LE];
$csv = $this->getIterator();
@@ -356,59 +309,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
}
/**
- * Set the BOM sequence to prepend the CSV on output
- *
- * @param string $str The BOM sequence
- *
- * @return static
- */
- public function setOutputBOM($str = null)
- {
- if (is_null($str)) {
- $this->bom = $str;
- return $this;
- }
- $str = (string) $str;
- $str = trim($str);
- $this->bom = $str;
-
- return $this;
- }
-
- /**
- * Returns the BOM sequence in use on Output methods
- *
- * @return string
- */
- public function getOutputBOM()
- {
- return $this->bom;
- }
-
- /**
- * 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) && self::isValidString($filename)) {
- $fname = (string) $filename;
- $fname = trim($fname);
- $fname = filter_var($fname, FILTER_UNSAFE_RAW, ['flags' => FILTER_FLAG_STRIP_LOW]);
- header('Content-Type: application/octet-stream');
- header('Content-Transfer-Encoding: binary');
- header("Content-Disposition: attachment; filename=\"$fname\"; filename*=UTF-8' '".rawurlencode($fname));
- }
- //@codeCoverageIgnoreEnd
- echo $this->bom;
- $iterator->rewind();
- $iterator->fpassthru();
- }
-
- /**
* Validate a variable to be stringable
*
* @param object|string $str
@@ -419,82 +319,4 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
{
return is_scalar($str) || (is_object($str) && method_exists($str, '__toString'));
}
-
- /**
- * Retrieves the CSV content
- *
- * @return string
- */
- public function __toString()
- {
- ob_start();
- $this->output();
-
- return ob_get_clean();
- }
-
- /**
- * Return a HTML table representation of the CSV Table
- *
- * @param string $class_name optional classname
- *
- * @return string
- */
- public function toHTML($class_name = 'table-csv-data')
- {
- $doc = $this->toXML('table', 'tr', 'td');
- $doc->documentElement->setAttribute('class', $class_name);
-
- return $doc->saveHTML($doc->documentElement);
- }
-
- /**
- * 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);
- $iterator = $this->convertToUtf8($this->getIterator());
- foreach ($iterator as $row) {
- $item = $doc->createElement($row_name);
- array_walk($row, function ($value) use (&$item, $doc, $cell_name) {
- $content = $doc->createTextNode($value);
- $cell = $doc->createElement($cell_name);
- $cell->appendChild($content);
- $item->appendChild($cell);
- });
- $root->appendChild($item);
- }
- $doc->appendChild($root);
-
- return $doc;
- }
-
- /**
- * Convert Csv file into UTF-8
- *
- * @return \Traversable
- */
- protected function convertToUtf8(Traversable $iterator)
- {
- if (strpos($this->encodingFrom, 'UTF-8') !== false) {
- return $iterator;
- }
-
- return new MapIterator($iterator, function ($row) {
- foreach ($row as &$value) {
- $value = mb_convert_encoding($value, 'UTF-8', $this->encodingFrom);
- }
- unset($value);
-
- return $row;
- });
- }
}
diff --git a/src/Config/Output.php b/src/Config/Output.php
new file mode 100644
index 0000000..6eab996
--- /dev/null
+++ b/src/Config/Output.php
@@ -0,0 +1,218 @@
+<?php
+/**
+* This file is part of the League.csv library
+*
+* @license http://opensource.org/licenses/MIT
+* @link https://github.com/thephpleague/csv/
+* @version 6.2.0
+* @package League.csv
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code.
+*/
+namespace League\Csv\Config;
+
+use DomDocument;
+use InvalidArgumentException;
+use League\Csv\Iterator\MapIterator;
+use Traversable;
+
+/**
+ * A trait to output CSV
+ *
+ * @package League.csv
+ * @since 6.3.0
+ *
+ */
+trait Output
+{
+ /**
+ * Charset Encoding for the CSV
+ *
+ * @var string
+ */
+ protected $encodingFrom = 'UTF-8';
+
+ /**
+ * BOM sequence for Outputting the CSV
+ * @var string
+ */
+ protected $bom;
+
+ /**
+ * Return the CSV Iterator
+ *
+ * @return \SplFileObject
+ */
+ abstract public function getIterator();
+
+ /**
+ * JsonSerializable Interface
+ *
+ * @return array
+ */
+ public function jsonSerialize()
+ {
+ return iterator_to_array($this->convertToUtf8($this->getIterator()), false);
+ }
+
+ /**
+ * Set the CSV encoding charset
+ *
+ * @param string $str
+ *
+ * @return static
+ */
+ public function setEncodingFrom($str)
+ {
+ $str = str_replace('_', '-', $str);
+ $str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH]);
+ if (empty($str)) {
+ throw new InvalidArgumentException('you should use a valid charset');
+ }
+ $this->encodingFrom = strtoupper($str);
+
+ return $this;
+ }
+
+ /**
+ * Get the source CSV encoding charset
+ *
+ * @return string
+ */
+ public function getEncodingFrom()
+ {
+ return $this->encodingFrom;
+ }
+
+ /**
+ * Set the BOM sequence to prepend the CSV on output
+ *
+ * @param string $str The BOM sequence
+ *
+ * @return static
+ */
+ public function setOutputBOM($str = null)
+ {
+ if (empty($str)) {
+ $this->bom = null;
+ return $this;
+ }
+ $str = (string) $str;
+ $str = trim($str);
+ $this->bom = $str;
+
+ return $this;
+ }
+
+ /**
+ * Returns the BOM sequence in use on Output methods
+ *
+ * @return string
+ */
+ public function getOutputBOM()
+ {
+ return $this->bom;
+ }
+
+ /**
+ * 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) && League\Csv\AbstractCsv::isValidString($filename)) {
+ $fname = (string) $filename;
+ $fname = trim($fname);
+ $fname = filter_var($fname, FILTER_UNSAFE_RAW, ['flags' => FILTER_FLAG_STRIP_LOW]);
+ header('Content-Type: application/octet-stream');
+ header('Content-Transfer-Encoding: binary');
+ header("Content-Disposition: attachment; filename=\"$fname\"; filename*=UTF-8' '".rawurlencode($fname));
+ }
+ //@codeCoverageIgnoreEnd
+ echo $this->bom;
+ $iterator->rewind();
+ $iterator->fpassthru();
+ }
+
+ /**
+ * Retrieves the CSV content
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ ob_start();
+ $this->output();
+
+ return ob_get_clean();
+ }
+
+ /**
+ * Return a HTML table representation of the CSV Table
+ *
+ * @param string $class_name optional classname
+ *
+ * @return string
+ */
+ public function toHTML($class_name = 'table-csv-data')
+ {
+ $doc = $this->toXML('table', 'tr', 'td');
+ $doc->documentElement->setAttribute('class', $class_name);
+
+ return $doc->saveHTML($doc->documentElement);
+ }
+
+ /**
+ * 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);
+ $iterator = $this->convertToUtf8($this->getIterator());
+ foreach ($iterator as $row) {
+ $item = $doc->createElement($row_name);
+ array_walk($row, function ($value) use (&$item, $doc, $cell_name) {
+ $content = $doc->createTextNode($value);
+ $cell = $doc->createElement($cell_name);
+ $cell->appendChild($content);
+ $item->appendChild($cell);
+ });
+ $root->appendChild($item);
+ }
+ $doc->appendChild($root);
+
+ return $doc;
+ }
+
+ /**
+ * Convert Csv file into UTF-8
+ *
+ * @return \Traversable
+ */
+ protected function convertToUtf8(Traversable $iterator)
+ {
+ if (strpos($this->encodingFrom, 'UTF-8') !== false) {
+ return $iterator;
+ }
+
+ return new MapIterator($iterator, function ($row) {
+ foreach ($row as &$value) {
+ $value = mb_convert_encoding($value, 'UTF-8', $this->encodingFrom);
+ }
+ unset($value);
+
+ return $row;
+ });
+ }
+}
diff --git a/test/CsvTest.php b/test/CsvTest.php
index 9d9d620..9d3215a 100644
--- a/test/CsvTest.php
+++ b/test/CsvTest.php
@@ -127,7 +127,7 @@ class CsvTest extends PHPUnit_Framework_TestCase
$expected = "john,doe,john.doe@example.com".PHP_EOL
."jane,doe,jane.doe@example.com".PHP_EOL;
$reader = Reader::createFromString($expected);
- $this->assertEmpty($reader->getInputBOM());
+ $this->assertEmpty($reader->detectInputBOM());
}
public function testGetBomOnInputWithBOM()
@@ -135,7 +135,7 @@ class CsvTest extends PHPUnit_Framework_TestCase
$expected = "\x00\x00\xFE\xFFjohn,doe,john.doe@example.com".PHP_EOL
."jane,doe,jane.doe@example.com".PHP_EOL;
$reader = Reader::createFromString($expected);
- $this->assertSame(Reader::BOM_UTF32_BE, $reader->getInputBOM());
+ $this->assertSame(Reader::BOM_UTF32_BE, $reader->detectInputBOM());
}
/**