-
-
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 KeyValueIterator
allows serial & parallel iterations
- Loading branch information
Showing
5 changed files
with
172 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
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,30 @@ | ||
<?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 KeyValueIterator implements \IteratorAggregate | ||
{ | ||
public function __construct( | ||
private readonly array $pairs, | ||
) { | ||
} | ||
|
||
|
||
public function getIterator(): \Generator | ||
{ | ||
foreach ($this->pairs as [$key, $value]) { | ||
yield $key => $value; | ||
} | ||
} | ||
} |
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,70 @@ | ||
<?php | ||
|
||
/** | ||
* Test: KeyValueIterator usage. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Latte\Essential\KeyValueIterator; | ||
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 KeyValueIterator([])), | ||
); | ||
}); | ||
|
||
|
||
test('single usage', function () { | ||
$pairs = [[new stdClass, new stdClass], [null, null]]; | ||
$iterator = new KeyValueIterator($pairs); | ||
Assert::same( | ||
$pairs, | ||
exportIterator($iterator), | ||
); | ||
}); | ||
|
||
|
||
test('serial usage', function () { | ||
$pairs = [[0, 0], [1, 1], [2, 2]]; | ||
$iterator = new KeyValueIterator($pairs); | ||
Assert::same( | ||
$pairs, | ||
exportIterator($iterator), | ||
); | ||
Assert::same( | ||
$pairs, | ||
exportIterator($iterator), | ||
); | ||
}); | ||
|
||
|
||
test('parallel usage', function () { | ||
$pairs = [[0, 0], [1, 1], [2, 2]]; | ||
$keys = []; | ||
$iterator = new KeyValueIterator($pairs); | ||
foreach ($iterator as $key => $value) { | ||
$keys[] = $key; | ||
foreach ($iterator as $value); | ||
} | ||
|
||
Assert::same( | ||
[0, 1, 2], | ||
$keys, | ||
); | ||
}); |
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