diff options
author | ignace nyamagana butera <nyamsprod@gmail.com> | 2014-03-22 23:11:47 +0100 |
---|---|---|
committer | ignace nyamagana butera <nyamsprod@gmail.com> | 2014-03-24 21:39:41 +0100 |
commit | d271a29ce97c03c9d4a46b8c8b801c78ed571ca9 (patch) | |
tree | 4e930b2dd51dd40c171281ddac1c4a7ace1b2f3a | |
parent | 747d20ae6b467e33e6712b81618cf3e252502281 (diff) | |
download | csv-d271a29ce97c03c9d4a46b8c8b801c78ed571ca9.zip csv-d271a29ce97c03c9d4a46b8c8b801c78ed571ca9.tar.gz csv-d271a29ce97c03c9d4a46b8c8b801c78ed571ca9.tar.bz2 |
move encoding to ConverterTrait
-rw-r--r-- | src/AbstractCsv.php | 46 | ||||
-rw-r--r-- | src/ConverterTrait.php | 62 | ||||
-rw-r--r-- | src/Reader.php | 1 |
3 files changed, 55 insertions, 54 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 86fd783..7498837 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -92,13 +92,6 @@ class AbstractCsv implements JsonSerializable, IteratorAggregate protected $flags = SplFileObject::READ_CSV; /** - * Charset Encoding for the CSV - * - * @var string - */ - protected $encoding = 'UTF-8'; - - /** * The destructor * * Make sure the class reference is destroy when the class is no longer used @@ -194,6 +187,7 @@ class AbstractCsv implements JsonSerializable, IteratorAggregate */ protected function fetchFile($path, $open_mode) { + ini_set("auto_detect_line_endings", true); if ($path instanceof SplFileObject) { return $path; } @@ -365,44 +359,38 @@ class AbstractCsv implements JsonSerializable, IteratorAggregate } /** - * Set the CSV encoding charset - * - * @param string $str + * Return the CSV Iterator * - * @return self + * @return \SplFileObject */ - public function setEncoding($str) + public function getIterator() { - $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->encoding = strtoupper($str); + $this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape); + $this->csv->setFlags($this->flags); - return $this; + return $this->csv; } /** - * Get the CSV encoding charset + * JsonSerializable Interface * - * @return string + * @return array */ - public function getEncoding() + public function jsonSerialize() { - return $this->encoding; + return iterator_to_array($this->convert2Utf8(), false); } /** - * Return the CSV Iterator + * Retrieves the CSV content * - * @return \SplFileObject + * @return string */ - public function getIterator() + public function __toString() { - $this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape); - $this->csv->setFlags($this->flags); + ob_start(); + $this->output(); - return $this->csv; + return ob_get_clean(); } } diff --git a/src/ConverterTrait.php b/src/ConverterTrait.php index 2ae47fc..a870dc8 100644 --- a/src/ConverterTrait.php +++ b/src/ConverterTrait.php @@ -35,6 +35,7 @@ namespace League\Csv; use DomDocument; use SplTempFileObject; use League\Csv\Iterator\MapIterator; +use InvalidArgumentException; /** * A abstract class to enable basic CSV manipulation @@ -46,6 +47,42 @@ use League\Csv\Iterator\MapIterator; trait ConverterTrait { /** + * Charset Encoding for the CSV + * + * @var string + */ + protected $encoding = 'UTF-8'; + + /** + * Set the CSV encoding charset + * + * @param string $str + * + * @return self + */ + public function setEncoding($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->encoding = strtoupper($str); + + return $this; + } + + /** + * Get the CSV encoding charset + * + * @return string + */ + public function getEncoding() + { + return $this->encoding; + } + + /** * Convert Csv file into UTF-8 * * @return \Iterator @@ -88,19 +125,6 @@ trait ConverterTrait } /** - * Retrieves the CSV content - * - * @return string - */ - public function __toString() - { - ob_start(); - $this->output(); - - return ob_get_clean(); - } - - /** * transform a CSV into a XML * * @param string $root_name XML root node name @@ -142,16 +166,4 @@ trait ConverterTrait return $doc->saveHTML($doc->documentElement); } - - /** - * JsonSerializable Interface - * - * @return array - */ - public function jsonSerialize() - { - $iterator = $this->convert2Utf8(); - - return iterator_to_array($iterator, false); - } } diff --git a/src/Reader.php b/src/Reader.php index e1bce40..de7e10d 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -63,6 +63,7 @@ class Reader extends AbstractCsv { $this->csv = $this->fetchFile($path, $open_mode); } + /** * Intelligent Array Combine * |