forked from lstrojny/functional-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFromEntries.php
More file actions
38 lines (32 loc) · 970 Bytes
/
FromEntries.php
File metadata and controls
38 lines (32 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
/**
* @package Functional-php
* @author Hugo Sales <hugo@hsal.es>
* @copyright 2021 Lars Strojny
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/lstrojny/functional-php
*/
namespace Functional;
use Functional\Exceptions\InvalidArgumentException;
use Traversable;
/**
* Inspired by JavaScript’s `Object.fromEntries`,
* convert an array of key-value pairs into a key-value map
*
* @see entries
* @param Traversable|array $collection
* @return array
* @no-named-arguments
*/
function from_entries($collection)
{
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
$aggregation = [];
foreach ($collection as $entry) {
InvalidArgumentException::assertPair($entry, __FUNCTION__, 1);
[$key, $value] = $entry;
InvalidArgumentException::assertValidArrayKey($key, __FUNCTION__, 1);
$aggregation[$key] = $value;
}
return $aggregation;
}