Skip to content

Commit cd0666c

Browse files
authored
Merge pull request #9 from samsonasik/add-returnKey-flag
Add returnKey flag on Finder to allow return key instead of row
2 parents 78165e3 + 8d18e27 commit cd0666c

File tree

3 files changed

+146
-12
lines changed

3 files changed

+146
-12
lines changed

README.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ $filter = static fn($datum): bool => $datum === 4;
4949

5050
var_dump(\ArrayLookup\AtLeast::once($data, $filter)) // false
5151

52-
// WITH key array included
52+
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter
5353

5454
$data = [1, 2, 3];
5555
$filter = static fn($datum, $key): bool => $datum === 1 && $key >= 0;
@@ -77,7 +77,7 @@ $filter = static fn($datum): bool => $datum === 1;
7777

7878
var_dump(\ArrayLookup\AtLeast::twice($data, $filter)) // false
7979

80-
// WITH key array included
80+
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter
8181

8282
$data = [1, "1", 3];
8383
$filter = static fn($datum, $key): bool => $datum == 1 && $key >= 0;
@@ -107,7 +107,7 @@ $times = 3;
107107

108108
var_dump(\ArrayLookup\AtLeast::times($data, $filter, $times)) // false
109109

110-
// WITH key array included
110+
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter
111111

112112
$data = [false, null, 0];
113113
$filter = static fn($datum, $key): bool => ! $datum && $key >= 0;
@@ -141,7 +141,7 @@ $filter = static fn($datum): bool => $datum == 1;
141141

142142
var_dump(\ArrayLookup\Only::once($data, $filter)) // false
143143

144-
// WITH key array included
144+
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter
145145

146146
$data = [1, 2, 3];
147147
$filter = static fn($datum, $key): bool => $datum === 1 && $key >= 0;
@@ -170,7 +170,7 @@ $filter = static fn($datum): bool => (bool) $datum;
170170

171171
var_dump(\ArrayLookup\Only::twice($data, $filter)) // false
172172

173-
// WITH key array included
173+
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter
174174

175175
$data = [1, "1", 3];
176176
$filter = static fn($datum, $key): bool => $datum == 1 && $key >= 0;
@@ -201,7 +201,7 @@ $times = 2;
201201

202202
var_dump(\ArrayLookup\Only::times($data, $filter, $times)) // false
203203

204-
// WITH key array included
204+
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter
205205

206206
$data = [false, null, 1];
207207
$filter = static fn($datum, $key): bool => ! $datum && $key >= 0;
@@ -233,7 +233,16 @@ var_dump(\ArrayLookup\Finder::first($data, $filter)) // 1
233233
$filter = static fn($datum): bool => $datum == 1000;
234234
var_dump(\ArrayLookup\Finder::first($data, $filter)) // null
235235

236-
// WITH key array included
236+
// RETURN the Array key, pass true to 3rd arg
237+
238+
$filter = static fn($datum): bool => $datum === 1;
239+
240+
var_dump(\ArrayLookup\Finder::first($data, $filter, true)) // 0
241+
242+
$filter = static fn($datum): bool => $datum == 1000;
243+
var_dump(\ArrayLookup\Finder::first($data, $filter, true)) // null
244+
245+
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter
237246

238247
$filter = static fn($datum, $key): bool => $datum === 1 && $key >= 0;
239248

@@ -259,7 +268,21 @@ var_dump(\ArrayLookup\Finder::last(
259268
static fn ($datum): bool => $datum < 5
260269
)); // null
261270

262-
// WITH key array included
271+
// RETURN the Array key, pass true to 3rd arg
272+
273+
var_dump(\ArrayLookup\Finder::last(
274+
$data,
275+
static fn ($datum): bool => $datum > 5,
276+
true
277+
)); // 3
278+
279+
var_dump(\ArrayLookup\Finder::last(
280+
$data,
281+
static fn ($datum): bool => $datum < 5,
282+
true
283+
)); // null
284+
285+
// WITH key array included, pass $key variable as 2nd arg on filter to be used in filter
263286

264287
var_dump(\ArrayLookup\Finder::last(
265288
$data,

src/Finder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class Finder
2121
* @param array<int|string, mixed>|Traversable<int|string, mixed> $data
2222
* @param callable(mixed $datum, int|string|null $key=): bool $filter
2323
*/
24-
public static function first(iterable $data, callable $filter): mixed
24+
public static function first(iterable $data, callable $filter, bool $returnKey = false): mixed
2525
{
2626
foreach ($data as $key => $datum) {
2727
$isFound = $filter($datum, $key);
@@ -33,7 +33,7 @@ public static function first(iterable $data, callable $filter): mixed
3333
continue;
3434
}
3535

36-
return $datum;
36+
return $returnKey ? $key : $datum;
3737
}
3838

3939
return null;
@@ -56,7 +56,7 @@ private static function resolveArrayFromTraversable(Traversable $traversable): a
5656
* @param array<int|string, mixed>|Traversable<int|string, mixed> $data
5757
* @param callable(mixed $datum, int|string|null $key=): bool $filter
5858
*/
59-
public static function last(iterable $data, callable $filter): mixed
59+
public static function last(iterable $data, callable $filter, bool $returnKey = false): mixed
6060
{
6161
// convert to array when data is Traversable instance
6262
if ($data instanceof Traversable) {
@@ -94,7 +94,7 @@ public static function last(iterable $data, callable $filter): mixed
9494
continue;
9595
}
9696

97-
return $current;
97+
return $returnKey ? $key : $current;
9898
}
9999

100100
return null;

tests/FinderTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,43 @@ public function firstDataProvider(): array
5151
];
5252
}
5353

54+
/**
55+
* @dataProvider firstReturnKeyDataProvider
56+
*/
57+
public function testFirstReturnKey(iterable $data, callable $filter, mixed $expected): void
58+
{
59+
$this->assertSame(
60+
$expected,
61+
Finder::first($data, $filter, true)
62+
);
63+
}
64+
65+
public function firstReturnKeyDataProvider(): array
66+
{
67+
return [
68+
[
69+
[1, 2, 3],
70+
static fn($datum): bool => $datum === 2,
71+
1,
72+
],
73+
[
74+
[1, "1", 3],
75+
static fn($datum): bool => $datum === 1000,
76+
null,
77+
],
78+
[
79+
['abc test', 'def', 'some test'],
80+
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key >= 0,
81+
0,
82+
],
83+
[
84+
['abc test', 'def', 'some test'],
85+
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key === 1,
86+
null,
87+
],
88+
];
89+
}
90+
5491
/**
5592
* @dataProvider lastDataProvider
5693
*/
@@ -124,4 +161,78 @@ public function lastDataProvider(): array
124161
],
125162
];
126163
}
164+
165+
/**
166+
* @dataProvider lastReturnKeyDataProvider
167+
*/
168+
public function testLastReturnKey(iterable $data, callable $filter, mixed $expected): void
169+
{
170+
$this->assertSame(
171+
$expected,
172+
Finder::last($data, $filter, true)
173+
);
174+
}
175+
176+
public function lastReturnKeyDataProvider(): array
177+
{
178+
$generator = static function (): Generator {
179+
yield 6;
180+
yield 7;
181+
yield 8;
182+
yield 9;
183+
};
184+
185+
return [
186+
[
187+
[6, 7, 8, 9],
188+
static fn($datum): bool => $datum > 5,
189+
3,
190+
],
191+
[
192+
[6, 7, 8, 9],
193+
static fn($datum): bool => $datum < 5,
194+
null,
195+
],
196+
[
197+
new ArrayIterator([6, 7, 8, 9]),
198+
static fn($datum): bool => $datum > 5,
199+
3,
200+
],
201+
[
202+
new ArrayIterator([6, 7, 8, 9]),
203+
static fn($datum): bool => $datum < 5,
204+
null,
205+
],
206+
[
207+
new ArrayObject([6, 7, 8, 9]),
208+
static fn($datum): bool => $datum > 5,
209+
3,
210+
],
211+
[
212+
new ArrayObject([6, 7, 8, 9]),
213+
static fn($datum): bool => $datum < 5,
214+
null,
215+
],
216+
[
217+
$generator(),
218+
static fn($datum): bool => $datum > 5,
219+
3,
220+
],
221+
[
222+
$generator(),
223+
static fn($datum): bool => $datum < 5,
224+
null,
225+
],
226+
[
227+
['abc test', 'def', 'some test'],
228+
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key >= 0,
229+
2,
230+
],
231+
[
232+
['abc test', 'def', 'some test'],
233+
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key === 1,
234+
null,
235+
],
236+
];
237+
}
127238
}

0 commit comments

Comments
 (0)