-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decoupling IteratorQuery into new simplified Traits
- Loading branch information
Showing
9 changed files
with
306 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.