-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
212 additions
and
106 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
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Iterator; | ||
|
||
use Generator; | ||
use Iterator; | ||
|
||
abstract class ProxyIterator | ||
{ | ||
/** | ||
* @var Generator<mixed>|\loophp\collection\Iterator\ClosureIterator | ||
*/ | ||
protected $iterator; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function current() | ||
{ | ||
return $this->iterator->current(); | ||
} | ||
|
||
/** | ||
* @return Iterator<mixed> | ||
*/ | ||
public function getInnerIterator(): Iterator | ||
{ | ||
return $this->iterator; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function key() | ||
{ | ||
return $this->iterator->key(); | ||
} | ||
|
||
public function next(): void | ||
{ | ||
$this->iterator->next(); | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->iterator->rewind(); | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
return $this->iterator->valid(); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Iterator; | ||
|
||
use Generator; | ||
use Iterator; | ||
use OuterIterator; | ||
|
||
/** | ||
* @implements Iterator<mixed> | ||
*/ | ||
final class ResourceIterator extends ProxyIterator implements Iterator, OuterIterator | ||
{ | ||
/** | ||
* ResourceIterator constructor. | ||
* | ||
* @param resource $resource | ||
*/ | ||
public function __construct($resource) | ||
{ | ||
$closure = | ||
/** | ||
* @param resource $resource | ||
*/ | ||
static function ($resource): Generator { | ||
while (false !== $chunk = fgetc($resource)) { | ||
yield $chunk; | ||
} | ||
}; | ||
|
||
$this->iterator = new ClosureIterator( | ||
$closure, | ||
$resource | ||
); | ||
} | ||
} |
Oops, something went wrong.