Skip to content

Commit 13fbcb1

Browse files
test first
1 parent 323548c commit 13fbcb1

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"src/Add.php",
3939
"src/Set.php",
4040
"src/Pipe.php",
41+
"src/First.php",
4142
"src/Flatten.php",
4243
"src/Expand.php",
4344
"src/DotNotation.php",

src/First.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt;
5+
6+
/**
7+
* Returns the first element that matches the specification or a default value
8+
*
9+
* @param iterable $items The collection
10+
* @param callable $callback The specification must return a boolean
11+
* @param mixed $default default value
12+
* @param int $mode [optional] <p>
13+
* Flag determining what arguments are sent to <i>callback</i>:
14+
* </p><ul>
15+
* <li>
16+
* <b>CALLBACK_USE_VALUE</b> - <b>default</b> pass value as the only argument
17+
* </li>
18+
* <li>
19+
* <b>CALLBACK_USE_KEY</b> - pass key as the only argument
20+
* to <i>callback</i> instead of the value</span>
21+
* </li>
22+
* <li>
23+
* <b>CALLBACK_USE_BOTH</b> - pass both value and key as
24+
* arguments to <i>callback</i></span>
25+
* </li>
26+
* </ul>
27+
* @return mixed return the corresponding element or default value
28+
*/
29+
function first(iterable $items, callable $callback, $default = null, int $mode = CALLBACK_USE_VALUE)
30+
{
31+
foreach ($items as $key => $value) {
32+
if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
33+
return $value;
34+
}
35+
}
36+
37+
return $default;
38+
}

src/array_functions.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33

44
namespace BrenoRoosevelt;
55

6-
function first(iterable $items, callable $callback, $default = null, int $mode = CALLBACK_USE_VALUE)
7-
{
8-
foreach ($items as $key => $value) {
9-
if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
10-
return $value;
11-
}
12-
}
13-
14-
return $default;
15-
}
16-
176
function map(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): array
187
{
198
$result = [];

tests/FirstTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use function BrenoRoosevelt\first;
8+
use function BrenoRoosevelt\pull;
9+
10+
class FirstTest extends TestCase
11+
{
12+
public function testGetFirst()
13+
{
14+
$result = first([1, 'a' => 2, 'b' => 3], fn($e) => $e % 2 === 0);
15+
$this->assertEquals(2, $result);
16+
}
17+
18+
public function testFirstRetunsDefault()
19+
{
20+
$result = first([1, 'a' => 2, 'b' => 3], fn($e) => $e === 0, 'default');
21+
$this->assertEquals('default', $result);
22+
}
23+
}

0 commit comments

Comments
 (0)