diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-08-25 09:07:41 +0200 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-08-25 09:29:21 +0200 |
commit | d0c07427c9e7526da1e5aa436c6dc8143af3bde2 (patch) | |
tree | d059c6767461d5ed90927f1ea23e87ca161d03f5 /src | |
parent | cb2c6afbe3299135825f19b39d4ed593a162795c (diff) | |
download | csv-d0c07427c9e7526da1e5aa436c6dc8143af3bde2.zip csv-d0c07427c9e7526da1e5aa436c6dc8143af3bde2.tar.gz csv-d0c07427c9e7526da1e5aa436c6dc8143af3bde2.tar.bz2 |
prepare v6
Diffstat (limited to 'src')
-rw-r--r-- | src/AbstractCsv.php | 97 | ||||
-rw-r--r-- | src/Config/Controls.php | 59 | ||||
-rw-r--r-- | src/Iterator/Query.php | 102 |
3 files changed, 129 insertions, 129 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index f20d21a..4ad7c53 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -18,9 +18,11 @@ use IteratorAggregate; use JsonSerializable; use League\Csv\Config\Controls; 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 @@ -268,6 +270,42 @@ 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 @@ -292,6 +330,18 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate } /** + * Validate a variable to be stringable + * + * @param mixed $str + * + * @return boolean + */ + public static function isValidString($str) + { + return is_scalar($str) || (is_object($str) && method_exists($str, '__toString')); + } + + /** * Retrieves the CSV content * * @return string @@ -305,6 +355,21 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate } /** + * 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 @@ -334,29 +399,23 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate } /** - * Return a HTML table representation of the CSV Table - * - * @param string $class_name optional classname + * Convert Csv file into UTF-8 * - * @return string + * @return \Traversable */ - public function toHTML($class_name = 'table-csv-data') + protected function convertToUtf8(Traversable $iterator) { - $doc = $this->toXML('table', 'tr', 'td'); - $doc->documentElement->setAttribute('class', $class_name); + if (strpos($this->encodingFrom, 'UTF-8') !== false) { + return $iterator; + } - return $doc->saveHTML($doc->documentElement); - } + return new MapIterator($iterator, function ($row) { + foreach ($row as &$value) { + $value = mb_convert_encoding($value, 'UTF-8', $this->encodingFrom); + } + unset($value); - /** - * Validate a variable to be stringable - * - * @param mixed $str - * - * @return boolean - */ - public static function isValidString($str) - { - return is_scalar($str) || (is_object($str) && method_exists($str, '__toString')); + return $row; + }); } } diff --git a/src/Config/Controls.php b/src/Config/Controls.php index f1239bc..2c07fc4 100644 --- a/src/Config/Controls.php +++ b/src/Config/Controls.php @@ -14,10 +14,8 @@ namespace League\Csv\Config; use CallbackFilterIterator; use InvalidArgumentException; -use League\Csv\Iterator\MapIterator; use LimitIterator; use SplFileObject; -use Traversable; /** * A trait to configure and check CSV file and content @@ -57,13 +55,6 @@ trait Controls protected $flags = SplFileObject::READ_CSV; /** - * Charset Encoding for the CSV - * - * @var string - */ - protected $encodingFrom = 'UTF-8'; - - /** * set the field delimeter * * @param string $delimiter @@ -240,54 +231,4 @@ trait Controls { return $this->flags; } - - /** - * 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; - } - - /** - * 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/Iterator/Query.php b/src/Iterator/Query.php index 9a36932..e2f1a0a 100644 --- a/src/Iterator/Query.php +++ b/src/Iterator/Query.php @@ -90,6 +90,27 @@ trait Query } /** + * Sort the Iterator + * + * @param \Iterator $iterator + * + * @return \LimitIterator + */ + protected function applyIteratorInterval(Iterator $iterator) + { + if (0 == $this->iterator_offset && -1 == $this->iterator_limit) { + return $iterator; + } + $offset = $this->iterator_offset; + $limit = $this->iterator_limit; + + $this->iterator_limit = -1; + $this->iterator_offset = 0; + + return new LimitIterator($iterator, $offset, $limit); + } + + /** * Set an Iterator sorting callable function * * @param callable $callable @@ -145,6 +166,36 @@ trait Query } /** + * Sort the Iterator + * + * @param \Iterator $iterator + * + * @return \ArrayIterator + */ + protected function applyIteratorSortBy(Iterator $iterator) + { + if (! $this->iterator_sort_by) { + return $iterator; + } + $res = iterator_to_array($iterator, false); + + uasort($res, function ($rowA, $rowB) { + foreach ($this->iterator_sort_by as $callable) { + $res = $callable($rowA, $rowB); + if (0 !== $res) { + return $res; + } + } + + return 0; + }); + + $this->clearSortBy(); + + return new ArrayIterator($res); + } + + /** * Set the Iterator filter method * * @param callable $callable @@ -200,57 +251,6 @@ trait Query } /** - * Sort the Iterator - * - * @param \Iterator $iterator - * - * @return \LimitIterator - */ - protected function applyIteratorInterval(Iterator $iterator) - { - if (0 == $this->iterator_offset && -1 == $this->iterator_limit) { - return $iterator; - } - $offset = $this->iterator_offset; - $limit = $this->iterator_limit; - - $this->iterator_limit = -1; - $this->iterator_offset = 0; - - return new LimitIterator($iterator, $offset, $limit); - } - - /** - * Sort the Iterator - * - * @param \Iterator $iterator - * - * @return \ArrayIterator - */ - protected function applyIteratorSortBy(Iterator $iterator) - { - if (! $this->iterator_sort_by) { - return $iterator; - } - $res = iterator_to_array($iterator, false); - - uasort($res, function ($rowA, $rowB) { - foreach ($this->iterator_sort_by as $callable) { - $res = $callable($rowA, $rowB); - if (0 !== $res) { - return $res; - } - } - - return 0; - }); - - $this->clearSortBy(); - - return new ArrayIterator($res); - } - - /** * Filter the Iterator * * @param \Iterator $iterator |