Skip to content

Commit

Permalink
Merge pull request #10 from samsonasik/add-ability-to-not-preserve-key
Browse files Browse the repository at this point in the history
Add $preserveKey flag ability to not preserve key on Finder::last()
  • Loading branch information
samsonasik authored Jan 14, 2023
2 parents cd0666c + 02c5b8e commit a387403
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,21 @@ var_dump(\ArrayLookup\Finder::last(

// RETURN the Array key, pass true to 3rd arg

// ... with PRESERVE original key
var_dump(\ArrayLookup\Finder::last(
$data,
static fn ($datum): bool => $datum > 5,
true
)); // 3

// ... with RESORT key, first key is last record
var_dump(\ArrayLookup\Finder::last(
$data,
static fn ($datum): bool => $datum > 5,
true,
false
)); // 0

var_dump(\ArrayLookup\Finder::last(
$data,
static fn ($datum): bool => $datum < 5,
Expand Down
25 changes: 21 additions & 4 deletions src/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ private static function resolveArrayFromTraversable(Traversable $traversable): a
* @param array<int|string, mixed>|Traversable<int|string, mixed> $data
* @param callable(mixed $datum, int|string|null $key=): bool $filter
*/
public static function last(iterable $data, callable $filter, bool $returnKey = false): mixed
{
public static function last(
iterable $data,
callable $filter,
bool $returnKey = false,
bool $preserveKey = true
): mixed {
// convert to array when data is Traversable instance
if ($data instanceof Traversable) {
$data = self::resolveArrayFromTraversable($data);
Expand All @@ -74,10 +78,15 @@ public static function last(iterable $data, callable $filter, bool $returnKey =
end($data);

// grab current key
$key = key($data);
$key = key($data);
$resortkey = -1;

// key = null means no longer current data
while ($key !== null) {
if (! $preserveKey && $returnKey) {
++$resortkey;
}

$current = current($data);
$isFound = $filter($current, $key);

Expand All @@ -94,7 +103,15 @@ public static function last(iterable $data, callable $filter, bool $returnKey =
continue;
}

return $returnKey ? $key : $current;
if (! $returnKey) {
return $current;
}

if ($preserveKey) {
return $key;
}

return $resortkey;
}

return null;
Expand Down
27 changes: 27 additions & 0 deletions tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,31 @@ public function lastReturnKeyDataProvider(): array
],
];
}

/**
* @dataProvider lastReturnKeyResortKeyDataProvider
*/
public function testLastReturnKeyResortKey(iterable $data, callable $filter, mixed $expected): void
{
$this->assertSame(
$expected,
Finder::last($data, $filter, true, false)
);
}

public function lastReturnKeyResortKeyDataProvider(): array
{
return [
[
[6, 7, 8, 9],
static fn($datum): bool => $datum > 5,
0,
],
[
[6, 7, 8, 9],
static fn($datum): bool => $datum < 5,
null,
],
];
}
}

0 comments on commit a387403

Please sign in to comment.