diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-08-26 12:22:12 +0200 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-08-26 12:23:00 +0200 |
commit | 25bd84885a879469918ebc8e2161a998feecacb7 (patch) | |
tree | cbe131053ab5e56c1082cdbf6ebd86cfa191bc2f /src | |
parent | b6e75f7dc42d4dae5879f2cbeca8141d89050b41 (diff) | |
download | csv-25bd84885a879469918ebc8e2161a998feecacb7.zip csv-25bd84885a879469918ebc8e2161a998feecacb7.tar.gz csv-25bd84885a879469918ebc8e2161a998feecacb7.tar.bz2 |
adding the Formatter
Diffstat (limited to 'src')
-rw-r--r-- | src/AbstractCsv.php | 159 | ||||
-rw-r--r-- | src/Config/Formatter.php | 176 |
2 files changed, 188 insertions, 147 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 4ad7c53..6880650 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -12,17 +12,15 @@ */ namespace League\Csv; -use DomDocument; use InvalidArgumentException; use IteratorAggregate; use JsonSerializable; use League\Csv\Config\Controls; +use League\Csv\Config\Formatter; use League\Csv\Config\StreamFilter; -use League\Csv\Iterator\MapIterator; use SplFileInfo; use SplFileObject; use SplTempFileObject; -use Traversable; /** * An abstract class to enable basic CSV manipulation @@ -34,14 +32,19 @@ use Traversable; abstract class AbstractCsv implements JsonSerializable, IteratorAggregate { /** - * Stream Filter API Trait + * Csv Controls Trait */ - use StreamFilter; + use Controls; /** - * Csv Controls Trait + * Csv Formatter Trait */ - use Controls; + use Formatter; + + /** + * Stream Filter API Trait + */ + use StreamFilter; /** * The constructor path @@ -83,7 +86,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate */ public function __destruct() { - //in case path is a SplFileObject we need to remove its reference + //in case $this->path is a SplFileObject we need to remove its reference $this->path = null; } @@ -181,7 +184,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate * Return a normalize path which could be a SplFileObject * or a string path * - * @param \SplFileInfo|object|string $path the filepath + * @param object|string $path the filepath * * @return \SplFileObject|string */ @@ -270,66 +273,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate } /** - * Charset Encoding for the CSV - * - * @var string - */ - protected $encodingFrom = 'UTF-8'; - - /** - * Set the CSV encoding charset - * - * @param string $str - * - * @return $this - */ - 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; - } - - /** - * 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)) { - $filename = (string) $filename; - $filename = filter_var($filename, FILTER_UNSAFE_RAW, ['flags' => FILTER_FLAG_STRIP_LOW]); - header('Content-Type: application/octet-stream'); - header('Content-Transfer-Encoding: binary'); - header('Content-Disposition: attachment; filename="'.$filename.'"'); - if (! $iterator instanceof SplTempFileObject) { - header('Content-Length: '.$iterator->getSize()); - } - } - //@codeCoverageIgnoreEnd - $iterator->rewind(); - $iterator->fpassthru(); - } - - /** * Validate a variable to be stringable * * @param mixed $str @@ -340,82 +283,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); - 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; - } - - /** - * 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/Formatter.php b/src/Config/Formatter.php new file mode 100644 index 0000000..080cb9e --- /dev/null +++ b/src/Config/Formatter.php @@ -0,0 +1,176 @@ +<?php +/** +* This file is part of the League.csv library +* +* @license http://opensource.org/licenses/MIT +* @link https://github.com/thephpleague/csv/ +* @version 6.0.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 SplTempFileObject; +use Traversable; + +/** + * A Trait to ease CSV export/conversion + * + * @package League.csv + * @since 6.0.0 + * + */ +trait Formatter +{ + /** + * Charset Encoding for the CSV + * + * @var string + */ + protected $encodingFrom = 'UTF-8'; + + /** + * Set the CSV encoding charset + * + * @param string $str + * + * @return $this + */ + 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; + } + + /** + * 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)) { + $filename = (string) $filename; + $filename = filter_var($filename, FILTER_UNSAFE_RAW, ['flags' => FILTER_FLAG_STRIP_LOW]); + header('Content-Type: application/octet-stream'); + header('Content-Transfer-Encoding: binary'); + header('Content-Disposition: attachment; filename="'.$filename.'"'); + if (! $iterator instanceof SplTempFileObject) { + header('Content-Length: '.$iterator->getSize()); + } + } + //@codeCoverageIgnoreEnd + $iterator->rewind(); + $iterator->fpassthru(); + } + + /** + * Validate a variable to be stringable + * + * @param mixed $str + * + * @return boolean + */ + abstract public static function isValidString($str); + + /** + * 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); + 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; + } + + /** + * 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; + }); + } +} |