Skip to content

Commit

Permalink
merging decoupled Iterator Query Components
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Feb 21, 2014
2 parents c8b33f2 + 038e566 commit f4c2216
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 115 deletions.
2 changes: 1 addition & 1 deletion src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ConverterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 85 additions & 0 deletions src/Iterator/IteratorFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Bakame.csv - A lightweight CSV Coder/Decoder library
*
* @author Ignace Nyamagana Butera <[email protected]>
* @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;
}
}
118 changes: 118 additions & 0 deletions src/Iterator/IteratorInterval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Bakame.csv - A lightweight CSV Coder/Decoder library
*
* @author Ignace Nyamagana Butera <[email protected]>
* @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);
}
}
120 changes: 11 additions & 109 deletions src/Iterator/IteratorQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit f4c2216

Please sign in to comment.