From dafc7420cc24bc3e93bc93c5d913d9a975d9a698 Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Tue, 18 Feb 2014 12:06:04 +0100 Subject: [PATCH 1/2] IteratorQuery::execute improved when using IteratorQuery::setSortBy --- src/Iterator/IteratorQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Iterator/IteratorQuery.php b/src/Iterator/IteratorQuery.php index f498f5a0..3fd71456 100644 --- a/src/Iterator/IteratorQuery.php +++ b/src/Iterator/IteratorQuery.php @@ -153,7 +153,7 @@ protected function execute(Iterator $iterator, callable $callable = null) } if ($this->sortBy) { - $res = new ArrayObject(iterator_to_array($iterator)); + $res = new ArrayObject(iterator_to_array($iterator, false)); $res->uasort($this->sortBy); $iterator = $res->getIterator(); unset($res); From 038e5663564e0c442cbadbd6b94832ff9b9e5a46 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Fri, 21 Feb 2014 22:56:52 +0100 Subject: [PATCH 2/2] decoupling IteratorQuery into new simplified Traits --- src/AbstractCsv.php | 2 +- src/ConverterTrait.php | 2 +- src/Iterator/IteratorFilter.php | 85 +++++++++++++++++++++ src/Iterator/IteratorInterval.php | 118 +++++++++++++++++++++++++++++ src/Iterator/IteratorQuery.php | 120 +++--------------------------- src/Iterator/IteratorSortBy.php | 86 +++++++++++++++++++++ src/Iterator/MapIterator.php | 2 +- src/Reader.php | 4 +- src/Writer.php | 2 +- 9 files changed, 306 insertions(+), 115 deletions(-) create mode 100644 src/Iterator/IteratorFilter.php create mode 100644 src/Iterator/IteratorInterval.php create mode 100644 src/Iterator/IteratorSortBy.php diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index c98fc90a..614f5c5e 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -6,7 +6,7 @@ * @copyright 2014 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 4.2.0 +* @version 4.2.1 * @package Bakame.csv * * MIT LICENSE diff --git a/src/ConverterTrait.php b/src/ConverterTrait.php index f6cf6ed8..67482492 100644 --- a/src/ConverterTrait.php +++ b/src/ConverterTrait.php @@ -6,7 +6,7 @@ * @copyright 2014 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 4.2.0 +* @version 4.2.1 * @package Bakame.csv * * MIT LICENSE diff --git a/src/Iterator/IteratorFilter.php b/src/Iterator/IteratorFilter.php new file mode 100644 index 00000000..9977e243 --- /dev/null +++ b/src/Iterator/IteratorFilter.php @@ -0,0 +1,85 @@ + +* @copyright 2014 Ignace Nyamagana Butera +* @link https://github.com/nyamsprod/Bakame.csv +* @license http://opensource.org/licenses/MIT +* @version 4.2.1 +* @package Bakame.csv +* +* MIT LICENSE +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +namespace Bakame\Csv\Iterator; + +use CallbackFilterIterator; +use Iterator; + +/** + * A Trait to filter Iterators + * + * @package Bakame.csv + * @since 4.2.1 + * + */ +trait IteratorFilter +{ + /** + * Callable function to filter the iterator + * + * @var callable + */ + private $filter; + + /** + * Set the Iterator filter method + * + * @param callable $filter + * + * @return self + */ + public function setFilter(callable $filter) + { + $this->filter = $filter; + + return $this; + } + + /** + * Filter the Iterator + * + * @param \Iterator $iterator + * + * @return \CallbackFilterIterator + */ + protected function applyFilter($iterator) + { + if (! $this->filter) { + return $iterator; + } + $iterator = new CallbackFilterIterator($iterator, $this->filter); + $this->filter = null; + + return $iterator; + } +} diff --git a/src/Iterator/IteratorInterval.php b/src/Iterator/IteratorInterval.php new file mode 100644 index 00000000..180c4e33 --- /dev/null +++ b/src/Iterator/IteratorInterval.php @@ -0,0 +1,118 @@ + +* @copyright 2014 Ignace Nyamagana Butera +* @link https://github.com/nyamsprod/Bakame.csv +* @license http://opensource.org/licenses/MIT +* @version 4.2.1 +* @package Bakame.csv +* +* MIT LICENSE +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +namespace Bakame\Csv\Iterator; + +use InvalidArgumentException; +use Iterator; +use LimitIterator; + +/** + * A Trait to Set a LimitIterator object + * + * @package Bakame.csv + * @since 4.2.1 + * + */ +trait IteratorInterval +{ + /** + * iterator Offset + * + * @var integer + */ + private $offset = 0; + + /** + * iterator maximum length + * + * @var integer + */ + private $limit = -1; + + /** + * Set LimitIterator Offset + * + * @param $offset + * + * @return self + */ + public function setOffset($offset) + { + if (false === filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { + throw new InvalidArgumentException('the offset must be a positive integer or 0'); + } + $this->offset = $offset; + + return $this; + } + + /** + * Set LimitInterator Count + * + * @param integer $limit + * + * @return self + */ + public function setLimit($limit) + { + if (false === filter_var($limit, FILTER_VALIDATE_INT, ['options' => ['min_range' => -1]])) { + throw new InvalidArgumentException('the limit must an integer greater or equals to -1'); + } + $this->limit = $limit; + + return $this; + } + + /** + * Sort the Iterator + * + * @param \Iterator $iterator + * + * @return \LimitIterator + */ + protected function applyInterval(Iterator $iterator) + { + if (0 == $this->offset && -1 == $this->limit) { + return $iterator; + } + $offset = $this->offset; + $limit = -1; + if ($this->limit > 0) { + $limit = $this->limit; + } + $this->limit = -1; + $this->offset = 0; + + return new LimitIterator($iterator, $offset, $limit); + } +} diff --git a/src/Iterator/IteratorQuery.php b/src/Iterator/IteratorQuery.php index 3fd71456..fbfc9d2c 100644 --- a/src/Iterator/IteratorQuery.php +++ b/src/Iterator/IteratorQuery.php @@ -6,7 +6,7 @@ * @copyright 2014 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 4.2.0 +* @version 4.2.1 * @package Bakame.csv * * MIT LICENSE @@ -32,14 +32,10 @@ */ namespace Bakame\Csv\Iterator; -use ArrayObject; -use CallbackFilterIterator; -use InvalidArgumentException; use Iterator; -use LimitIterator; /** - * A Trait to filter in a SQL-like manner Iterators + * A Trait to Query in a SQL-like manner Iterators * * @package Bakame.csv * @since 4.0.0 @@ -48,94 +44,19 @@ trait IteratorQuery { /** - * iterator Offset - * - * @var integer - */ - private $offset = 0; - - /** - * iterator maximum length - * - * @var integer - */ - private $limit = -1; - - /** - * Callable function to filter the iterator - * - * @var callable - */ - private $filter; - - /** - * Callable function to sort the ArrayObject - * - * @var callable + * Iterator Filtering Trait */ - private $sortBy; + use IteratorFilter; /** - * Set the Iterator filter method - * - * @param callable $filter - * - * @return self + * Iterator Sorting Trait */ - public function setFilter(callable $filter) - { - $this->filter = $filter; - - return $this; - } + use IteratorSortBy; /** - * Set the ArrayObject sort method - * - * @param callable $sort - * - * @return self + * Iterator Set Interval Trait */ - public function setSortBy(callable $sortBy) - { - $this->sortBy = $sortBy; - - return $this; - } - - /** - * Set LimitIterator Offset - * - * @param $offset - * - * @return self - */ - public function setOffset($offset) - { - if (false === filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { - throw new InvalidArgumentException('the offset must be a positive integer or 0'); - } - $this->offset = $offset; - - return $this; - } - - /** - * Set LimitInterator Count - * - * @param integer $limit - * - * @return self - */ - public function setLimit($limit) - { - if (false === filter_var($limit, FILTER_VALIDATE_INT, ['options' => ['min_range' => -1]])) { - throw new InvalidArgumentException('the limit must an integer greater or equals to -1'); - } - $this->limit = $limit; - - return $this; - } + use IteratorInterval; /** * Return a filtered Iterator based on the filtering settings @@ -147,28 +68,9 @@ public function setLimit($limit) */ protected function execute(Iterator $iterator, callable $callable = null) { - if ($this->filter) { - $iterator = new CallbackFilterIterator($iterator, $this->filter); - $this->filter = null; - } - - if ($this->sortBy) { - $res = new ArrayObject(iterator_to_array($iterator, false)); - $res->uasort($this->sortBy); - $iterator = $res->getIterator(); - unset($res); - $this->sortBy = null; - } - - $offset = $this->offset; - $limit = -1; - if ($this->limit > 0) { - $limit = $this->limit; - } - $this->limit = -1; - $this->offset = 0; - - $iterator = new LimitIterator($iterator, $offset, $limit); + $iterator = $this->applyFilter($iterator); + $iterator = $this->applySortBy($iterator); + $iterator = $this->applyInterval($iterator); if (! is_null($callable)) { $iterator = new MapIterator($iterator, $callable); } diff --git a/src/Iterator/IteratorSortBy.php b/src/Iterator/IteratorSortBy.php new file mode 100644 index 00000000..c26e85cc --- /dev/null +++ b/src/Iterator/IteratorSortBy.php @@ -0,0 +1,86 @@ + +* @copyright 2014 Ignace Nyamagana Butera +* @link https://github.com/nyamsprod/Bakame.csv +* @license http://opensource.org/licenses/MIT +* @version 4.2.1 +* @package Bakame.csv +* +* MIT LICENSE +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +namespace Bakame\Csv\Iterator; + +use Iterator; +use ArrayIterator; + +/** + * A Trait to sort an Iterator + * + * @package Bakame.csv + * @since 4.2.1 + * + */ +trait IteratorSortBy +{ + /** + * Callable function to sort the ArrayObject + * + * @var callable + */ + private $sortBy; + + /** + * Set the ArrayObject sort method + * + * @param callable $sort + * + * @return self + */ + public function setSortBy(callable $sortBy) + { + $this->sortBy = $sortBy; + + return $this; + } + + /** + * Sort the Iterator + * + * @param \Iterator $iterator + * + * @return \ArrayIterator + */ + protected function applySortBy(Iterator $iterator) + { + if (! $this->sortBy) { + return $iterator; + } + $res = iterator_to_array($iterator, false); + uasort($res, $this->sortBy); + $this->sortBy = null; + + return new ArrayIterator($res); + } +} diff --git a/src/Iterator/MapIterator.php b/src/Iterator/MapIterator.php index 866adf4c..3b3c9a72 100644 --- a/src/Iterator/MapIterator.php +++ b/src/Iterator/MapIterator.php @@ -6,7 +6,7 @@ * @copyright 2014 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 4.2.0 +* @version 4.2.1 * @package Bakame.csv * * MIT LICENSE diff --git a/src/Reader.php b/src/Reader.php index 45a597a9..f837598c 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -6,7 +6,7 @@ * @copyright 2014 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 4.2.0 +* @version 4.2.1 * @package Bakame.csv * * MIT LICENSE @@ -47,7 +47,7 @@ class Reader extends AbstractCsv { /** - * Iterator Filtering Trait + * Iterator Query Trait */ use IteratorQuery; diff --git a/src/Writer.php b/src/Writer.php index f151988a..d3e9ed05 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -6,7 +6,7 @@ * @copyright 2014 Ignace Nyamagana Butera * @link https://github.com/nyamsprod/Bakame.csv * @license http://opensource.org/licenses/MIT -* @version 4.2.0 +* @version 4.2.1 * @package Bakame.csv * * MIT LICENSE