-
-
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
3 changed files
with
138 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\loophp\collection\Iterator; | ||
|
||
use ArrayIterator; | ||
use loophp\collection\Iterator\RandomIterator; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class RandomIteratorSpec extends ObjectBehavior | ||
{ | ||
public function it_can_get_the_innerIterator() | ||
{ | ||
$this->getInnerIterator()->shouldBeAnInstanceOf(ArrayIterator::class); | ||
} | ||
|
||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(RandomIterator::class); | ||
} | ||
|
||
public function let() | ||
{ | ||
$iterator = new ArrayIterator(range(0, 4)); | ||
|
||
$this->beConstructedWith($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,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Iterator; | ||
|
||
use ArrayIterator; | ||
use Iterator; | ||
use OuterIterator; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T of string | ||
* | ||
* @implements Iterator<TKey, T> | ||
*/ | ||
final class RandomIterator implements Iterator, OuterIterator | ||
{ | ||
/** | ||
* @var array<int, int> | ||
*/ | ||
private $indexes; | ||
|
||
/** | ||
* @var Iterator | ||
* @psalm-var Iterator<TKey, T> | ||
*/ | ||
private $inner; | ||
|
||
/** | ||
* @var ArrayIterator | ||
* @psalm-var ArrayIterator<int, array{0: TKey, 1: T}> | ||
*/ | ||
private $iterator; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $key; | ||
|
||
/** | ||
* @psalm-param Iterator<TKey, T> $iterator | ||
*/ | ||
public function __construct(Iterator $iterator) | ||
{ | ||
$this->inner = $iterator; | ||
$this->iterator = $this->buildArrayIterator($iterator); | ||
$this->indexes = array_keys($this->iterator->getArrayCopy()); | ||
$this->key = array_rand($this->indexes); | ||
} | ||
|
||
public function current() | ||
{ | ||
$value = $this->iterator[$this->key]; | ||
|
||
return $value[1]; | ||
} | ||
|
||
public function getInnerIterator() | ||
{ | ||
return $this->inner; | ||
} | ||
|
||
public function key() | ||
{ | ||
$value = $this->iterator[$this->key]; | ||
|
||
return $value[0]; | ||
} | ||
|
||
public function next(): void | ||
{ | ||
unset($this->indexes[$this->key]); | ||
|
||
if ($this->valid()) { | ||
$this->key = array_rand($this->indexes); | ||
} | ||
} | ||
|
||
public function rewind() | ||
{ | ||
$this->iterator->rewind(); | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
return [] !== $this->indexes; | ||
} | ||
|
||
/** | ||
* @psalm-param Iterator<TKey, T> $iterator | ||
* | ||
* @psalm-return ArrayIterator<int, array{0: TKey, 1: T}> | ||
*/ | ||
private function buildArrayIterator(Iterator $iterator): ArrayIterator | ||
{ | ||
/** @psalm-var ArrayIterator<int, array{0: TKey, 1: T}> $arrayIterator */ | ||
$arrayIterator = new ArrayIterator(); | ||
|
||
foreach ($iterator as $key => $value) { | ||
$arrayIterator->append([$key, $value]); | ||
} | ||
|
||
return $arrayIterator; | ||
} | ||
} |
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