Skip to content

Commit 0dd47fe

Browse files
author
Fureev Eugene
committed
feat: add mapValue
1 parent ef15ff1 commit 0dd47fe

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## v4.15.0
8+
9+
### Added
10+
11+
- Add global method `mapValue` Returns an array containing the results of applying func to the items of the $collection
12+
713
## v4.14.0
814

915
### Added

src/Global/base.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ function value(mixed $value, mixed ...$args): mixed
1717
}
1818
}
1919

20+
if (!function_exists('mapValue')) {
21+
function mapValue(callable $fn, iterable $collection, mixed ...$args): array
22+
{
23+
$result = [];
24+
25+
foreach ($collection as $key => $value) {
26+
$result[$key] = $fn($value, $key, ...$args);
27+
}
28+
29+
return $result;
30+
}
31+
}
32+
2033
if (!function_exists('when')) {
2134
/**
2235
* Returns a value when a condition is truthy.
@@ -222,7 +235,8 @@ function remoteStaticCallOrTrow(object|string|null $class, string $method, mixed
222235
return $class::$method(...$params);
223236
}
224237

225-
throw new \Php\Support\Exceptions\MissingMethodException("$class::$method");
238+
$strClass = is_object($class) ? $class::class : $class;
239+
throw new \Php\Support\Exceptions\MissingMethodException("$strClass::$method");
226240
}
227241
}
228242

tests/Global/BaseTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Php\Support\Tests;
5+
namespace Php\Support\Tests\Global;
66

77
use Php\Support\Types\Point;
88
use PHPUnit\Framework\TestCase;

tests/Global/MapValueTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Support\Tests\Global;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class MapValueTest extends TestCase
10+
{
11+
/**
12+
* @test
13+
*/
14+
public function mapValue(): void
15+
{
16+
$fnColl = static fn(string $value) => mb_strtoupper($value);
17+
$result = mapValue($fnColl, ['test', 'app']);
18+
$expect = [
19+
'TEST',
20+
'APP',
21+
];
22+
self::assertEquals($expect, $result);
23+
}
24+
25+
/**
26+
* @test
27+
*/
28+
public function mapValueWithParams(): void
29+
{
30+
$fnColl = static fn(string $value, $key, string $prefix, string $suffix) =>
31+
$prefix . mb_strtoupper($value) . $suffix;
32+
$result = mapValue($fnColl, ['test', 'app'], '- ', '.');
33+
$expect = [
34+
'- TEST.',
35+
'- APP.',
36+
];
37+
self::assertEquals($expect, $result);
38+
}
39+
}

0 commit comments

Comments
 (0)