-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filters |sort & |group returns CombineIterator
allows multiple iterations
- Loading branch information
Showing
5 changed files
with
163 additions
and
21 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,52 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Latte (https://latte.nette.org) | ||
* Copyright (c) 2008 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Latte\Essential; | ||
|
||
|
||
/** | ||
* @internal | ||
*/ | ||
class CombineIterator implements \Iterator | ||
{ | ||
public function __construct( | ||
private array $pairs, | ||
) { | ||
} | ||
|
||
|
||
public function current(): mixed | ||
{ | ||
return current($this->pairs)[1]; | ||
} | ||
|
||
|
||
public function key(): mixed | ||
{ | ||
return current($this->pairs)[0]; | ||
} | ||
|
||
|
||
public function next(): void | ||
{ | ||
next($this->pairs); | ||
} | ||
|
||
|
||
public function rewind(): void | ||
{ | ||
reset($this->pairs); | ||
} | ||
|
||
|
||
public function valid(): bool | ||
{ | ||
return key($this->pairs) !== null; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
/** | ||
* Test: CombineIterator basic usage. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Latte\Essential\CombineIterator; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
function exportIterator(iterable $iterator): array | ||
{ | ||
$res = []; | ||
foreach ($iterator as $key => $value) { | ||
$res[] = [$key, $value]; | ||
} | ||
return $res; | ||
} | ||
|
||
|
||
test('empty array', function () { | ||
Assert::same( | ||
[], | ||
exportIterator(new CombineIterator([], [])), | ||
); | ||
}); | ||
|
||
|
||
test('single usage', function () { | ||
$arr = [[new stdClass, new stdClass], [null, null]]; | ||
$iterator = new CombineIterator(array_column($arr, 0), array_column($arr, 1)); | ||
Assert::same( | ||
$arr, | ||
exportIterator($iterator), | ||
); | ||
}); | ||
|
||
|
||
test('double usage', function () { | ||
$arr = [[new stdClass, new stdClass], [null, null]]; | ||
$iterator = new CombineIterator(array_column($arr, 0), array_column($arr, 1)); | ||
Assert::same( | ||
$arr, | ||
exportIterator($iterator), | ||
); | ||
Assert::same( | ||
$arr, | ||
exportIterator($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
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