diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-02-02 12:45:11 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-02-02 12:45:11 +0100 |
commit | b15c0b18c48b808a6bf1ad610b96f1ac2fc4a2e5 (patch) | |
tree | 546d433f616cc7eb90d9a7baeecfc1e0d7bd6cdb /src | |
parent | 3cf545c4c170aacc9ddd56f93fb7f6c6bc4cd811 (diff) | |
download | csv-b15c0b18c48b808a6bf1ad610b96f1ac2fc4a2e5.zip csv-b15c0b18c48b808a6bf1ad610b96f1ac2fc4a2e5.tar.gz csv-b15c0b18c48b808a6bf1ad610b96f1ac2fc4a2e5.tar.bz2 |
improve lib
Diffstat (limited to 'src')
-rw-r--r-- | src/AbstractCsv.php | 5 | ||||
-rw-r--r-- | src/Config/Controls.php | 4 | ||||
-rw-r--r-- | src/Config/Factory.php | 4 | ||||
-rw-r--r-- | src/Config/Output.php | 72 | ||||
-rw-r--r-- | src/Config/StreamFilter.php | 2 | ||||
-rw-r--r-- | src/Iterator/MapIterator.php | 2 | ||||
-rw-r--r-- | src/Iterator/Query.php | 2 | ||||
-rw-r--r-- | src/Reader.php | 5 | ||||
-rw-r--r-- | src/Writer.php | 2 |
9 files changed, 52 insertions, 46 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index a792c4f..c09773c 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.3.0 +* @version 7.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -155,6 +155,9 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate return $obj; } + /** + * {@inheritdoc} + */ protected function getOutputIterator() { return $this->getIterator(); diff --git a/src/Config/Controls.php b/src/Config/Controls.php index 6479d88..ba2aeee 100644 --- a/src/Config/Controls.php +++ b/src/Config/Controls.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.3.0 +* @version 7.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -141,7 +141,7 @@ trait Controls arsort($res, SORT_NUMERIC); - return array_keys(array_filter($res)); + return array_flip(array_filter($res)); } /** diff --git a/src/Config/Factory.php b/src/Config/Factory.php index 00f69b2..80eef38 100644 --- a/src/Config/Factory.php +++ b/src/Config/Factory.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.X.X +* @version 7.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -21,7 +21,7 @@ use SplTempFileObject; * A trait to facilate class instantiation * * @package League.csv - * @since 6.4.0 + * @since 7.0.0 * */ trait Factory diff --git a/src/Config/Output.php b/src/Config/Output.php index ed84ca0..dcdd5f4 100644 --- a/src/Config/Output.php +++ b/src/Config/Output.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.3.0 +* @version 7.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -42,21 +42,11 @@ trait Output /** * Return the CSV Iterator * - * @return \SplFileObject + * @return \Iterator */ abstract protected function getOutputIterator(); /** - * JsonSerializable Interface - * - * @return array - */ - public function jsonSerialize() - { - return iterator_to_array($this->convertToUtf8($this->getOutputIterator()), false); - } - - /** * Set the CSV encoding charset * * @param string $str @@ -122,10 +112,8 @@ trait Output */ public function output($filename = null) { - $iterator = $this->getOutputIterator(); - $iterator->rewind(); //@codeCoverageIgnoreStart - if (! is_null($filename) && self::isValidString($filename)) { + if (! is_null($filename)) { $filename = trim($filename); $filename = filter_var($filename, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); header("Content-Type: application/octet-stream"); @@ -133,6 +121,8 @@ trait Output header('Content-Disposition: attachment; filename="'.$filename); } //@codeCoverageIgnoreEnd + $iterator = $this->getIterator(); + $iterator->rewind(); echo $this->bom; $iterator->fpassthru(); } @@ -151,6 +141,37 @@ trait Output } /** + * JsonSerializable Interface + * + * @return array + */ + public function jsonSerialize() + { + return iterator_to_array($this->convertToUtf8($this->getIterator()), false); + } + + /** + * 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; + }); + } + + /** * Return a HTML table representation of the CSV Table * * @param string $class_name optional classname @@ -193,25 +214,4 @@ trait Output 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/StreamFilter.php b/src/Config/StreamFilter.php index 330f62d..cf2aa8f 100644 --- a/src/Config/StreamFilter.php +++ b/src/Config/StreamFilter.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.3.0 +* @version 7.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Iterator/MapIterator.php b/src/Iterator/MapIterator.php index 9077a3e..5e837ca 100644 --- a/src/Iterator/MapIterator.php +++ b/src/Iterator/MapIterator.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.3.0 +* @version 7.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Iterator/Query.php b/src/Iterator/Query.php index f732af7..cb405cb 100644 --- a/src/Iterator/Query.php +++ b/src/Iterator/Query.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.3.0 +* @version 7.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Reader.php b/src/Reader.php index 7952875..3986720 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.3.0 +* @version 7.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -61,6 +61,9 @@ class Reader extends AbstractCsv return $iterator; } + /** + * {@inheritdoc} + */ protected function getOutputIterator() { $iterator = $this->getIterator(); diff --git a/src/Writer.php b/src/Writer.php index f19b4b4..6a7067c 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 6.3.0 +* @version 7.0.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE |