Skip to content

Commit b8e7abf

Browse files
committed
Add Collection.invokeMap function
1 parent 63a50d3 commit b8e7abf

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

src/Collection/invokeMap.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
// No 'declare(strict_types=1)' here as the invoked method needs to use weak-type
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2017
10+
*/
11+
12+
namespace _;
13+
14+
use function _\internal\baseInvoke;
15+
use function _\internal\baseRest;
16+
17+
/**
18+
* Invokes the method at `path` of each element in `collection`, returning
19+
* an array of the results of each invoked method. Any additional arguments
20+
* are provided to each invoked method. If `path` is a function, it's invoked
21+
* for, and `this` bound to, each element in `collection`.
22+
*
23+
* @category Collection
24+
*
25+
* @param iterable $collection The collection to iterate over.
26+
* @param array|callable|string $path The path of the method to invoke or the function invoked per iteration.
27+
* @param array $args The arguments to invoke each method with.
28+
*
29+
* @return array the array of results.
30+
* @example
31+
* <code>
32+
* invokeMap([[5, 1, 7], [3, 2, 1]], function($result) { sort($result); return $result;})
33+
* // => [[1, 5, 7], [1, 2, 3]]
34+
*
35+
* invokeMap([123, 456], 'str_split')
36+
* // => [['1', '2', '3'], ['4', '5', '6']]
37+
* </code>
38+
*/
39+
function invokeMap(iterable $collection, $path, array $args = []): array
40+
{
41+
return baseRest(function ($collection, $path, $args) {
42+
$isFunc = \is_callable($path);
43+
$result = [];
44+
45+
each($collection, function ($value) use (&$result, $isFunc, $path, $args) {
46+
$result[] = $isFunc ? $path($value, ...$args) : baseInvoke($value, $path, $args);
47+
});
48+
49+
return $result;
50+
})($collection, $path, $args);
51+
}

src/internal/baseInvoke.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
// No 'declare(strict_types=1)' here as the invoked method needs to use weak-type{
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2018
10+
*/
11+
12+
namespace _\internal;
13+
14+
use function _\last;
15+
16+
function baseInvoke($object, $path, $args)
17+
{
18+
$path = castPath($path, $object);
19+
$object = parent($object, $path);
20+
$func = null === $object ? $object : [$object, toKey(last($path))];
21+
22+
return null === $func ? null : $func($object, ...$args);
23+
}

src/internal/parent.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2018
10+
*/
11+
12+
namespace _\internal;
13+
14+
use function _\{slice, get};
15+
16+
function parent($object, $path)
17+
{
18+
return count($path) < 2 ? $object : get($object, slice($path, 0, -1));
19+
}

tests/Collection/InvokeMapTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2017
10+
*/
11+
12+
use function _\invokeMap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class InvokeMapTest extends TestCase
16+
{
17+
public function testInvokeMap()
18+
{
19+
$this->assertSame([[1, 5, 7], [1, 2, 3]], invokeMap([[5, 1, 7], [3, 2, 1]], function($result) { sort($result); return $result;}));
20+
$this->assertSame([['1', '2', '3'], ['4', '5', '6']], invokeMap([123, 456], 'str_split'));
21+
22+
$users = [
23+
new class () {
24+
public function getCount() { return 12; }
25+
},
26+
new class () {
27+
public function getCount() { return 24; }
28+
}
29+
];
30+
31+
$this->assertEquals([12, 24], invokeMap($users, 'getCount'));
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy