.State 0000666 00000000012 13371317603 0005626 0 ustar 00 finalized
.gitignore 0000666 00000000030 13371317603 0006535 0 ustar 00 /vendor/
/composer.lock
Aggregate.php 0000666 00000003567 13371317603 0007166 0 ustar 00 _iterator = $iterator;
$this->_bufferSize = max($bufferSize, 1);
$this->_buffer = new \SplDoublyLinkedList();
return;
}
/**
* Get inner iterator.
*
* @return \Iterator
*/
public function getInnerIterator()
{
return $this->_iterator;
}
/**
* Get buffer.
*
* @return \SplDoublyLinkedList
*/
protected function getBuffer()
{
return $this->_buffer;
}
/**
* Get buffer size.
*
* @return int
*/
public function getBufferSize()
{
return $this->_bufferSize;
}
/**
* Return the current element.
*
* @return mixed
*/
public function current()
{
return $this->getBuffer()->current()[self::BUFFER_VALUE];
}
/**
* Return the key of the current element.
*
* @return mixed
*/
public function key()
{
return $this->getBuffer()->current()[self::BUFFER_KEY];
}
/**
* Move forward to next element.
*
* @return void
*/
public function next()
{
$innerIterator = $this->getInnerIterator();
$buffer = $this->getBuffer();
$buffer->next();
// End of the buffer, need a new value.
if (false === $buffer->valid()) {
for (
$bufferSize = count($buffer),
$maximumBufferSize = $this->getBufferSize();
$bufferSize >= $maximumBufferSize;
--$bufferSize
) {
$buffer->shift();
}
$innerIterator->next();
$buffer->push([
self::BUFFER_KEY => $innerIterator->key(),
self::BUFFER_VALUE => $innerIterator->current()
]);
// Seek to the end of the buffer.
$buffer->setIteratorMode($buffer::IT_MODE_LIFO | $buffer::IT_MODE_KEEP);
$buffer->rewind();
$buffer->setIteratorMode($buffer::IT_MODE_FIFO | $buffer::IT_MODE_KEEP);
}
return;
}
/**
* Move backward to previous element.
*
* @return void
*/
public function previous()
{
$this->getBuffer()->prev();
return;
}
/**
* Rewind the iterator to the first element.
*
* @return void
*/
public function rewind()
{
$innerIterator = $this->getInnerIterator();
$buffer = $this->getBuffer();
$innerIterator->rewind();
if (true === $buffer->isEmpty()) {
$buffer->push([
self::BUFFER_KEY => $innerIterator->key(),
self::BUFFER_VALUE => $innerIterator->current()
]);
}
$buffer->rewind();
return;
}
/**
* Check if current position is valid.
*
* @return bool
*/
public function valid()
{
return
$this->getBuffer()->valid() &&
$this->getInnerIterator()->valid();
}
}
CHANGELOG.md 0000666 00000007151 13371317603 0006371 0 ustar 00 # 2.16.11.19
* Documentation: New `README.md` file. (Ivan Enderlin, 2016-10-18T15:52:39+02:00)
* Documentation: Use HTTPS in `composer.json`. (Ivan Enderlin, 2016-10-07T07:56:41+02:00)
* Documentation: Update support properties. (Alexis von Glasow, 2016-10-06T19:17:39+02:00)
* Test: Use the `::class` constant. (Ivan Enderlin, 2016-09-09T09:29:43+02:00)
# 2.16.03.15
* Buffer: Make `current` & `key` methods inlinable. (Ivan Enderlin, 2016-02-22T12:01:39+01:00)
* README: Illustrate `Hoa\Iterator\Buffer`. (Ivan Enderlin, 2016-01-21T01:09:38+01:00)
* Test: Write test suite of `Hoa\Iterator\Buffer`. (Ivan Enderlin, 2016-01-21T01:01:32+01:00)
* Iterator: New `Buffer` iterator. (Ivan Enderlin, 2016-01-21T00:55:07+01:00)
* Documentation: Update API annotation. (Ivan Enderlin, 2016-01-20T22:18:30+01:00)
# 2.16.01.11
* Quality: Drop PHP5.4. (Ivan Enderlin, 2016-01-11T09:15:26+01:00)
* Quality: Run devtools:cs. (Ivan Enderlin, 2016-01-09T09:03:23+01:00)
* Core: Remove `Hoa\Core`. (Ivan Enderlin, 2016-01-09T08:17:49+01:00)
* Consistency: Use `Hoa\Consistency`. (Ivan Enderlin, 2015-12-08T11:16:47+01:00)
* Exception: Use `Hoa\Exception`. (Ivan Enderlin, 2015-11-20T07:54:23+01:00)
# 1.15.10.29
* Test: Specify file type with `hoa://Test/Vfs`. (Ivan Enderlin, 2015-10-29T22:20:43+01:00)
# 1.15.09.08
* Format code. (Ivan Enderlin, 2015-08-27T09:29:13+02:00)
* Add a `.gitignore` file. (Stéphane HULARD, 2015-08-03T11:33:46+02:00)
# 1.15.05.29
* Move to PSR-1 and PSR-2. (Ivan Enderlin, 2015-05-05T09:26:53+02:00)
# 1.15.02.20
* Add the CHANGELOG.md file. (Ivan Enderlin, 2015-02-20T09:38:07+01:00)
* Happy new year! (Ivan Enderlin, 2015-01-05T14:36:22+01:00)
# 1.14.12.10
* Move to PSR-4. (Ivan Enderlin, 2014-12-09T13:52:31+01:00)
# 1.14.11.26
* Require `hoa/test`. (Ivan Enderlin, 2014-11-24T15:37:30+01:00)
# 1.14.11.07
* Finalized the library! (Ivan Enderlin, 2014-11-07T14:39:57+01:00)
* `Recursive\Lookahead` has been removed. (Ivan Enderlin, 2014-11-07T14:29:59+01:00)
* Add a note about Lookbehind. (Ivan Enderlin, 2014-11-07T14:28:33+01:00)
* `RegularExpression` no longer extend `\RegexIterator`. (Ivan Enderlin, 2014-11-07T14:26:04+01:00)
* Remove a note about PHP5.4. (Ivan Enderlin, 2014-11-07T14:24:48+01:00)
* Add tests. (Ivan Enderlin, 2014-09-28T21:41:45+02:00)
* Implement the `Lookbehind` iterator. (Ivan Enderlin, 2014-09-29T21:23:54+02:00)
* `HasChildren` and `Tree` have been removed. (Ivan Enderlin, 2014-11-07T14:05:06+01:00)
* Remove the `Recursive\Tree` iterator. (Ivan Enderlin, 2014-11-07T14:04:30+01:00)
* `HasChildren` is not very well-defined, remove it. (Ivan Enderlin, 2014-11-07T13:38:15+01:00)
* Fix API documentation. (Ivan Enderlin, 2014-10-11T09:17:42+02:00)
* Fix the specific `fileinfo` class support. (Ivan Enderlin, 2014-10-11T09:15:37+02:00)
* Update API documentation. (Ivan Enderlin, 2014-10-09T23:29:31+02:00)
* Re-implement SPL `RegexIterator` and `Recursive~`. (Ivan Enderlin, 2014-10-06T14:47:25+02:00)
* Bye bye `Recursive\Lookahead`. (Ivan Enderlin, 2014-10-01T22:25:48+02:00)
* Rewrite the `Lookahead` iterator. (Ivan Enderlin, 2014-09-29T20:50:34+02:00)
* Fix key. (Ivan Enderlin, 2014-09-28T21:41:51+02:00)
* Add the `CallbackGenerator` iterator. (Ivan Enderlin, 2014-09-29T09:34:07+02:00)
* Remove `from`/`import` and update to PHP5.4. (Ivan Enderlin, 2014-09-28T20:50:27+02:00)
# 0.14.09.23
* Add `branch-alias`. (Stéphane PY, 2014-09-23T11:50:40+02:00)
# 0.14.09.17
* Drop PHP5.3. (Ivan Enderlin, 2014-09-17T16:58:17+02:00)
* Add the installation section. (Ivan Enderlin, 2014-09-17T16:57:53+02:00)
(first snapshot)
CallbackFilter.php 0000666 00000003577 13371317603 0010143 0 ustar 00 _callback = $callback;
return;
}
/**
* Return the current element.
*
* @return mixed
*/
public function current()
{
$handle = $this->_callback;
return $this->_current = $handle($this->_key);
}
/**
* Return the key of the current element.
*
* @return mixed
*/
public function key()
{
return $this->_key;
}
/**
* Move forward to next element.
*
* @return void
*/
public function next()
{
++$this->_key;
return;
}
/**
* Rewind the iterator to the first element.
*
* @return void
*/
public function rewind()
{
$this->_key = 0;
$this->_current = null;
return;
}
/**
* Check if current position is valid.
*
* @return bool
*/
public function valid()
{
return true;
}
}
Counter.php 0000666 00000007364 13371317603 0006716 0 ustar 00 _from = $from;
$this->_to = $to;
$this->_step = $step;
return;
}
/**
* Return the current element.
*
* @return int
*/
public function current()
{
return $this->_i;
}
/**
* Return the key of the current element.
*
* @return int
*/
public function key()
{
return $this->_key;
}
/**
* Move forward to next element.
*
* @return void
*/
public function next()
{
++$this->_key;
$this->_i += $this->_step;
return;
}
/**
* Rewind the iterator to the first element.
*
* @return void
*/
public function rewind()
{
$this->_key = 0;
$this->_i = $this->_from;
return;
}
/**
* Check if current position is valid.
*
* @return bool
*/
public function valid()
{
return $this->_i < $this->_to;
}
}
Demultiplexer.php 0000666 00000007401 13371317603 0010112 0 ustar 00 getIterator();
}
$this->_iterator = $iterator;
$this->_demuxer = $demuxer;
return;
}
/**
* Return the current element.
*
* @return mixed
*/
public function current()
{
if (null !== $this->_current) {
return $this->_current;
}
$demuxer = $this->_demuxer;
return $this->_current = $demuxer($this->_iterator->current());
}
/**
* Return the key of the current element.
*
* @return mixed
*/
public function key()
{
return $this->_iterator->key();
}
/**
* Move forward to next element.
*
* @return void
*/
public function next()
{
$this->_current = null;
return $this->_iterator->next();
}
/**
* Rewind the iterator to the first element.
*
* @return void
*/
public function rewind()
{
return $this->_iterator->rewind();
}
/**
* Check if current position is valid.
*
* @return bool
*/
public function valid()
{
return $this->_iterator->valid();
}
}
Directory.php 0000666 00000007075 13371317603 0007242 0 ustar 00 _splFileInfoClass = $splFileInfoClass;
parent::__construct($path);
$this->setRelativePath($path);
return;
}
/**
* Current.
* Please, see \DirectoryIterator::current() method.
*
* @return mixed
*/
public function current()
{
$out = parent::current();
if (null !== $this->_splFileInfoClass &&
$out instanceof \SplFileInfo) {
$out->setInfoClass($this->_splFileInfoClass);
$out = $out->getFileInfo();
if ($out instanceof \Hoa\Iterator\SplFileInfo) {
$out->setRelativePath($this->getRelativePath());
}
}
return $out;
}
/**
* Set relative path.
*
* @param string $relativePath Relative path.
* @return string
*/
protected function setRelativePath($path)
{
$old = $this->_relativePath;
$this->_relativePath = $path;
return $old;
}
/**
* Get relative path (if given).
*
* @return string
*/
public function getRelativePath()
{
return $this->_relativePath;
}
}
Exception.php 0000666 00000003614 13371317603 0007227 0 ustar 00 _splFileInfoClass = $splFileInfoClass;
if (null === $flags) {
parent::__construct($path);
} else {
parent::__construct($path, $flags);
}
return;
}
/**
* Current.
* Please, see \FileSystemIterator::current() method.
*
* @return mixed
*/
public function current()
{
$out = parent::current();
if (null !== $this->_splFileInfoClass &&
$out instanceof \SplFileInfo) {
$out->setInfoClass($this->_splFileInfoClass);
$out = $out->getFileInfo();
}
return $out;
}
}
Filter.php 0000666 00000003550 13371317603 0006515 0 ustar 00 _iterator = $iterator;
return;
}
/**
* Get inner iterator.
*
* @return \Iterator
*/
public function getInnerIterator()
{
return $this->_iterator;
}
/**
* Return the current element.
*
* @return mixed
*/
public function current()
{
return $this->_current;
}
/**
* Return the key of the current element.
*
* @return mixed
*/
public function key()
{
return $this->_key;
}
/**
* Move forward to next element.
*
* @return void
*/
public function next()
{
$innerIterator = $this->getInnerIterator();
$this->_valid = $innerIterator->valid();
if (false === $this->_valid) {
return;
}
$this->_key = $innerIterator->key();
$this->_current = $innerIterator->current();
return $innerIterator->next();
}
/**
* Rewind the iterator to the first element.
*
* @return void
*/
public function rewind()
{
$out = $this->getInnerIterator()->rewind();
$this->next();
return $out;
}
/**
* Check if current position is valid.
*
* @return bool
*/
public function valid()
{
return $this->_valid;
}
/**
* Check whether there is a next element.
*
* @return bool
*/
public function hasNext()
{
return $this->getInnerIterator()->valid();
}
/**
* Get next value.
*
* @return mixed
*/
public function getNext()
{
return $this->getInnerIterator()->current();
}
/**
* Get next key.
*
* @return mixed
*/
public function getNextKey()
{
return $this->getInnerIterator()->key();
}
}
Lookbehind.php 0000666 00000010146 13371317603 0007345 0 ustar 00 _iterator = $iterator;
return;
}
/**
* Get inner iterator.
*
* @return \Iterator
*/
public function getInnerIterator()
{
return $this->_iterator;
}
/**
* Return the current element.
*
* @return mixed
*/
public function current()
{
return $this->getInnerIterator()->current();
}
/**
* Return the key of the current element.
*
* @return mixed
*/
public function key()
{
return $this->getInnerIterator()->key();
}
/**
* Move forward to next element.
*
* @return void
*/
public function next()
{
$this->_previousKey = $this->key();
$this->_previousCurrent = $this->current();
return $this->getInnerIterator()->next();
}
/**
* Rewind the iterator to the first element.
*
* @return void
*/
public function rewind()
{
$this->_previousKey = -1;
$this->_previousCurrent = null;
return $this->getInnerIterator()->rewind();
}
/**
* Check if current position is valid.
*
* @return bool
*/
public function valid()
{
return $this->getInnerIterator()->valid();
}
/**
* Check whether there is a previous element.
*
* @return bool
*/
public function hasPrevious()
{
return -1 !== $this->_previousKey;
}
/**
* Get previous value.
*
* @return mixed
*/
public function getPrevious()
{
return $this->_previousCurrent;
}
/**
* Get previous key.
*
* @return mixed
*/
public function getPreviousKey()
{
return $this->_previousKey;
}
}
Map.php 0000666 00000003527 13371317603 0006011 0 ustar 00 _infos[] = $default;
} else {
$this->_infos[$infos] = $default;
}
return $out;
}
/**
* Get the registered iterator instances.
*
* @return array
*/
public function current()
{
$out = parent::current();
foreach ($out as $key => &$value) {
if (null === $value) {
$value = $this->_infos[$key];
}
}
return $out;
}
}
NoRewind.php 0000666 00000003547 13371317603 0007023 0 ustar 00
---
Hoa is a modular, extensible and
structured set of PHP libraries.
Moreover, Hoa aims at being a bridge between industrial and research worlds.
# Hoa\Iterator
[![Help on IRC](https://img.shields.io/badge/help-%23hoaproject-ff0066.svg)](https://webchat.freenode.net/?channels=#hoaproject)
[![Help on Gitter](https://img.shields.io/badge/help-gitter-ff0066.svg)](https://gitter.im/hoaproject/central)
[![Documentation](https://img.shields.io/badge/documentation-hack_book-ff0066.svg)](https://central.hoa-project.net/Documentation/Library/Iterator)
[![Board](https://img.shields.io/badge/organisation-board-ff0066.svg)](https://waffle.io/hoaproject/iterator)
This library provides a set of useful iterator (compatible with PHP iterators).
Existing PHP iterators have been updated to get new features and prior PHP
versions compatibility.
[Learn more](https://central.hoa-project.net/Documentation/Library/Iterator).
## Installation
With [Composer](https://getcomposer.org/), to include this library into
your dependencies, you need to
require [`hoa/iterator`](https://packagist.org/packages/hoa/iterator):
```sh
$ composer require hoa/iterator '~2.0'
```
For more installation procedures, please read [the Source
page](https://hoa-project.net/Source.html).
## Testing
Before running the test suites, the development dependencies must be installed:
```sh
$ composer install
```
Then, to run all the test suites:
```sh
$ vendor/bin/hoa test:run
```
For more information, please read the [contributor
guide](https://hoa-project.net/Literature/Contributor/Guide.html).
## Quick usage
We propose a quick overview of all iterators.
### The One
`Hoa\Iterator\Iterator` defines the basis of an iterator. It extends
[`Iterator`](http://php.net/class.iterator).
### External iterator
`Hoa\Iterator\Aggregate` allows a class to use an external iterator through the
`getIterator` method. It extends
[`IteratorAggregate`](http://php.net/iteratoraggregate)
### Traversable to iterator
`Hoa\Iterator\IteratorIterator` transforms anything that is
[traversable](http://php.net/traversable) into an iterator. It extends
[`IteratorIterator`](http://php.net/iteratoriterator).
### Iterator of iterators
`Hoa\Iterator\Outer` represents an iterator that iterates over iterators. It
extends [`OuterIterator`](http://php.net/outeriterator).
### Mock
`Hoa\Iterator\Mock` represents an empty iterator. It extends
[`EmptyIterator`](http://php.net/emptyiterator).
### Seekable
`Hoa\Iterator\Seekable` represents an iterator that can be seeked. It extends
[`SeekableIterator`](http://php.net/seekableiterator).
### Map
`Hoa\Iterator\Map` allows to iterate an array. It extends
[`ArrayIterator`](http://php.net/arrayiterator).
```php
$foobar = new Hoa\Iterator\Map(['f', 'o', 'o', 'b', 'a', 'r']);
foreach ($foobar as $value) {
echo $value;
}
/**
* Will output:
* foobar
*/
```
### Filters
`Hoa\Iterator\Filter` and `Hoa\Iterator\CallbackFilter` allows to filter the
content of an iterator. It extends
[`FilterIterator`](http://php.net/filteriterator) and
[`CallbackFilterIterator`](http://php.net/callbackfilteriterator).
```php
$filter = new Hoa\Iterator\CallbackFilter(
$foobar,
function ($value, $key, $iterator) {
return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
}
);
foreach ($filter as $value) {
echo $value;
}
/**
* Will output:
* fbr
*/
```
Also, `Hoa\Iterator\RegularExpression` allows to filter based on a regular
expression.
### Limit
`Hoa\Iterator\Limit` allows to iterate *n* elements of an iterator starting from
a specific offset. It extends [`LimitIterator`](http://php.net/limititerator).
```php
$limit = new Hoa\Iterator\Limit($foobar, 2, 3);
foreach ($limit as $value) {
echo $value;
}
/**
* Will output:
* oba
*/
```
### Infinity
`Hoa\Iterator\Infinite` allows to iterate over and over again the same iterator.
It extends [`InfiniteIterator`](http://php.net/infiniteiterator).
```php
$infinite = new Hoa\Iterator\Infinite($foobar);
$limit = new Hoa\Iterator\Limit($infinite, 0, 21);
foreach ($limit as $value) {
echo $value;
}
/**
* Will output:
* foobarfoobarfoobarfoo
*/
```
Also, `Hoa\Iterator\NoRewind` is an iterator that does not rewind. It extends
[`NoRewindIterator`](http://php.net/norewinditerator).
### Repeater
`Hoa\Iterator\Repeater` allows to repeat an iterator *n* times.
```php
$repeater = new Hoa\Iterator\Repeater(
$foobar,
3,
function ($i) {
echo "\n";
}
);
foreach ($repeater as $value) {
echo $value;
}
/**
* Will output:
* foobar
* foobar
* foobar
*/
```
### Counter
`Hoa\Iterator\Counter` is equivalent to a `for($i = $from, $i < $to, $i +=
$step)` loop.
```php
$counter = new Hoa\Iterator\Counter(0, 12, 3);
foreach ($counter as $value) {
echo $value, ' ';
}
/**
* Will output:
* 0 3 6 9
*/
```
### Union of iterators
`Hoa\Iterator\Append` allows to iterate over iterators one after another. It
extends [`AppendIterator`](http://php.net/appenditerator).
```php
$counter1 = new Hoa\Iterator\Counter(0, 12, 3);
$counter2 = new Hoa\Iterator\Counter(13, 23, 2);
$append = new Hoa\Iterator\Append();
$append->append($counter1);
$append->append($counter2);
foreach ($append as $value) {
echo $value, ' ';
}
/**
* Will output:
* 0 3 6 9 13 15 17 19 21
*/
```
### Multiple
`Hoa\Iterator\Multiple` allows to iterate over several iterator at the same
times. It extends [`MultipleIterator`](http://php.net/multipleiterator).
```php
$foobar = new Hoa\Iterator\Map(['f', 'o', 'o', 'b', 'a', 'r']);
$baz = new Hoa\Iterator\Map(['b', 'a', 'z']);
$multiple = new Hoa\Iterator\Multiple(
Hoa\Iterator\Multiple::MIT_NEED_ANY
| Hoa\Iterator\Multiple::MIT_KEYS_ASSOC
);
$multiple->attachIterator($foobar, 'one', '!');
$multiple->attachIterator($baz, 'two', '?');
foreach ($multiple as $iterators) {
echo $iterators['one'], ' | ', $iterators['two'], "\n";
}
/**
* Will output:
* f | b
* o | a
* o | z
* b | ?
* a | ?
* r | ?
*/
```
### Demultiplexer
`Hoa\Iterator\Demultiplexer` demuxes result from another iterator. This iterator
is somehow the opposite of the `Hoa\Iterator\Multiple` iterator.
```php
$counter = new Hoa\Iterator\Counter(0, 10, 1);
$multiple = new Hoa\Iterator\Multiple();
$multiple->attachIterator($counter);
$multiple->attachIterator(clone $counter);
$demultiplexer = new Hoa\Iterator\Demultiplexer(
$multiple,
function ($current) {
return $current[0] * $current[1];
}
);
foreach ($demultiplexer as $value) {
echo $value, ' ';
}
/**
* Will output:
* 0 1 4 9 16 25 36 49 64 81
*/
```
### File system
`Hoa\Iterator\Directory` and `Hoa\Iterator\FileSystem` allow to iterate the file
system where files are represented by instances of `Hoa\Iterator\SplFileInfo`.
They respectively extend
[`DirectoryIterator`](http://php.net/directoryiterator),
[`FilesystemIterator`](http://php.net/filesystemiterator) and
[`SplFileInfo`](http://php.net/splfileinfo).
```php
$directory = new Hoa\Iterator\Directory(resolve('hoa://Library/Iterator'));
foreach ($directory as $value) {
echo $value->getFilename(), "\n";
}
/**
* Will output:
* .
* ..
* .State
* Aggregate.php
* Append.php
* CallbackFilter.php
* composer.json
* Counter.php
* Demultiplexer.php
* …
*/
```
Also, the `Hoa\Iterator\Glob` allows to iterator with the glob strategy. It
extends [`GlobIterator`](http://php.net/globiterator). Thus:
```php
$glob = new Hoa\Iterator\Glob(resolve('hoa://Library/Iterator') . '/M*.php');
foreach ($glob as $value) {
echo $value->getFilename(), "\n";
}
/**
* Will output:
* Map.php
* Mock.php
* Multiple.php
*/
```
### Look ahead
`Hoa\Iterator\Lookahead` allows to look ahead for the next element:
```php
$counter = new Hoa\Iterator\Counter(0, 5, 1);
$lookahead = new Hoa\Iterator\Lookahead($counter);
foreach ($lookahead as $value) {
echo $value;
if (true === $lookahead->hasNext()) {
echo ' (next: ', $lookahead->getNext(), ')';
}
echo "\n";
}
/**
* Will output:
* 0 (next: 1)
* 1 (next: 2)
* 2 (next: 3)
* 3 (next: 4)
* 4
*/
```
The `Hoa\Iterator\Lookbehind` also exists and allows to look behind for the
previous element.
### Buffer
`Hoa\Iterator\Buffer` allows to move forward as usual but also backward up to a
given buffer size over another iterator:
```php
$abcde = new Hoa\Iterator\Map(['a', 'b', 'c', 'd', 'e']);
$buffer = new Hoa\Iterator\Buffer($abcde, 3);
$buffer->rewind();
echo $buffer->current(); // a
$buffer->next();
echo $buffer->current(); // b
$buffer->next();
echo $buffer->current(); // c
$buffer->previous();
echo $buffer->current(); // b
$buffer->previous();
echo $buffer->current(); // a
$buffer->next();
echo $buffer->current(); // b
/**
* Will output:
* abcbab
*/
```
### Callback generator
`Hoa\Iterator\CallbackGenerator` allows to transform any callable into an
iterator. This is very useful when combined with other iterators, for instance
with `Hoa\Iterator\Limit`:
```php
$generator = new Hoa\Iterator\CallbackGenerator(function ($key) {
return mt_rand($key, $key * 2);
});
$limit = new Hoa\Iterator\Limit($generator, 0, 10);
foreach ($limit as $value) {
echo $value, ' ';
}
/**
* Could output:
* 0 2 3 4 4 7 8 10 12 18
*/
```
### Recursive iterators
A recursive iterator is an iterator where its values is other iterators. The
most important interface is `Hoa\Iterator\Recursive\Recursive` (it extends
[`RecursiveIterator`](http://php.net/recursiveiterator)). Then we find (in
alphabetic order):
* `Hoa\Iterator\Recursive\CallbackFilter` (it extends
[`RecursiveCallbackFilterIterator`](http://php.net/recursivecallbackfilteriterator)),
* `Hoa\Iterator\Recursive\Directory` (it extends
[`RecursiveDirectoryIterator`](http://php.net/recursivedirectoryiterator)),
* `Hoa\Iterator\Recursive\Filter` (it extends
[`RecursiveFilterIterator`](http://php.net/recursivefilteriterator)),
* `Hoa\Iterator\Recursive\Iterator` (it extends
[`RecursiveIteratorIterator`](http://php.net/recursiveiteratoriterator)),
* `Hoa\Iterator\Recursive\Map` (it extends
[`RecursiveArrayIterator`](http://php.net/recursivearrayiterator)),
* `Hoa\Iterator\Recursive\Mock`,
* `Hoa\Iterator\Recursive\RegularExpression`
(it extends [`RecursiveRegularExpression`](http://php.net/recursiveregexiterator)).
## Documentation
The
[hack book of `Hoa\Iterator`](https://central.hoa-project.net/Documentation/Library/Iterator) contains
detailed information about how to use this library and how it works.
To generate the documentation locally, execute the following commands:
```sh
$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open
```
More documentation can be found on the project's website:
[hoa-project.net](https://hoa-project.net/).
## Getting help
There are mainly two ways to get help:
* On the [`#hoaproject`](https://webchat.freenode.net/?channels=#hoaproject)
IRC channel,
* On the forum at [users.hoa-project.net](https://users.hoa-project.net).
## Contribution
Do you want to contribute? Thanks! A detailed [contributor
guide](https://hoa-project.net/Literature/Contributor/Guide.html) explains
everything you need to know.
## License
Hoa is under the New BSD License (BSD-3-Clause). Please, see
[`LICENSE`](https://hoa-project.net/LICENSE) for details.
Recursive/CallbackFilter.php 0000666 00000003645 13371317603 0012106 0 ustar 00 _relativePath = self::$_handlePath;
self::$_handlePath = null;
} else {
$this->_relativePath = $path;
}
$this->setSplFileInfoClass($splFileInfoClass);
return;
}
/**
* Current.
* Please, see \RecursiveDirectoryIterator::current() method.
*
* @return mixed
*/
public function current()
{
$out = parent::current();
if (null !== $this->_splFileInfoClass &&
$out instanceof \SplFileInfo) {
$out->setInfoClass($this->_splFileInfoClass);
$out = $out->getFileInfo();
if ($out instanceof \Hoa\Iterator\SplFileInfo) {
$out->setRelativePath($this->getRelativePath());
}
}
return $out;
}
/**
* Get children.
* Please, see \RecursiveDirectoryIterator::getChildren() method.
*
* @return mixed
*/
public function getChildren()
{
self::$_handlePath = $this->getRelativePath();
$out = parent::getChildren();
if ($out instanceof \RecursiveDirectoryIterator) {
$out->setSplFileInfoClass($this->_splFileInfoClass);
}
return $out;
}
/**
* Set SplFileInfo classname.
*
* @param string $splFileInfoClass SplFileInfo classname.
* @return void
*/
public function setSplFileInfoClass($splFileInfoClass)
{
$this->_splFileInfoClass = $splFileInfoClass;
return;
}
/**
* Get relative path (if given).
*
* @return string
*/
public function getRelativePath()
{
return $this->_relativePath;
}
}
Recursive/Filter.php 0000666 00000003616 13371317603 0010467 0 ustar 00 getIterator();
}
$this->_iterator = $iterator;
return;
}
/**
* Return the current element.
*
* @return mixed
*/
public function current()
{
return $this->_iterator->current();
}
/**
* Return the key of the current element.
*
* @return mixed
*/
public function key()
{
return $this->_iterator->key();
}
/**
* Move forward to next element.
*
* @return void
*/
public function next()
{
return $this->_iterator->next();
}
/**
* Rewind the iterator to the first element.
*
* @return void
*/
public function rewind()
{
return $this->_iterator->rewind();
}
/**
* Check if current position is valid.
*
* @return bool
*/
public function valid()
{
return $this->_iterator->valid();
}
/**
* Return an iterator for the current entry.
* It's a fake, we return null.
*
* @return void
*/
public function getChildren()
{
return null;
}
/**
* Return if an iterator can be created for the current entry.
* It's a fake, we return false.
*
* @return bool
*/
public function hasChildren()
{
return false;
}
}
Recursive/Recursive.php 0000666 00000003751 13371317603 0011211 0 ustar 00 hasChildren() ||
true === parent::accept();
}
/**
* Get an iterator for the current entry.
*
* @return \Hoa\Iterator\Recursive\RegularExpression
*/
public function getChildren()
{
return new static(
true === $this->hasChildren()
? $this->getInnerIterator()->getChildren()
: null,
$this->getRegex(),
$this->getMode(),
$this->getFlags(),
$this->getPregFlags()
);
}
/**
* Check whether an iterator can be obtained for the current entry.
*
* @return bool
*/
public function hasChildren()
{
return $this->getInnerIterator()->hasChildren();
}
}
RegularExpression.php 0000666 00000022364 13371317603 0010755 0 ustar 00 _regex = $regex;
$this->setMode($mode);
$this->setFlags($flags);
$this->setPregFlags($pregFlags);
$this->replacement = null;
return;
}
/**
* Get accept status.
*
* @return bool
*/
public function accept()
{
if (is_array(parent::current())) {
return false;
}
$this->_key = parent::key();
$this->_current = parent::current();
$matches = [];
$useKey = $this->_flags & self::USE_KEY;
$subject = $useKey ? $this->_key : $this->_current;
$out = false;
switch ($this->_mode) {
case self::MATCH:
$out = 0 !== preg_match(
$this->_regex,
$subject,
$matches,
$this->_pregFlags
);
break;
case self::GET_MATCH:
$this->_current = [];
$out = 0 !== preg_match(
$this->_regex,
$subject,
$this->_current,
$this->_pregFlags
);
break;
case self::ALL_MATCHES:
$this->_current = [];
$out = 0 < preg_match_all(
$this->_regex,
$subject,
$this->_current,
$this->_pregFlags
);
break;
case self::SPLIT:
$this->_current = preg_split(
$this->_regex,
$subject,
null,
$this->_pregFlags
);
$out =
is_array($this->_current) &&
1 < count($this->_current);
break;
case self::REPLACE:
$numberOfReplacement = 0;
$result = preg_replace(
$this->_regex,
$this->replacement,
$subject,
-1,
$numberOfReplacement
);
if (null === $result || 0 === $numberOfReplacement) {
$out = false;
break;
}
if (0 !== $useKey) {
$this->_key = $result;
$out = true;
break;
}
$this->_current = $result;
$out = true;
break;
default:
$out = false;
break;
}
if (0 !== ($this->_flags & self::INVERT_MATCH)) {
return false === $out;
}
return $out;
}
/**
* Get current key.
*
* @return int
*/
public function key()
{
return $this->_key;
}
/**
* Get current value.
*
* @return string
*/
public function current()
{
return $this->_current;
}
/**
* Set mode.
*
* @param int $mode Mode.
* @return void
*/
public function setMode($mode)
{
if ($mode < self::MATCH || $mode > self::REPLACE) {
throw new \InvalidArgumentException(
'Illegal mode ' . $mode . '.'
);
}
$this->_mode = $mode;
return;
}
/**
* Set flags.
*
* @param int $flags Flags.
* @return void
*/
public function setFlags($flags)
{
$this->_flags = $flags;
return;
}
/**
* Set preg flags.
*
* @param int $pregFlags Preg flags.
* @return void
*/
public function setPregFlags($pregFlags)
{
$this->_pregFlags = $pregFlags;
return;
}
/**
* Get regular expression.
*
* @return string
*/
public function getRegex()
{
return $this->_regex;
}
/**
* Get mode.
*
* @return int
*/
public function getMode()
{
return $this->_mode;
}
/**
* Get flags.
*
* @return int
*/
public function getFlags()
{
return $this->_flags;
}
/**
* Get preg flags.
*
* @return int
*/
public function getPregFlags()
{
return $this->_pregFlags;
}
}
Repeater.php 0000666 00000010232 13371317603 0007032 0 ustar 00 = $n) {
throw new Exception(
'n must be greater than 0, given %d.',
0,
$n
);
}
if ($iterator instanceof \IteratorAggregate) {
$iterator = $iterator->getIterator();
}
$this->_iterator = $iterator;
$this->_n = $n;
$this->_body = $body;
return;
}
/**
* Return the current element.
*
* @return mixed
*/
public function current()
{
return $this->_iterator->current();
}
/**
* Return the key of the current element.
*
* @return mixed
*/
public function key()
{
return $this->_iterator->key();
}
/**
* Move forward to next element.
*
* @return void
*/
public function next()
{
return $this->_iterator->next();
}
/**
* Rewind the iterator to the first element.
*
* @return void
*/
public function rewind()
{
return $this->_iterator->rewind();
}
/**
* Check if current position is valid.
*
* @return bool
*/
public function valid()
{
$valid = $this->_iterator->valid();
if (true === $valid) {
return true;
}
if (null !== $this->_body) {
$handle = &$this->_body;
$handle($this->_i);
}
$this->rewind();
if ($this->_n <= $this->_i++) {
$this->_i = 1;
return false;
}
return true;
}
}
Seekable.php 0000666 00000003562 13371317603 0007006 0 ustar 00 getMTime()) {
$this->_hash = md5($this->getPathname() . $mtime);
}
$this->_relativePath = $relativePath;
return;
}
/**
* Get the hash.
*
* @return string
*/
public function getHash()
{
return $this->_hash;
}
/**
* Get the MTime.
*
* @return int
*/
public function getMTime()
{
try {
return parent::getMTime();
} catch (\RuntimeException $e) {
return -1;
}
}
/**
* Set relative path.
*
* @param string $relativePath Relative path.
* @return string
*/
public function setRelativePath($relativePath)
{
$old = $this->_relativePath;
$this->_relativePath = $relativePath;
return $old;
}
/**
* Get relative path (if given).
*
* @return string
*/
public function getRelativePath()
{
return $this->_relativePath;
}
/**
* Get relative pathname (if possible).
*
* @return string
*/
public function getRelativePathname()
{
if (null === $relative = $this->getRelativePath()) {
return $this->getPathname();
}
return substr($this->getPathname(), strlen($relative));
}
}
Test/Unit/Append.php 0000666 00000006620 13371317603 0010336 0 ustar 00 given(
$counter1 = new LUT\Counter(0, 12, 3),
$counter2 = new LUT\Counter(13, 23, 2),
$append = new LUT\Append(),
$append->append($counter1),
$append->append($counter2)
)
->when($result = iterator_to_array($append, false))
->then
->array($result)
->isEqualTo([
0,
3,
6,
9,
13,
15,
17,
19,
21
]);
}
public function case_singleton()
{
$this
->given(
$counter1 = new LUT\Counter(0, 12, 3),
$append = new LUT\Append(),
$append->append($counter1)
)
->when($result = iterator_to_array($append))
->then
->array($result)
->isEqualTo([
0,
3,
6,
9
]);
}
public function case_empty()
{
$this
->given($append = new LUT\Append())
->when($result = iterator_to_array($append))
->then
->array($result)
->isEmpty();
}
}
Test/Unit/Buffer.php 0000666 00000042674 13371317603 0010351 0 ustar 00 given(
$innerIterator = $this->getInnerIterator(),
$bufferSize = 3
)
->when($result = new SUT($innerIterator, $bufferSize))
->then
->object($result->getInnerIterator())
->isIdenticalTo($innerIterator)
->integer($result->getBufferSize())
->isEqualTo($bufferSize)
->let($buffer = $this->invoke($result)->getBuffer())
->object($buffer)
->isInstanceOf(\SplDoublyLinkedList::class)
->boolean($buffer->isEmpty())
->isTrue();
}
public function case_negative_buffer_size()
{
$this
->given(
$innerIterator = $this->getInnerIterator(),
$bufferSize = -42
)
->when($result = new SUT($innerIterator, $bufferSize))
->then
->integer($result->getBufferSize())
->isEqualTo(1);
}
public function case_null_buffer_size()
{
$this
->given(
$innerIterator = $this->getInnerIterator(),
$bufferSize = 0
)
->when($result = new SUT($innerIterator, $bufferSize))
->then
->integer($result->getBufferSize())
->isEqualTo(1);
}
public function case_fast_forward()
{
$this
->given($iterator = new SUT($this->getInnerIterator(), 3))
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo(['a', 'b', 'c', 'd', 'e'])
->array(iterator_to_array($this->invoke($iterator)->getBuffer()))
->isEqualTo([
0 => [
$iterator::BUFFER_KEY => 3,
$iterator::BUFFER_VALUE => 'd'
],
1 => [
$iterator::BUFFER_KEY => 4,
$iterator::BUFFER_VALUE => 'e'
],
2 => [
$iterator::BUFFER_KEY => null,
$iterator::BUFFER_VALUE => null
]
]);
}
public function case_fast_forward_with_too_big_buffer()
{
$this
->given($iterator = new SUT($this->getInnerIterator(), 10))
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo(['a', 'b', 'c', 'd', 'e'])
->array(iterator_to_array($this->invoke($iterator)->getBuffer()))
->isEqualTo([
0 => [
$iterator::BUFFER_KEY => 0,
$iterator::BUFFER_VALUE => 'a'
],
1 => [
$iterator::BUFFER_KEY => 1,
$iterator::BUFFER_VALUE => 'b'
],
2 => [
$iterator::BUFFER_KEY => 2,
$iterator::BUFFER_VALUE => 'c'
],
3 => [
$iterator::BUFFER_KEY => 3,
$iterator::BUFFER_VALUE => 'd'
],
4 => [
$iterator::BUFFER_KEY => 4,
$iterator::BUFFER_VALUE => 'e'
],
5 => [
$iterator::BUFFER_KEY => null,
$iterator::BUFFER_VALUE => null
]
]);
}
public function case_fast_forward_with_smallest_buffer()
{
$this
->given($iterator = new SUT($this->getInnerIterator(), 1))
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo(['a', 'b', 'c', 'd', 'e'])
->array(iterator_to_array($this->invoke($iterator)->getBuffer()))
->isEqualTo([
0 => [
$iterator::BUFFER_KEY => null,
$iterator::BUFFER_VALUE => null
]
]);
}
public function case_forward_forward_forward()
{
$this
->when($result = new SUT(new LUT\Map(['a', 'b', 'c']), 2))
->then
->variable($result->rewind())
->isNull()
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(0)
->string($result->current())
->isEqualTo('a')
->variable($result->next())
->isNull()
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(1)
->string($result->current())
->isEqualTo('b')
->variable($result->next())
->isNull()
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(2)
->string($result->current())
->isEqualTo('c')
->variable($result->next())
->isNull()
->boolean($result->valid())
->isFalse()
->variable($result->key())
->isNull()
->variable($result->current())
->isNull();
}
public function case_forward_forward_backward_backward_forward_forward_forward_step_by_step()
{
$this
->when($result = new SUT(new LUT\Map(['a', 'b', 'c']), 3))
->then
->variable($result->rewind())
->isNull()
->array(iterator_to_array($this->invoke($result)->getBuffer()))
->isEqualTo([
0 => [
$result::BUFFER_KEY => 0,
$result::BUFFER_VALUE => 'a'
]
])
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(0)
->string($result->current())
->isEqualTo('a')
->variable($result->next())
->isNull()
->array(iterator_to_array($this->invoke($result)->getBuffer()))
->isEqualTo([
0 => [
$result::BUFFER_KEY => 0,
$result::BUFFER_VALUE => 'a'
],
1 => [
$result::BUFFER_KEY => 1,
$result::BUFFER_VALUE => 'b'
]
])
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(1)
->string($result->current())
->isEqualTo('b')
->variable($result->next())
->isNull()
->array(iterator_to_array($this->invoke($result)->getBuffer()))
->isEqualTo([
0 => [
$result::BUFFER_KEY => 0,
$result::BUFFER_VALUE => 'a'
],
1 => [
$result::BUFFER_KEY => 1,
$result::BUFFER_VALUE => 'b'
],
2 => [
$result::BUFFER_KEY => 2,
$result::BUFFER_VALUE => 'c'
]
])
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(2)
->string($result->current())
->isEqualTo('c')
->variable($result->previous())
->isNull()
->array(iterator_to_array($this->invoke($result)->getBuffer()))
->isEqualTo([
0 => [
$result::BUFFER_KEY => 0,
$result::BUFFER_VALUE => 'a'
],
1 => [
$result::BUFFER_KEY => 1,
$result::BUFFER_VALUE => 'b'
],
2 => [
$result::BUFFER_KEY => 2,
$result::BUFFER_VALUE => 'c'
]
])
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(1)
->string($result->current())
->isEqualTo('b')
->variable($result->previous())
->isNull()
->array(iterator_to_array($this->invoke($result)->getBuffer()))
->isEqualTo([
0 => [
$result::BUFFER_KEY => 0,
$result::BUFFER_VALUE => 'a'
],
1 => [
$result::BUFFER_KEY => 1,
$result::BUFFER_VALUE => 'b'
],
2 => [
$result::BUFFER_KEY => 2,
$result::BUFFER_VALUE => 'c'
]
])
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(0)
->string($result->current())
->isEqualTo('a')
->variable($result->next())
->isNull()
->array(iterator_to_array($this->invoke($result)->getBuffer()))
->isEqualTo([
0 => [
$result::BUFFER_KEY => 0,
$result::BUFFER_VALUE => 'a'
],
1 => [
$result::BUFFER_KEY => 1,
$result::BUFFER_VALUE => 'b'
],
2 => [
$result::BUFFER_KEY => 2,
$result::BUFFER_VALUE => 'c'
]
])
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(1)
->string($result->current())
->isEqualTo('b')
->variable($result->next())
->isNull()
->array(iterator_to_array($this->invoke($result)->getBuffer()))
->isEqualTo([
0 => [
$result::BUFFER_KEY => 0,
$result::BUFFER_VALUE => 'a'
],
1 => [
$result::BUFFER_KEY => 1,
$result::BUFFER_VALUE => 'b'
],
2 => [
$result::BUFFER_KEY => 2,
$result::BUFFER_VALUE => 'c'
]
])
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(2)
->string($result->current())
->isEqualTo('c')
->variable($result->next())
->isNull()
->array(iterator_to_array($this->invoke($result)->getBuffer()))
->isEqualTo([
0 => [
$result::BUFFER_KEY => 1,
$result::BUFFER_VALUE => 'b'
],
1 => [
$result::BUFFER_KEY => 2,
$result::BUFFER_VALUE => 'c'
],
2 => [
$result::BUFFER_KEY => null,
$result::BUFFER_VALUE => null
]
])
->boolean($result->valid())
->isFalse()
->variable($result->key())
->isNull()
->variable($result->current())
->isNull();
}
public function case_backward_out_of_buffer()
{
$this
->when($result = new SUT(new LUT\Map(['a', 'b', 'c']), 1))
->then
->variable($result->rewind())
->isNull()
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(0)
->string($result->current())
->isEqualTo('a')
->variable($result->next())
->isNull()
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(1)
->string($result->current())
->isEqualTo('b')
->variable($result->previous())
->isNull()
->boolean($result->valid())
->isFalse();
}
public function case_rewind_rewind()
{
$this
->when($result = new SUT(new LUT\Map(['a', 'b']), 3))
->then
->variable($result->rewind())
->isNull()
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(0)
->string($result->current())
->isEqualTo('a')
->variable($result->next())
->isNull()
->variable($result->rewind())
->isNull()
->boolean($result->valid())
->isTrue()
->integer($result->key())
->isEqualTo(0)
->string($result->current())
->isEqualTo('a')
->variable($result->next())
->isNull()
->array(iterator_to_array($this->invoke($result)->getBuffer()))
->isEqualTo([
0 => [
$result::BUFFER_KEY => 0,
$result::BUFFER_VALUE => 'a'
],
1 => [
$result::BUFFER_KEY => 1,
$result::BUFFER_VALUE => 'b'
]
]);
}
protected function getInnerIterator()
{
return new LUT\Map(['a', 'b', 'c', 'd', 'e']);
}
}
Test/Unit/CallbackFilter.php 0000666 00000016321 13371317603 0011770 0 ustar 00 given(
$foobar = $this->getDummyIterator(),
$filter = new LUT\CallbackFilter(
$foobar,
function ($value) {
return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
}
)
)
->when($result = iterator_to_array($filter))
->then
->array($result)
->isEqualTo([
0 => 'f',
3 => 'b',
5 => 'r'
]);
}
public function case_all_callback_parameters()
{
$self = $this;
$this
->given(
$foobar = $this->getDummyIterator(),
$keys = [],
$values = [],
$filter = new LUT\CallbackFilter(
$foobar,
function ($value, $key, $iterator) use (
$self,
$foobar,
&$keys,
&$values
) {
$self
->object($iterator)
->isIdenticalTo($foobar);
$keys[] = $key;
$values[] = $value;
return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
}
)
)
->when($result = iterator_to_array($filter))
->then
->array($result)
->isEqualTo([
0 => 'f',
3 => 'b',
5 => 'r'
])
->array(array_combine($keys, $values))
->isEqualTo(iterator_to_array($foobar));
}
public function case_remove_all()
{
$this
->given(
$foobar = $this->getDummyIterator(),
$filter = new LUT\CallbackFilter(
$foobar,
function () {
return false;
}
)
)
->when($result = iterator_to_array($filter))
->then
->array($result)
->isEmpty();
}
public function case_remove_none()
{
$this
->given(
$foobar = $this->getDummyIterator(),
$filter = new LUT\CallbackFilter(
$foobar,
function () {
return true;
}
)
)
->when(
$foobarResult = iterator_to_array($foobar),
$filterResult = iterator_to_array($filter)
)
->then
->array($foobarResult)
->isEqualTo($filterResult);
}
public function case_recursive()
{
$this
->given(
$foobar = $this->getDummyRecursiveIterator(),
$filter = new LUT\Recursive\CallbackFilter(
$foobar,
function ($value) {
return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
}
),
$iterator = new LUT\Recursive\Iterator($filter)
)
->when($result = iterator_to_array($iterator, false))
->then
->array($result)
->isEqualTo([
0 => 'f',
1 => 'b',
2 => 'r'
]);
}
public function case_recursive_remove_all()
{
$this
->given(
$foobar = $this->getDummyRecursiveIterator(),
$filter = new LUT\Recursive\CallbackFilter(
$foobar,
function () {
return false;
}
),
$iterator = new LUT\Recursive\Iterator($filter)
)
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEmpty();
}
public function case_recursive_remove_none()
{
$this
->given(
$foobar = $this->getDummyRecursiveIterator(),
$filter = new LUT\Recursive\CallbackFilter(
$foobar,
function () {
return true;
}
),
$foobarIterator = new LUT\Recursive\Iterator($foobar),
$filterIterator = new LUT\Recursive\Iterator($filter)
)
->when(
$foobarResult = iterator_to_array($foobarIterator),
$filterResult = iterator_to_array($filterIterator)
)
->then
->array($foobarResult)
->isEqualTo($filterResult);
}
protected function getDummyIterator()
{
return new LUT\Map(['f', 'o', 'o', 'b', 'a', 'r']);
}
protected function getDummyRecursiveIterator()
{
return new LUT\Recursive\Map([['f', 'o', 'o'], ['b', 'a', 'r']]);
}
}
Test/Unit/CallbackGenerator.php 0000666 00000004775 13371317603 0012503 0 ustar 00 given(
$iterator = new LUT\CallbackGenerator(function ($key) {
return $key * 2;
}),
$limit = new LUT\Limit($iterator, 0, 5)
)
->when($result = iterator_to_array($limit))
->then
->array($result)
->isEqualTo([
0,
2,
4,
6,
8
]);
}
}
Test/Unit/Counter.php 0000666 00000005643 13371317603 0010552 0 ustar 00 given($iterator = new LUT\Counter(0, 12, 3))
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo([0, 3, 6, 9]);
}
public function case_offset()
{
$this
->given($iterator = new LUT\Counter(6, 12, 3))
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo([6, 9]);
}
public function case_too_small()
{
$this
->exception(function () {
new LUT\Counter(0, 0, 0);
})
->isInstanceOf(LUT\Exception::class);
}
public function case_too_big()
{
$this
->given($iterator = new LUT\Counter(0, 12, 13))
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo([0]);
}
}
Test/Unit/Demultiplexer.php 0000666 00000010006 13371317603 0011743 0 ustar 00 given(
$counter = new LUT\Counter(0, 10, 1),
$multiple = new LUT\Multiple(),
$multiple->attachIterator($counter),
$multiple->attachIterator(clone $counter),
$demultiplexer = new LUT\Demultiplexer(
$multiple,
function ($current) {
return $current[0] * $current[1];
}
)
)
->when($result = iterator_to_array($demultiplexer, false))
->then
->array($result)
->isEqualTo([
0,
1,
4,
9,
16,
25,
36,
49,
64,
81
]);
}
public function case_associative_keys()
{
$this
->given(
$counter = new LUT\Counter(0, 10, 1),
$multiple = new LUT\Multiple(
LUT\Multiple::MIT_NEED_ANY
| LUT\Multiple::MIT_KEYS_ASSOC
),
$multiple->attachIterator($counter, 'one'),
$multiple->attachIterator(clone $counter, 'two'),
$demultiplexer = new LUT\Demultiplexer(
$multiple,
function ($current) {
return $current['one'] * $current['two'];
}
)
)
->when($result = iterator_to_array($demultiplexer, false))
->then
->array($result)
->isEqualTo([
0,
1,
4,
9,
16,
25,
36,
49,
64,
81
]);
}
}
Test/Unit/Directory.php 0000666 00000021417 13371317603 0011074 0 ustar 00 given(
$root = resolve('hoa://Test/Vfs/Root?type=directory'),
resolve('hoa://Test/Vfs/Root/A?type=file'),
resolve('hoa://Test/Vfs/Root/Aa?type=file'),
resolve('hoa://Test/Vfs/Root/Aaa?type=file'),
$iterator = new LUT\Directory($root),
$result = []
)
->when(function () use ($iterator, &$result) {
foreach ($iterator as $key => $file) {
$result[$key] = $file->getFilename();
$this
->object($file)
->isInstanceOf(LUT\Directory::class);
}
})
->then
->array($result)
->isEqualTo([
0 => 'A',
1 => 'Aa',
2 => 'Aaa'
]);
}
public function case_seek_and_dots()
{
$this
->given(
$root = resolve('hoa://Test/Vfs/Root?type=directory'),
resolve('hoa://Test/Vfs/Root/.?type=directory'),
resolve('hoa://Test/Vfs/Root/..?type=directory'),
resolve('hoa://Test/Vfs/Root/Skip?type=file'),
resolve('hoa://Test/Vfs/Root/Gotcha?type=file'),
$iterator = new LUT\Directory($root)
)
->when($result = $iterator->current())
->then
->boolean($result->isDot())
->isTrue()
->when(
$iterator->next(),
$result = $iterator->current()
)
->then
->boolean($result->isDot())
->isTrue()
->when(
$iterator->seek(3),
$result = $iterator->current()
)
->then
->string($result->getFilename())
->isEqualTo('Gotcha')
->when(
$iterator->seek(2),
$result = $iterator->current()
)
->then
->string($result->getFilename())
->isEqualTo('Skip');
}
public function case_recursive()
{
$this
->given(
$root = resolve('hoa://Test/Vfs/Root?type=directory'),
resolve('hoa://Test/Vfs/Root/A?type=file'),
resolve('hoa://Test/Vfs/Root/Aa?type=file'),
resolve('hoa://Test/Vfs/Root/Aaa?type=file'),
resolve('hoa://Test/Vfs/Root/Foo?type=directory'),
resolve('hoa://Test/Vfs/Root/Foo/Bar?type=directory'),
resolve('hoa://Test/Vfs/Root/Foo/Bar/B?type=file'),
resolve('hoa://Test/Vfs/Root/Foo/Bar/Bb?type=file'),
resolve('hoa://Test/Vfs/Root/Foo/Bar/Bbb?type=file'),
resolve('hoa://Test/Vfs/Root/Foo/C?type=file'),
resolve('hoa://Test/Vfs/Root/Foo/Cc?type=file'),
resolve('hoa://Test/Vfs/Root/Foo/Ccc?type=file'),
$directory = new LUT\Recursive\Directory($root),
$iterator = new LUT\Recursive\Iterator($directory),
$result = []
)
->when(function () use ($iterator, &$result) {
foreach ($iterator as $file) {
$result[] = $file->getFilename();
}
})
->then
->array($result)
->isEqualTo([
'A',
'Aa',
'Aaa',
'B',
'Bb',
'Bbb',
'C',
'Cc',
'Ccc'
]);
}
public function case_splFileClassInfo()
{
$this
->given(
$splFileInfo = 'Hoa\Iterator\SplFileInfo',
$root = resolve('hoa://Test/Vfs/Root?type=directory'),
resolve('hoa://Test/Vfs/Root/a?type=file'),
resolve('hoa://Test/Vfs/Root/b?type=file'),
resolve('hoa://Test/Vfs/Root/c?type=file'),
resolve('hoa://Test/Vfs/Root/d?type=file'),
resolve('hoa://Test/Vfs/Root/e?type=file'),
resolve('hoa://Test/Vfs/Root/f?type=file'),
$iterator = new LUT\Directory(
$root,
$splFileInfo
),
$result = []
)
->when(function () use ($iterator, $splFileInfo, &$result) {
foreach ($iterator as $file) {
$this
->object($file)
->isInstanceOf($splFileInfo);
$result[] = $file->getFilename();
}
})
->then
->array($result)
->isEqualTo([
'a',
'b',
'c',
'd',
'e',
'f'
]);
}
public function case_recursive_splFileClassInfo()
{
$this
->given(
$splFileInfo = 'Hoa\Iterator\SplFileInfo',
$root = resolve('hoa://Test/Vfs/Root?type=directory'),
resolve('hoa://Test/Vfs/Root/A?type=directory'),
resolve('hoa://Test/Vfs/Root/A/a?type=file'),
resolve('hoa://Test/Vfs/Root/A/b?type=file'),
resolve('hoa://Test/Vfs/Root/A/c?type=file'),
resolve('hoa://Test/Vfs/Root/B?type=directory'),
resolve('hoa://Test/Vfs/Root/B/d?type=file'),
resolve('hoa://Test/Vfs/Root/B/e?type=file'),
resolve('hoa://Test/Vfs/Root/B/c?type=directory'),
resolve('hoa://Test/Vfs/Root/B/c/f?type=file'),
$directory = new LUT\Recursive\Directory(
$root,
LUT\FileSystem::CURRENT_AS_FILEINFO,
$splFileInfo
),
$iterator = new LUT\Recursive\Iterator($directory),
$result = []
)
->when(function () use ($iterator, $splFileInfo, &$result) {
foreach ($iterator as $file) {
$this
->object($file)
->isInstanceOf($splFileInfo);
$result[] = $file->getFilename();
}
})
->then
->array($result)
->isEqualTo([
'a',
'b',
'c',
'd',
'e',
'f'
]);
}
}
Test/Unit/FileSystem.php 0000666 00000010440 13371317603 0011206 0 ustar 00 given(
$root = resolve('hoa://Test/Vfs/Root?type=directory'),
resolve('hoa://Test/Vfs/Root/.?type=directory'),
resolve('hoa://Test/Vfs/Root/..?type=directory'),
resolve('hoa://Test/Vfs/Root/A?type=file'),
resolve('hoa://Test/Vfs/Root/B?type=file'),
$iterator = new LUT\FileSystem($root),
$result = []
)
->when(function () use ($iterator, &$result) {
foreach ($iterator as $pathname => $file) {
$this
->object($file)
->isInstanceOf('SplFileInfo');
$result[basename($pathname)] = $file->getFilename();
}
})
->array($result)
->isEqualTo([
'A' => 'A',
'B' => 'B'
]);
}
public function case_splFileClassInfo()
{
$this
->given(
$splFileInfo = 'Hoa\Iterator\SplFileInfo',
$root = resolve('hoa://Test/Vfs/Root?type=directory'),
resolve('hoa://Test/Vfs/Root/a?type=file'),
resolve('hoa://Test/Vfs/Root/b?type=file'),
resolve('hoa://Test/Vfs/Root/c?type=file'),
resolve('hoa://Test/Vfs/Root/d?type=file'),
resolve('hoa://Test/Vfs/Root/e?type=file'),
resolve('hoa://Test/Vfs/Root/f?type=file'),
$iterator = new LUT\FileSystem(
$root,
LUT\FileSystem::CURRENT_AS_FILEINFO,
$splFileInfo
),
$result = []
)
->when(function () use ($iterator, $splFileInfo, &$result) {
foreach ($iterator as $file) {
$this
->object($file)
->isInstanceOf($splFileInfo);
$result[] = $file->getFilename();
}
})
->then
->array($result)
->isEqualTo([
'a',
'b',
'c',
'd',
'e',
'f'
]);
}
}
Test/Unit/Filter.php 0000666 00000007510 13371317603 0010353 0 ustar 00 given(
$foobar = $this->getDummyIterator(),
$filter = new \Mock\Hoa\Iterator\Test\Unit\MyFilter($foobar),
$this->calling($filter)->accept = function () {
$value = $this->current();
return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
}
)
->when($result = iterator_to_array($filter))
->then
->array($result)
->isEqualTo([
0 => 'f',
3 => 'b',
5 => 'r'
]);
}
public function case_remove_all()
{
$this
->given(
$foobar = $this->getDummyIterator(),
$filter = new \Mock\Hoa\Iterator\Test\Unit\MyFilter($foobar),
$this->calling($filter)->accept = false
)
->when($result = iterator_to_array($filter))
->then
->array($result)
->isEmpty();
}
public function case_remove_none()
{
$this
->given(
$foobar = $this->getDummyIterator(),
$filter = new MyFilter($foobar)
)
->when(
$foobarResult = iterator_to_array($foobar),
$filterResult = iterator_to_array($filter)
)
->then
->array($foobarResult)
->isEqualTo($filterResult);
}
protected function getDummyIterator()
{
return new LUT\Map(['f', 'o', 'o', 'b', 'a', 'r']);
}
}
Test/Unit/Infinite.php 0000666 00000004623 13371317603 0010675 0 ustar 00 given(
$iterator = new LUT\Map(['a']),
$infinite = new LUT\Infinite($iterator),
$limit = new LUT\Limit($infinite, 0, 100)
)
->when($result = iterator_to_array($limit, false))
->then
->array($result)
->isEqualTo(array_fill(0, 100, 'a'))
->size
->isEqualTo(100);
}
}
Test/Unit/IteratorIterator.php 0000666 00000012250 13371317603 0012426 0 ustar 00 given(
$iterator = new LUT\Map([]),
$iteratoriterator = new LUT\IteratorIterator($iterator)
)
->when($result = $iteratoriterator->getInnerIterator())
->then
->object($result)
->isIdenticalTo($iterator);
}
public function case_traverse()
{
$this
->given(
$iterator = new LUT\Map(['a', 'b', 'c']),
$iteratoriterator = new LUT\IteratorIterator($iterator)
)
->when($result = iterator_to_array($iteratoriterator))
->then
->array($result)
->isEqualTo(['a', 'b', 'c']);
}
public function case_recursive_leaves_only()
{
$this
->given(
$array = [
'a' => ['b', 'c', 'd'],
'e' => ['f', 'g', 'i']
],
$iterator = new LUT\Recursive\Map($array),
$iteratoriterator = new LUT\Recursive\Iterator(
$iterator,
LUT\Recursive\Iterator::LEAVES_ONLY
)
)
->when($result = iterator_to_array($iteratoriterator, false))
->then
->array($result)
->isEqualTo(['b', 'c', 'd', 'f', 'g', 'i']);
}
public function case_recursive_self_first()
{
$this
->given(
$array = [
'a' => ['b', 'c', 'd'],
'e' => ['f', 'g', 'i']
],
$iterator = new LUT\Recursive\Map($array),
$iteratoriterator = new LUT\Recursive\Iterator(
$iterator,
LUT\Recursive\Iterator::SELF_FIRST
)
)
->when($result = iterator_to_array($iteratoriterator, false))
->then
->array($result)
->isEqualTo([
['b', 'c', 'd'],
'b',
'c',
'd',
['f', 'g', 'i'],
'f',
'g',
'i'
]);
}
public function case_recursive_child_first()
{
$this
->given(
$array = [
'a' => ['b', 'c', 'd'],
'e' => ['f', 'g', 'i']
],
$iterator = new LUT\Recursive\Map($array),
$iteratoriterator = new LUT\Recursive\Iterator(
$iterator,
LUT\Recursive\Iterator::CHILD_FIRST
)
)
->when($result = iterator_to_array($iteratoriterator, false))
->then
->array($result)
->isEqualTo([
'b',
'c',
'd',
['b', 'c', 'd'],
'f',
'g',
'i',
['f', 'g', 'i']
]);
}
}
Test/Unit/Limit.php 0000666 00000006167 13371317603 0010213 0 ustar 00 given(
$iterator = new LUT\Map(self::$_dummyArray),
$limit = new LUT\Limit($iterator, 2, 3)
)
->when($result = iterator_to_array($limit))
->then
->array($result)
->isEqualTo([
2 => 'o',
3 => 'b',
4 => 'a'
]);
}
public function case_negative_offset()
{
$this
->given($iterator = new LUT\Map(self::$_dummyArray))
->exception(function () use ($iterator) {
new LUT\Limit($iterator, -2, 3);
})
->isInstanceOf(\OutOfRangeException::class);
}
public function case_empty()
{
$this
->given(
$iterator = new LUT\Map(self::$_dummyArray),
$limit = new LUT\Limit($iterator, 0, 0)
)
->exception(function () use ($limit) {
iterator_to_array($limit);
})
->isInstanceOf(\OutOfBoundsException::class);
}
}
Test/Unit/Lookahead.php 0000666 00000013521 13371317603 0011014 0 ustar 00 given(
$iterator = new LUT\Map(['a', 'b', 'c']),
$lookahead = new LUT\Lookahead($iterator)
)
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo(['a', 'b', 'c']);
}
public function case_check_ahead()
{
$this
->given(
$iterator = new LUT\Map(['a', 'b', 'c']),
$lookahead = new LUT\Lookahead($iterator)
)
->when(
$lookahead->rewind(),
$key = $lookahead->key(),
$current = $lookahead->current(),
$hasNext = $lookahead->hasNext(),
$next = $lookahead->getNext()
)
->then
->integer($key)
->isEqualTo(0)
->string($current)
->isEqualTo('a')
->boolean($hasNext)
->isTrue()
->string($next)
->isEqualTo('b')
->when(
$lookahead->next(),
$key = $lookahead->key(),
$current = $lookahead->current(),
$hasNext = $lookahead->hasNext(),
$next = $lookahead->getNext()
)
->then
->integer($key)
->isEqualTo(1)
->string($current)
->isEqualTo('b')
->boolean($hasNext)
->isTrue()
->string($next)
->isEqualTo('c')
->when(
$lookahead->next(),
$key = $lookahead->key(),
$current = $lookahead->current(),
$hasNext = $lookahead->hasNext(),
$next = $lookahead->getNext()
)
->then
->integer($key)
->isEqualTo(2)
->string($current)
->isEqualTo('c')
->boolean($hasNext)
->isFalse()
->variable($next)
->isNull();
}
public function case_double_rewind()
{
$this
->given(
$iterator = new LUT\Map(['a', 'b', 'c']),
$lookahead = new LUT\Lookahead($iterator)
)
->when(
$lookahead->rewind(),
$key = $lookahead->key(),
$current = $lookahead->current(),
$hasNext = $lookahead->hasNext(),
$next = $lookahead->getNext()
)
->then
->integer($key)
->isEqualTo(0)
->string($current)
->isEqualTo('a')
->boolean($hasNext)
->isTrue()
->string($next)
->isEqualTo('b')
->when(
$lookahead->rewind(),
$key = $lookahead->key(),
$current = $lookahead->current(),
$hasNext = $lookahead->hasNext(),
$next = $lookahead->getNext()
)
->then
->integer($key)
->isEqualTo(0)
->string($current)
->isEqualTo('a')
->boolean($hasNext)
->isTrue()
->string($next)
->isEqualTo('b');
}
public function case_empty()
{
$this
->given(
$iterator = new LUT\Mock(),
$lookahead = new LUT\Lookahead($iterator)
)
->when(
$lookahead->rewind(),
$valid = $lookahead->valid()
)
->then
->boolean($valid)
->isFalse();
}
}
Test/Unit/Lookbehind.php 0000666 00000013426 13371317603 0011207 0 ustar 00 given(
$iterator = new LUT\Map(['a', 'b', 'c']),
$lookbehind = new LUT\Lookbehind($iterator)
)
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo(['a', 'b', 'c']);
}
public function case_check_behind()
{
$this
->given(
$iterator = new LUT\Map(['a', 'b', 'c']),
$lookbehind = new LUT\Lookbehind($iterator)
)
->when(
$lookbehind->rewind(),
$key = $lookbehind->key(),
$current = $lookbehind->current(),
$hasPrevious = $lookbehind->hasPrevious(),
$previous = $lookbehind->getPrevious()
)
->then
->integer($key)
->isEqualTo(0)
->string($current)
->isEqualTo('a')
->boolean($hasPrevious)
->isFalse()
->variable($previous)
->isNull()
->when(
$lookbehind->next(),
$key = $lookbehind->key(),
$current = $lookbehind->current(),
$hasPrevious = $lookbehind->hasPrevious(),
$previous = $lookbehind->getPrevious()
)
->then
->integer($key)
->isEqualTo(1)
->string($current)
->isEqualTo('b')
->boolean($hasPrevious)
->isTrue()
->string($previous)
->isEqualTo('a')
->when(
$lookbehind->next(),
$key = $lookbehind->key(),
$current = $lookbehind->current(),
$hasPrevious = $lookbehind->hasPrevious(),
$previous = $lookbehind->getPrevious()
)
->then
->integer($key)
->isEqualTo(2)
->string($current)
->isEqualTo('c')
->boolean($hasPrevious)
->isTrue()
->string($previous)
->isEqualTo('b');
}
public function case_double_rewind()
{
$this
->given(
$iterator = new LUT\Map(['a', 'b', 'c']),
$lookbehind = new LUT\Lookbehind($iterator)
)
->when(
$lookbehind->rewind(),
$key = $lookbehind->key(),
$current = $lookbehind->current(),
$hasPrevious = $lookbehind->hasPrevious()
)
->then
->integer($key)
->isEqualTo(0)
->string($current)
->isEqualTo('a')
->boolean($hasPrevious)
->isFalse()
->when(
$lookbehind->rewind(),
$key = $lookbehind->key(),
$current = $lookbehind->current(),
$hasPrevious = $lookbehind->hasPrevious()
)
->then
->integer($key)
->isEqualTo(0)
->string($current)
->isEqualTo('a')
->boolean($hasPrevious)
->isFalse();
}
public function case_empty()
{
$this
->given(
$iterator = new LUT\Mock(),
$lookbehind = new LUT\Lookbehind($iterator)
)
->when(
$lookbehind->rewind(),
$valid = $lookbehind->valid()
)
->then
->boolean($valid)
->isFalse();
}
}
Test/Unit/Map.php 0000666 00000010026 13371317603 0007637 0 ustar 00 given($iterator = new LUT\Map(self::$_dummyArray))
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo(self::$_dummyArray);
}
public function case_empty()
{
$this
->given($iterator = new LUT\Map())
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEmpty();
}
public function case_recursive()
{
$this
->given(
$array = [
'a' => ['b', 'c', 'd'],
'e' => ['f', 'g', 'i'],
'k'
],
$iterator = new LUT\Recursive\Map($array)
)
->when(function () use ($iterator) {
foreach ($iterator as $key => $value) {
if ('a' === $key) {
$this
->boolean($iterator->hasChildren())
->isTrue()
->object($children = $iterator->getChildren())
->isInstanceOf(LUT\Recursive\Map::class)
->array(iterator_to_array($children))
->isEqualTo(['b', 'c', 'd']);
} elseif ('e' === $key) {
$this
->boolean($iterator->hasChildren())
->isTrue()
->object($children = $iterator->getChildren())
->isInstanceOf(LUT\Recursive\Map::class)
->array(iterator_to_array($children))
->isEqualTo(['f', 'g', 'i']);
} elseif ('k' === $value) {
$this
->boolean($iterator->hasChildren())
->isFalse();
}
}
});
}
}
Test/Unit/Mock.php 0000666 00000005652 13371317603 0010024 0 ustar 00 given($iterator = new LUT\Mock())
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEmpty();
}
public function case_recursive_mock_mock()
{
$this
->when($iterator = new LUT\Recursive\Mock(new LUT\Mock()))
->then
->variable($iterator->getChildren())
->isNull()
->boolean($iterator->hasChildren())
->isFalse();
}
public function case_recursive()
{
$this
->given(
$map = new LUT\Map(['a', 'b', 'c']),
$mock = new LUT\Recursive\Mock($map),
$iteratoriterator = new LUT\Recursive\Iterator($mock)
)
->when($result = iterator_to_array($map, false))
->then
->array($result)
->isEqualTo(['a', 'b', 'c']);
}
}
Test/Unit/Multiple.php 0000666 00000007717 13371317603 0010732 0 ustar 00 given(
$foo = new LUT\Map(['f', 'o', 'o']),
$bar = new LUT\Map(['b', 'a', 'r']),
$multiple = new LUT\Multiple(
LUT\Multiple::MIT_NEED_ANY
| LUT\Multiple::MIT_KEYS_ASSOC
),
$multiple->attachIterator($foo, 'one'),
$multiple->attachIterator($bar, 'two')
)
->when($result = iterator_to_array($multiple, false))
->then
->array($result)
->isEqualTo([
['one' => 'f', 'two' => 'b'],
['one' => 'o', 'two' => 'a'],
['one' => 'o', 'two' => 'r']
]);
}
public function case_default_value()
{
$this
->given(
$foobar = new LUT\Map(['f', 'o', 'o', 'b', 'a', 'r']),
$baz = new LUT\Map(['b', 'a', 'z']),
$multiple = new LUT\Multiple(
LUT\Multiple::MIT_NEED_ANY
| LUT\Multiple::MIT_KEYS_ASSOC
),
$multiple->attachIterator($foobar, 'one', '!'),
$multiple->attachIterator($baz, 'two', '?')
)
->when($result = iterator_to_array($multiple, false))
->then
->array($result)
->isEqualTo([
['one' => 'f', 'two' => 'b'],
['one' => 'o', 'two' => 'a'],
['one' => 'o', 'two' => 'z'],
['one' => 'b', 'two' => '?'],
['one' => 'a', 'two' => '?'],
['one' => 'r', 'two' => '?'],
]);
}
public function case_empty()
{
$this
->given($multiple = new LUT\Multiple())
->when($result = iterator_to_array($multiple))
->then
->array($result)
->isEmpty();
}
}
Test/Unit/NoRewind.php 0000666 00000005121 13371317603 0010647 0 ustar 00 given(
$dummyArray = ['f', 'o', 'o', 'b', 'a', 'r'],
$iterator = new LUT\Map($dummyArray),
$norewind = new LUT\NoRewind($iterator)
)
->when($result = iterator_to_array($norewind))
->then
->array($result)
->isEqualTo($dummyArray)
->when($norewind->rewind())
->boolean($norewind->valid())
->isFalse()
->when($result = iterator_to_array($norewind))
->then
->array($result)
->isEmpty();
}
}
Test/Unit/RegularExpression.php 0000666 00000010504 13371317603 0012604 0 ustar 00 given(
$map = new LUT\Map([
'abc',
'dea',
'fgh',
'iaj',
'klm'
]),
$iterator = new LUT\RegularExpression($map, '/a/')
)
->when($result = iterator_to_array($iterator))
->then
->array($result)
->isEqualTo([
0 => 'abc',
1 => 'dea',
3 => 'iaj'
]);
}
public function case_recursive()
{
$this
->given(
$map = new LUT\Recursive\Map([
['abc', 'dea', 'fgh'],
['iaj', 'klm']
]),
$regex = new LUT\Recursive\RegularExpression($map, '/a/'),
$iterator = new LUT\Recursive\Iterator($regex)
)
->when($result = iterator_to_array($iterator, false))
->then
->array($result)
->isEqualTo([
0 => 'abc',
1 => 'dea',
2 => 'iaj'
]);
}
public function case_recursive_children_flags()
{
$this
->given(
$map = new LUT\Recursive\Map([
['abc', 'dea', 'fgh'],
['iaj', 'klm']
]),
$mode = LUT\Recursive\RegularExpression::ALL_MATCHES,
$flag = LUT\Recursive\RegularExpression::USE_KEY,
$pregFlag = LUT\Recursive\RegularExpression::ALL_MATCHES,
$iterator = new LUT\Recursive\RegularExpression(
$map,
'/a/',
$mode,
$flag,
$pregFlag
)
)
->when($result = $iterator->getChildren())
->then
->object($result)
->isInstanceOf(LUT\Recursive\RegularExpression::class)
->integer($result->getMode())
->isEqualTo($mode)
->integer($result->getFlags())
->isEqualTo($flag)
->integer($result->getPregFlags())
->isEqualTo($pregFlag);
}
}
Test/Unit/Repeater.php 0000666 00000006627 13371317603 0010705 0 ustar 00 given(
$iterator = new LUT\Map(self::$_dummyArray),
$repeater = new LUT\Repeater($iterator, 3)
)
->when($result = iterator_to_array($repeater))
->then
->array($result)
->isEqualTo(
self::$_dummyArray +
self::$_dummyArray +
self::$_dummyArray
);
}
public function case_with_body()
{
$self = $this;
$this
->given(
$iterator = new LUT\Map(self::$_dummyArray),
$count = 0,
$repeater = new LUT\Repeater(
$iterator,
3,
function ($repetition) use ($self, &$count) {
$this
->integer($repetition)
->isEqualTo($count + 1);
++$count;
})
)
->when($result = iterator_to_array($repeater))
->then
->array($result)
->isEqualTo(
self::$_dummyArray +
self::$_dummyArray +
self::$_dummyArray
)
->integer($count)
->isEqualTo(3);
}
}
Test/Unit/SplFileInfo.php 0000666 00000014724 13371317603 0011305 0 ustar 00 given($pathname = 'hoa://Test/Vfs/Foo.bar?type=file')
->when($result = new LUT\SplFileInfo($pathname))
->then
->boolean($result->isFile())
->isTrue()
->string($result->getType())
->isEqualTo('file');
}
public function case_directory()
{
$this
->given($pathname = 'hoa://Test/Vfs/Foo?type=directory')
->when($result = new LUT\SplFileInfo($pathname))
->then
->boolean($result->isDir())
->isTrue()
->string($result->getType())
->isEqualTo('dir');
}
public function case_path_informations()
{
$this
->given(
$relativePath = 'hoa://Test/Vfs/A/B/',
$relativePathname = 'C/Foo.bar',
$pathname = $relativePath . $relativePathname
)
->when($result = new LUT\SplFileInfo($pathname . '?type=file', $relativePath))
->then
->boolean($result->isFile())
->isTrue()
->string($result->getBasename())
->isEqualTo('Foo.bar?type=file')
->string($result->getExtension())
->isEqualTo('bar?type=file')
->string($result->getRelativePath())
->isEqualTo($relativePath)
->string($result->getRelativePathname())
->isEqualTo($relativePathname . '?type=file')
->string($result->getPath())
->isEqualTo('hoa://Test/Vfs/A/B/C')
->string($result->getPathname())
->isEqualTo($pathname . '?type=file');
}
public function case_times()
{
$this
->given(
$timestamp = $this->realdom->boundinteger(
$this->realdom->timestamp('yesterday'),
$this->realdom->timestamp('tomorrow')
),
$atime = $this->sample($timestamp),
$ctime = $this->sample($timestamp),
$mtime = $this->sample($timestamp),
$pathname =
'hoa://Test/Vfs/Foo.bar?' .
http_build_query([
'type' => 'file',
'atime' => $atime,
'ctime' => $ctime,
'mtime' => $mtime
])
)
->when($result = new LUT\SplFileInfo($pathname))
->then
->integer($result->getATime())
->isEqualTo($atime)
->integer($result->getCTime())
->isEqualTo($ctime)
->integer($result->getMTime())
->isEqualTo($mtime);
}
public function case_permissions()
{
$this
->given($pathname = 'hoa://Test/Vfs/Fo.bar?type=file&permissions=0744')
->when($result = new LUT\SplFileInfo($pathname))
->then
->boolean($result->isReadable())
->isTrue()
->boolean($result->isWritable())
->isTrue()
->boolean($result->isExecutable())
->isTrue()
->given($pathname = 'hoa://Test/Vfs/Foo.bar?type=file&permissions=0644')
->when($result = new LUT\SplFileInfo($pathname))
->then
->boolean($result->isReadable())
->isTrue()
->boolean($result->isWritable())
->isTrue()
->boolean($result->isExecutable())
->isFalse()
->given($pathname = 'hoa://Test/Vfs/Fooo.bar?type=file&permissions=0444')
->when($result = new LUT\SplFileInfo($pathname))
->then
->boolean($result->isReadable())
->isTrue()
->boolean($result->isWritable())
->isFalse()
->boolean($result->isExecutable())
->isFalse()
->given($pathname = 'hoa://Test/Vfs/Foooo.bar?type=file&permissions=0044')
->when($result = new LUT\SplFileInfo($pathname))
->then
->boolean($result->isReadable())
->isFalse()
->boolean($result->isWritable())
->isFalse()
->boolean($result->isExecutable())
->isFalse();
}
}
composer.json 0000666 00000002215 13371317603 0007276 0 ustar 00 {
"name" : "hoa/iterator",
"description": "The Hoa\\Iterator library.",
"type" : "library",
"keywords" : ["library", "iterator"],
"homepage" : "https://hoa-project.net/",
"license" : "BSD-3-Clause",
"authors" : [
{
"name" : "Ivan Enderlin",
"email": "ivan.enderlin@hoa-project.net"
},
{
"name" : "Hoa community",
"homepage": "https://hoa-project.net/"
}
],
"support": {
"email" : "support@hoa-project.net",
"irc" : "irc://chat.freenode.net/hoaproject",
"forum" : "https://users.hoa-project.net/",
"docs" : "https://central.hoa-project.net/Documentation/Library/Iterator",
"source": "https://central.hoa-project.net/Resource/Library/Iterator"
},
"require": {
"hoa/consistency": "~1.0",
"hoa/exception" : "~1.0"
},
"require-dev": {
"hoa/test": "~2.0"
},
"autoload": {
"psr-4": {
"Hoa\\Iterator\\": "."
}
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
}
}