-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
115 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\DataType\Iterators; | ||
|
||
use Iterator; | ||
|
||
/** | ||
* @implements Iterator<mixed, mixed> | ||
*/ | ||
final class ReverseArray implements Iterator | ||
{ | ||
/** | ||
* @param array<mixed, mixed> $array | ||
*/ | ||
public function __construct(private array $array) | ||
{ | ||
} | ||
|
||
|
||
public function current(): mixed | ||
{ | ||
return current($this->array); | ||
} | ||
|
||
|
||
public function next(): void | ||
{ | ||
prev($this->array); | ||
} | ||
|
||
|
||
public function key(): mixed | ||
{ | ||
return key($this->array); | ||
} | ||
|
||
|
||
public function valid(): bool | ||
{ | ||
return key($this->array) !== null; | ||
} | ||
|
||
|
||
public function rewind(): void | ||
{ | ||
end($this->array); | ||
} | ||
|
||
} |
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,49 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\DataType\Tests\Unit\Iterators; | ||
|
||
use h4kuna\DataType\Iterators\ReverseArray; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
require_once __DIR__ . '/bootstrap.php'; | ||
|
||
/** | ||
* @testCase | ||
*/ | ||
final class ReverseArrayTest extends TestCase | ||
{ | ||
/** | ||
* @return array<mixed> | ||
*/ | ||
protected function basicProvider(): array | ||
{ | ||
return [ | ||
[ | ||
[1, 2, 3], | ||
[2 => 3, 1 => 2, 0 => 1], | ||
], | ||
[ | ||
[], | ||
[], | ||
], | ||
]; | ||
} | ||
|
||
|
||
/** | ||
* @dataProvider basicProvider | ||
* @param array<mixed> $source | ||
* @param array<mixed> $expected | ||
*/ | ||
public function testBasic(array $source, array $expected): void | ||
{ | ||
$actual = []; | ||
foreach (new ReverseArray($source) as $k => $v) { | ||
$actual[$k] = $v; | ||
} | ||
Assert::same($expected, $actual); | ||
} | ||
} | ||
|
||
(new ReverseArrayTest())->run(); |