1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2022 Sebastian Böttger <[email protected]> |
5
|
|
|
* You may use, distribute and modify this code under the |
6
|
|
|
* terms of the MIT license. |
7
|
|
|
* |
8
|
|
|
* You should have received a copy of the MIT license with |
9
|
|
|
* this file. If not, please visit: https://opensource.org/licenses/mit-license.php |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Seboettg\Collection\Map; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
final class Functions |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return MapInterface |
20
|
|
|
*/ |
21
|
|
|
public static final function emptyMap(): MapInterface |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
return new class() implements MapInterface { |
24
|
|
|
private $array = []; |
|
|
|
|
25
|
|
|
use MapTrait; |
26
|
|
|
}; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array<Pair> ...$pairs |
31
|
|
|
* @return MapInterface |
32
|
|
|
*/ |
33
|
22 |
|
public final static function mapOf(...$pairs): MapInterface |
|
|
|
|
34
|
|
|
{ |
35
|
22 |
|
$map = emptyMap(); |
36
|
22 |
|
foreach ($pairs as $pair) { |
37
|
22 |
|
if (!$pair instanceof Pair) { |
38
|
|
|
throw new InvalidArgumentException(sprintf( |
39
|
|
|
"At least one pair is not of type %s", Pair::class)); |
40
|
|
|
} |
41
|
22 |
|
$map->put($pair->getKey(), $pair->getValue()); |
42
|
|
|
} |
43
|
22 |
|
return $map; |
44
|
|
|
} |
45
|
|
|
|
46
|
22 |
|
public final static function pair($key, $value): Pair |
|
|
|
|
47
|
|
|
{ |
48
|
22 |
|
return new Pair($key, $value); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return MapInterface |
54
|
|
|
*/ |
55
|
|
|
function emptyMap(): MapInterface |
56
|
|
|
{ |
57
|
23 |
|
return Functions::emptyMap(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array<Pair> ...$pairs |
62
|
|
|
* @return MapInterface |
63
|
|
|
*/ |
64
|
|
|
function mapOf(...$pairs): MapInterface |
65
|
|
|
{ |
66
|
22 |
|
return Functions::mapOf(...$pairs); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
function pair($key, $value): Pair |
71
|
|
|
{ |
72
|
22 |
|
return Functions::pair($key, $value); |
73
|
|
|
} |
74
|
|
|
|