diff options
author | ignace nyamagana butera <nyamsprod@gmail.com> | 2015-10-23 09:44:28 +0200 |
---|---|---|
committer | ignace nyamagana butera <nyamsprod@gmail.com> | 2015-10-23 09:44:28 +0200 |
commit | 426feba9a362320912ddcbadbb77c15e3ce906ec (patch) | |
tree | f3905cfe6f36b6726103e0097b04e42a84b75fee /src | |
parent | cecca13d6207a10a6feea939b18aa67d3eea8c21 (diff) | |
download | csv-426feba9a362320912ddcbadbb77c15e3ce906ec.zip csv-426feba9a362320912ddcbadbb77c15e3ce906ec.tar.gz csv-426feba9a362320912ddcbadbb77c15e3ce906ec.tar.bz2 |
Prepare for 7.2.0 release
- Introduce Controls::fetchDelimitersOccurrences
- Introduce Reader::fetch
- Deprecate Reader::query
- Deprecate Controls::detectDelimiterList
Diffstat (limited to 'src')
-rw-r--r-- | src/AbstractCsv.php | 2 | ||||
-rw-r--r-- | src/Config/Controls.php | 97 | ||||
-rw-r--r-- | src/Config/Output.php | 2 | ||||
-rw-r--r-- | src/Exception/InvalidRowException.php | 2 | ||||
-rw-r--r-- | src/Modifier/MapIterator.php | 2 | ||||
-rw-r--r-- | src/Modifier/QueryFilter.php | 2 | ||||
-rw-r--r-- | src/Modifier/RowFilter.php | 2 | ||||
-rw-r--r-- | src/Modifier/StreamFilter.php | 2 | ||||
-rw-r--r-- | src/Plugin/ColumnConsistencyValidator.php | 2 | ||||
-rw-r--r-- | src/Plugin/ForbiddenNullValuesValidator.php | 2 | ||||
-rw-r--r-- | src/Plugin/SkipNullValuesFormatter.php | 2 | ||||
-rw-r--r-- | src/Reader.php | 78 | ||||
-rw-r--r-- | src/Writer.php | 2 |
13 files changed, 115 insertions, 82 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index d541fd3..e61b6e2 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 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Config/Controls.php b/src/Config/Controls.php index 94ef968..99149b7 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 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -62,13 +62,6 @@ trait Controls protected $newline = "\n"; /** - * Returns the CSV Iterator - * - * @return \Iterator - */ - abstract public function getIterator(); - - /** * Sets the field delimiter * * @param string $delimiter @@ -98,57 +91,93 @@ trait Controls } /** - * Detects the actual number of row according to a delimiter + * Detects the CSV file delimiters * - * @param string $delimiter a CSV delimiter - * @param int $nb_rows the number of row to consider + * Returns a associative array where each key represents + * the number of occurences and each value a delimiter with the + * given occurence * - * @return int + * This method returns incorrect informations when two delimiters + * have the same occurrence count + * + * DEPRECATION WARNING! This method will be removed in the next major point release + * + * @deprecated deprecated since version 7.2 + * + * @param int $nb_rows + * @param string[] $delimiters additional delimiters + * + * @return string[] */ - protected function fetchRowsCountByDelimiter($delimiter, $nb_rows = 1) + public function detectDelimiterList($nb_rows = 1, array $delimiters = []) { - $iterator = $this->getIterator(); - $iterator->setCsvControl($delimiter, $this->enclosure, $this->escape); - $iterator = new LimitIterator($iterator, 0, $nb_rows); - $iterator = new CallbackFilterIterator($iterator, function ($row) { - return is_array($row) && count($row) > 1; - }); + $delimiters = array_merge([$this->delimiter, ',', ';', "\t"], $delimiters); + $stats = $this->fetchDelimitersOccurrence($delimiters, $nb_rows); - return count(iterator_to_array($iterator, false), COUNT_RECURSIVE); + return array_flip(array_filter($stats)); } /** - * Detects the CSV file delimiters + * Detect Delimiters occurences in the CSV * - * @param int $nb_rows - * @param string[] $delimiters additional delimiters + * Returns a associative array where each key represents + * a valid delimiter and each value the number of occurences + * + * @param string[] $delimiters the delimiters to consider + * @param int $nb_rows Detection is made using $nb_rows of the CSV * * @throws InvalidArgumentException If $nb_rows value is invalid * - * @return string[] + * @return array */ - public function detectDelimiterList($nb_rows = 1, array $delimiters = []) + public function fetchDelimitersOccurrence(array $delimiters, $nb_rows = 1) { $nb_rows = filter_var($nb_rows, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); - if (! $nb_rows) { - throw new InvalidArgumentException('`$nb_rows` must be a valid positive integer'); + if (!$nb_rows) { + throw new InvalidArgumentException('The number of rows to consider must be a valid positive integer'); } - $delimiters = array_filter($delimiters, function ($str) { return 1 == mb_strlen($str); }); - $delimiters = array_unique(array_merge([$this->delimiter, ',', ';', "\t"], $delimiters)); - $res = array_fill_keys($delimiters, 0); - array_walk($res, function (&$value, $delim) use ($nb_rows) { - $value = $this->fetchRowsCountByDelimiter($delim, $nb_rows); - }); + $delimiters = array_unique($delimiters); + $res = []; + foreach ($delimiters as $delim) { + $res[$delim] = $this->fetchRowsCountByDelimiter($delim, $nb_rows); + } arsort($res, SORT_NUMERIC); - return array_filter($res); + return $res; + } + + /** + * Detects the actual number of row according to a delimiter + * + * @param string $delimiter a CSV delimiter + * @param int $nb_rows the number of row to consider + * + * @return int + */ + protected function fetchRowsCountByDelimiter($delimiter, $nb_rows = 1) + { + $iterator = $this->getIterator(); + $iterator->setCsvControl($delimiter, $this->enclosure, $this->escape); + $iterator = new LimitIterator($iterator, 0, $nb_rows); + $iterator = new CallbackFilterIterator($iterator, function ($row) { + return is_array($row) && count($row) > 1; + }); + + return count(iterator_to_array($iterator, false), COUNT_RECURSIVE); } /** + * Returns the CSV Iterator + * + * @return \Iterator + */ + abstract public function getIterator(); + + /** * Sets the field enclosure * * @param string $enclosure diff --git a/src/Config/Output.php b/src/Config/Output.php index ca2cf44..f21dcc2 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 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Exception/InvalidRowException.php b/src/Exception/InvalidRowException.php index 559fcc2..0779310 100644 --- a/src/Exception/InvalidRowException.php +++ b/src/Exception/InvalidRowException.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Modifier/MapIterator.php b/src/Modifier/MapIterator.php index cedc95b..7853950 100644 --- a/src/Modifier/MapIterator.php +++ b/src/Modifier/MapIterator.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Modifier/QueryFilter.php b/src/Modifier/QueryFilter.php index eaceec8..ce34be1 100644 --- a/src/Modifier/QueryFilter.php +++ b/src/Modifier/QueryFilter.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Modifier/RowFilter.php b/src/Modifier/RowFilter.php index 4728660..532ccd7 100644 --- a/src/Modifier/RowFilter.php +++ b/src/Modifier/RowFilter.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Modifier/StreamFilter.php b/src/Modifier/StreamFilter.php index d3d717f..bc8f558 100644 --- a/src/Modifier/StreamFilter.php +++ b/src/Modifier/StreamFilter.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Plugin/ColumnConsistencyValidator.php b/src/Plugin/ColumnConsistencyValidator.php index b2942d1..dde9fea 100644 --- a/src/Plugin/ColumnConsistencyValidator.php +++ b/src/Plugin/ColumnConsistencyValidator.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Plugin/ForbiddenNullValuesValidator.php b/src/Plugin/ForbiddenNullValuesValidator.php index 2ce443c..f0dd825 100644 --- a/src/Plugin/ForbiddenNullValuesValidator.php +++ b/src/Plugin/ForbiddenNullValuesValidator.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE diff --git a/src/Plugin/SkipNullValuesFormatter.php b/src/Plugin/SkipNullValuesFormatter.php index 9746139..a023c44 100644 --- a/src/Plugin/SkipNullValuesFormatter.php +++ b/src/Plugin/SkipNullValuesFormatter.php @@ -4,7 +4,7 @@ * * @license http://opensource.org/licenses/MIT * @link https://github.com/thephpleague/csv/ -* @version 7.1.1 +* @version 7.2.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 f7e1d49..174a616 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 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE @@ -36,18 +36,32 @@ class Reader extends AbstractCsv /** * Returns a Filtered Iterator * - * @param callable $callable a callable function to be applied to each Iterator item + * DEPRECATION WARNING! This method will be removed in the next major point release + * + * @deprecated deprecated since version 7.2 * * @return Iterator */ public function query(callable $callable = null) { + return $this->fetch($callable); + } + + /** + * Return a Filtered Iterator + * + * @param callable $callable a callable function to be applied to each Iterator item + * + * @return Iterator + */ + public function fetch(callable $callable = null) + { + $filterArray = function ($row) { + return is_array($row); + }; $iterator = $this->getIterator(); $iterator = $this->applyBomStripping($iterator); - $iterator = new CallbackFilterIterator($iterator, function ($row) { - return is_array($row); - }); - + $iterator = new CallbackFilterIterator($iterator, $filterArray); $iterator = $this->applyIteratorFilter($iterator); $iterator = $this->applyIteratorSortBy($iterator); $iterator = $this->applyIteratorInterval($iterator); @@ -71,7 +85,7 @@ class Reader extends AbstractCsv public function each(callable $callable) { $index = 0; - $iterator = $this->query(); + $iterator = $this->fetch(); $iterator->rewind(); while ($iterator->valid() && true === call_user_func( $callable, @@ -99,7 +113,7 @@ class Reader extends AbstractCsv { $this->setOffset($offset); $this->setLimit(1); - $iterator = $this->query(); + $iterator = $this->fetch(); $iterator->rewind(); return (array) $iterator->current(); @@ -116,19 +130,7 @@ class Reader extends AbstractCsv */ public function fetchAll(callable $callable = null) { - return $this->execute($this->query($callable)); - } - - /** - * Transform the Iterator into an array - * - * @param Iterator $iterator - * - * @return array - */ - protected function execute(Iterator $iterator) - { - return iterator_to_array($iterator, false); + return iterator_to_array($this->fetch($callable), false); } /** @@ -150,16 +152,17 @@ class Reader extends AbstractCsv 'the column index must be a positive integer or 0' ); } - - $iterator = $this->query($callable); - $iterator = new CallbackFilterIterator($iterator, function ($row) use ($column_index) { + $filterColumn = function ($row) use ($column_index) { return array_key_exists($column_index, $row); - }); - $iterator = new MapIterator($iterator, function ($row) use ($column_index) { + }; + $selectColumn = function ($row) use ($column_index) { return $row[$column_index]; - }); + }; + + $iterator = $this->fetch($callable); + $iterator = new CallbackFilterIterator($iterator, $filterColumn); - return $this->execute($iterator); + return iterator_to_array(new MapIterator($iterator, $selectColumn), false); } /** @@ -168,7 +171,7 @@ class Reader extends AbstractCsv * The rows are presented as associated arrays * The callable function will be applied to each Iterator item * - * @param array|int $offset_or_keys the name for each key member OR the row Index to be + * @param int|array $offset_or_keys the name for each key member OR the row Index to be * used as the associated named keys * * @param callable $callable a callable function @@ -181,22 +184,23 @@ class Reader extends AbstractCsv { $keys = $this->getAssocKeys($offset_or_keys); $this->assertValidAssocKeys($keys); - if (! is_array($offset_or_keys)) { - $this->addFilter(function ($row, $rowIndex) use ($offset_or_keys) { + if (!is_array($offset_or_keys)) { + $filterOutOffset = function ($row, $rowIndex) use ($offset_or_keys) { return is_array($row) && $rowIndex != $offset_or_keys; - }); + }; + $this->addFilter($filterOutOffset); } - $keys_count = count($keys); - $iterator = $this->query($callable); - $iterator = new MapIterator($iterator, function (array $row) use ($keys, $keys_count) { + $iterator = $this->fetch($callable); + $combineArray = function (array $row) use ($keys) { + $keys_count = count($keys); if ($keys_count != count($row)) { $row = array_slice(array_pad($row, $keys_count, null), 0, $keys_count); } return array_combine($keys, $row); - }); + }; - return $this->execute($iterator); + return iterator_to_array(new MapIterator($iterator, $combineArray), false); } /** diff --git a/src/Writer.php b/src/Writer.php index 707c5bf..f0373f3 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 7.1.1 +* @version 7.2.0 * @package League.csv * * For the full copyright and license information, please view the LICENSE |