Skip to content

Commit

Permalink
Merge pull request #4 from samsonasik/allow-iterable
Browse files Browse the repository at this point in the history
Allow any iterable type
  • Loading branch information
samsonasik authored Jan 8, 2023
2 parents 78fac3f + 4a11326 commit de2007f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ArrayLookup
Introduction
------------

ArrayLookup is a fast array lookup library that help you verify and search array data.
ArrayLookup is a fast array lookup library that help you verify and search `array` and `Traversable` data.

Features
--------
Expand Down
16 changes: 8 additions & 8 deletions src/AtLeast.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@
final class AtLeast
{
/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
public static function once(array $data, callable $filter): bool
public static function once(iterable $data, callable $filter): bool
{
return self::hasFoundTimes($data, $filter, 1);
}

/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
public static function twice(array $data, callable $filter): bool
public static function twice(iterable $data, callable $filter): bool
{
return self::hasFoundTimes($data, $filter, 2);
}

/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
public static function times(array $data, callable $filter, int $count): bool
public static function times(iterable $data, callable $filter, int $count): bool
{
return self::hasFoundTimes($data, $filter, $count);
}

/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
private static function hasFoundTimes(array $data, callable $filter, int $maxCount): bool
private static function hasFoundTimes(iterable $data, callable $filter, int $maxCount): bool
{
// usage must be higher than 0
Assert::greaterThan($maxCount, 0);
Expand Down
18 changes: 12 additions & 6 deletions src/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,42 @@

namespace ArrayLookup;

use Traversable;
use Webmozart\Assert\Assert;

use function array_reverse;
use function iterator_to_array;

final class Finder
{
/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
public static function first(array $data, callable $filter): mixed
public static function first(iterable $data, callable $filter): mixed
{
return self::locateFirst($data, $filter);
}

/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
public static function last(array $data, callable $filter): mixed
public static function last(iterable $data, callable $filter): mixed
{
return self::locateFirst($data, $filter, true);
}

/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
private static function locateFirst(array $data, callable $filter, bool $flip = false): mixed
private static function locateFirst(iterable $data, callable $filter, bool $flip = false): mixed
{
if ($data instanceof Traversable) {
$data = iterator_to_array($data);
}

if ($flip) {
$data = array_reverse($data);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Only.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@
final class Only
{
/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
public static function once(array $data, callable $filter): bool
public static function once(iterable $data, callable $filter): bool
{
return self::onlyFoundTimes($data, $filter, 1);
}

/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
public static function twice(array $data, callable $filter): bool
public static function twice(iterable $data, callable $filter): bool
{
return self::onlyFoundTimes($data, $filter, 2);
}

/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
public static function times(array $data, callable $filter, int $count): bool
public static function times(iterable $data, callable $filter, int $count): bool
{
return self::onlyFoundTimes($data, $filter, $count);
}

/**
* @param mixed[] $data
* @param mixed[]|iterable $data
* @param callable(mixed $datum): bool $filter
*/
private static function onlyFoundTimes(array $data, callable $filter, int $maxCount): bool
private static function onlyFoundTimes(iterable $data, callable $filter, int $maxCount): bool
{
// usage must be higher than 0
Assert::greaterThan($maxCount, 0);
Expand Down
25 changes: 23 additions & 2 deletions tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace ArrayLookup\Tests;

use ArrayLookup\Finder;
use ArrayObject;
use PHPUnit\Framework\TestCase;

final class FinderTest extends TestCase
{
/**
* @dataProvider firstDataProvider
*/
public function testFirst(array $data, callable $filter, mixed $expected): void
public function testFirst(iterable $data, callable $filter, mixed $expected): void
{
$this->assertSame(
$expected,
Expand All @@ -33,13 +34,23 @@ public function firstDataProvider(): array
static fn($datum): bool => $datum === 1000,
null,
],
[
new ArrayObject([1, 2, 3]),
static fn($datum): bool => $datum === 2,
2,
],
[
new ArrayObject([1, "1", 3]),
static fn($datum): bool => $datum === 1000,
null,
],
];
}

/**
* @dataProvider lastDataProvider
*/
public function testLast(array $data, callable $filter, mixed $expected): void
public function testLast(iterable $data, callable $filter, mixed $expected): void
{
$this->assertSame(
$expected,
Expand All @@ -60,6 +71,16 @@ public function lastDataProvider(): array
static fn($datum): bool => $datum < 5,
null,
],
[
new ArrayObject([6, 7, 8, 9]),
static fn($datum): bool => $datum > 5,
9,
],
[
new ArrayObject([6, 7, 8, 9]),
static fn($datum): bool => $datum < 5,
null,
],
];
}
}

0 comments on commit de2007f

Please sign in to comment.