Skip to content

Commit 0d93283

Browse files
authored
feat(support): support $default on array first and last methods (#1096)
1 parent af5e9ab commit 0d93283

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

src/Tempest/Support/src/Arr/ManipulatesArray.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ public function equals(array|self $other): bool
336336
*
337337
* @return TValue
338338
*/
339-
public function first(?Closure $filter = null): mixed
339+
public function first(?Closure $filter = null, mixed $default = null): mixed
340340
{
341-
return namespace\first($this->value, $filter);
341+
return namespace\first($this->value, $filter, $default);
342342
}
343343

344344
/**
@@ -349,14 +349,24 @@ public function first(?Closure $filter = null): mixed
349349
*
350350
* @return TValue
351351
*/
352-
public function last(?Closure $filter = null): mixed
352+
public function last(?Closure $filter = null, mixed $default = null): mixed
353353
{
354-
return namespace\last($this->value, $filter);
354+
return namespace\last($this->value, $filter, $default);
355355
}
356356

357357
/**
358358
* Returns the item at the given index in the specified array.
359+
* @alias of `at()`
359360
*
361+
* @return TValue
362+
*/
363+
public function nth(int $index, mixed $default = null): mixed
364+
{
365+
return $this->at($index, $default);
366+
}
367+
368+
/**
369+
* Returns the item at the given index in the specified array.
360370
*
361371
* @return TValue
362372
*/

src/Tempest/Support/src/Arr/functions.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -492,19 +492,19 @@ function equals(iterable $array, iterable $other): bool
492492
*
493493
* @return TValue
494494
*/
495-
function first(iterable $array, ?Closure $filter = null): mixed
495+
function first(iterable $array, ?Closure $filter = null, mixed $default = null): mixed
496496
{
497497
$array = to_array($array);
498498

499499
if ($array === []) {
500-
return null;
500+
return $default;
501501
}
502502

503503
if ($filter === null) {
504-
return $array[array_key_first($array)];
504+
return $array[array_key_first($array)] ?? $default;
505505
}
506506

507-
return array_find($array, static fn ($value, $key) => $filter($value, $key));
507+
return array_find($array, static fn ($value, $key) => $filter($value, $key)) ?? $default;
508508
}
509509

510510
/**
@@ -541,19 +541,19 @@ function at(iterable $array, int $index, mixed $default = null): mixed
541541
*
542542
* @return TValue
543543
*/
544-
function last(iterable $array, ?Closure $filter = null): mixed
544+
function last(iterable $array, ?Closure $filter = null, mixed $default = null): mixed
545545
{
546546
$array = to_array($array);
547547

548548
if ($array === []) {
549-
return null;
549+
return $default;
550550
}
551551

552552
if ($filter === null) {
553-
return $array[array_key_last($array)];
553+
return $array[array_key_last($array)] ?? $default;
554554
}
555555

556-
return array_find(namespace\reverse($array), static fn ($value, $key) => $filter($value, $key));
556+
return array_find(namespace\reverse($array), static fn ($value, $key) => $filter($value, $key)) ?? $default;
557557
}
558558

559559
/**

src/Tempest/Support/tests/Arr/ManipulatesArrayTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,18 @@ public function test_last(): void
204204
{
205205
$this->assertSame(null, arr()->last());
206206
$this->assertSame('c', arr(['a', 'b', 'c'])->last());
207+
208+
$this->assertSame('foo', arr()->last(default: 'foo'));
209+
$this->assertSame(2, arr([1, 2])->last(default: 'foo'));
207210
}
208211

209212
public function test_first(): void
210213
{
211214
$this->assertSame('a', arr(['a', 'b', 'c'])->first());
212215
$this->assertSame(null, arr()->first());
216+
217+
$this->assertSame('foo', arr()->first(default: 'foo'));
218+
$this->assertSame(1, arr([1, 2])->first(default: 'foo'));
213219
}
214220

215221
public function test_is_empty(): void
@@ -1778,5 +1784,6 @@ public function test_group_by(): void
17781784
public function test_at(array $input, int $index, mixed $expected, mixed $default = null): void
17791785
{
17801786
$this->assertSame($expected, arr($input)->at($index, $default));
1787+
$this->assertSame($expected, arr($input)->nth($index, $default));
17811788
}
17821789
}

0 commit comments

Comments
 (0)