diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-08-26 12:31:51 +0200 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-08-26 12:31:51 +0200 |
commit | b99e10d043675c17f6b92b7d09ef8a9a32b927fe (patch) | |
tree | 8e652aad568bc5533dbbd82b983bf3d8ec396fb7 /src | |
parent | f8af103652a60dd856572e2d9b9bf9861e403683 (diff) | |
download | csv-b99e10d043675c17f6b92b7d09ef8a9a32b927fe.zip csv-b99e10d043675c17f6b92b7d09ef8a9a32b927fe.tar.gz csv-b99e10d043675c17f6b92b7d09ef8a9a32b927fe.tar.bz2 |
remove Formatter trait
Diffstat (limited to 'src')
-rw-r--r-- | src/AbstractCsv.php | 147 | ||||
-rw-r--r-- | src/Config/Formatter.php | 176 |
2 files changed, 141 insertions, 182 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 6880650..0664ca7 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -12,15 +12,17 @@ */ 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 @@ -37,11 +39,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate use Controls; /** - * Csv Formatter Trait - */ - use Formatter; - - /** * Stream Filter API Trait */ use StreamFilter; @@ -63,6 +60,13 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate protected $open_mode; /** + * Charset Encoding for the CSV + * + * @var string + */ + protected $encodingFrom = 'UTF-8'; + + /** * The constructor * * The path must be an SplFileInfo object @@ -273,6 +277,59 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate } /** + * 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 @@ -283,4 +340,82 @@ 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 deleted file mode 100644 index 080cb9e..0000000 --- a/src/Config/Formatter.php +++ /dev/null @@ -1,176 +0,0 @@ -<?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; - }); - } -} |