-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from xi-project/views
Views
- Loading branch information
Showing
22 changed files
with
671 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
/nbproject | ||
/.idea | ||
/tests/phpunit.xml | ||
/tests/coverage |
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
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
134 changes: 134 additions & 0 deletions
134
library/Xi/Collections/Collection/SimpleCollectionView.php
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,134 @@ | ||
<?php | ||
namespace Xi\Collections\Collection; | ||
use Xi\Collections\Util; | ||
use Xi\Collections\CollectionView; | ||
|
||
class SimpleCollectionView extends SimpleCollection implements CollectionView | ||
{ | ||
/** | ||
* @var callback(Traversable) $creator | ||
*/ | ||
protected $creator; | ||
|
||
/** | ||
* @param Traversable $traversable | ||
* @param callback(Traversable) $creator optional | ||
*/ | ||
public function __construct($traversable, $creator = null) | ||
{ | ||
$this->traversable = $traversable; | ||
if (null === $creator) { | ||
$creator = SimpleCollection::getCreator(); | ||
} | ||
$this->creator = $creator; | ||
} | ||
|
||
public function force() | ||
{ | ||
$creator = $this->creator; | ||
return $creator($this); | ||
} | ||
|
||
protected function lazy($iterator) | ||
{ | ||
return new static($iterator, $this->creator); | ||
} | ||
|
||
public function apply($callback) | ||
{ | ||
$self = $this; | ||
return $this->lazy($this->getLazyIteratorFor(function() use($self, $callback) { | ||
return $callback($self); | ||
})); | ||
} | ||
|
||
public function take($number) | ||
{ | ||
if (0 >= $number) { | ||
return $this->lazy(new \EmptyIterator); | ||
} | ||
return $this->lazy(new \LimitIterator($this->getIterator(), 0, $number)); | ||
} | ||
|
||
public function map($callback) | ||
{ | ||
return $this->lazy($this->getMapIteratorFor($this->getIterator(), $callback)); | ||
} | ||
|
||
public function filter($predicate = null) | ||
{ | ||
if (null === $predicate) { | ||
$predicate = function($value) { | ||
return !empty($value); | ||
}; | ||
} | ||
|
||
return $this->lazy($this->getFilterIteratorFor($this->getIterator(), $predicate)); | ||
} | ||
|
||
public function concatenate($other) | ||
{ | ||
$iterator = new \AppendIterator; | ||
$iterator->append($this->getIterator()); | ||
$iterator->append($this->getIteratorFor($other)); | ||
return $this->lazy($this->getReindexIteratorFor($iterator)); | ||
} | ||
|
||
public function union($other) | ||
{ | ||
$iterator = new \AppendIterator; | ||
$iterator->append($this->getIterator()); | ||
$iterator->append($this->getIteratorFor($other)); | ||
return $this->lazy($this->getLazyIteratorFor(function() use($iterator) { | ||
return iterator_to_array($iterator); | ||
})); | ||
} | ||
|
||
public function flatMap($callback) | ||
{ | ||
return $this->lazy($this->getReindexIteratorFor($this->getFlatMapIteratorFor($this->getIterator(), $callback))); | ||
} | ||
|
||
public function values() | ||
{ | ||
return $this->lazy($this->getReindexIteratorFor($this->getIterator())); | ||
} | ||
|
||
public function keys() | ||
{ | ||
return $this->lazy($this->getReindexIteratorFor($this->getMapIteratorFor( | ||
$this->getIterator(), | ||
function($v, $k) { return $k; } | ||
))); | ||
} | ||
|
||
protected function getIteratorFor($other) | ||
{ | ||
return Util\Functions::getIterator($other); | ||
} | ||
|
||
protected function getLazyIteratorFor($callback) | ||
{ | ||
return new Util\LazyIterator($callback); | ||
} | ||
|
||
protected function getReindexIteratorFor($iterator) | ||
{ | ||
return new Util\ReindexIterator($iterator); | ||
} | ||
|
||
protected function getFlatMapIteratorFor($iterator, $callback) | ||
{ | ||
return new \RecursiveIteratorIterator(new Util\FlatMapIterator($iterator, $callback)); | ||
} | ||
|
||
protected function getMapIteratorFor($iterator, $callback) | ||
{ | ||
return new Util\CallbackMapIterator($iterator, $callback); | ||
} | ||
|
||
protected function getFilterIteratorFor($iterator, $callback) | ||
{ | ||
return new Util\CallbackFilterIterator($iterator, $callback); | ||
} | ||
} |
Oops, something went wrong.