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
|
50 |
|
public final static function mapOf(...$pairs): MapInterface |
|
|
|
|
34
|
|
|
{ |
35
|
50 |
|
$map = emptyMap(); |
36
|
50 |
|
$map->setArray(array_combine( |
37
|
50 |
|
array_map(fn (Pair $pair) => $pair->getKey(), $pairs), //keys |
38
|
50 |
|
array_map(fn (Pair $pair) => $pair->getValue(), $pairs) //values |
39
|
|
|
)); |
40
|
50 |
|
return $map; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param scalar $key |
45
|
|
|
* @param mixed $value |
46
|
|
|
* @return Pair |
47
|
|
|
*/ |
48
|
55 |
|
public final static function pair($key, $value): Pair |
|
|
|
|
49
|
|
|
{ |
50
|
55 |
|
return Pair::factory($key, $value); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public static function isMap($value): bool |
54
|
|
|
{ |
55
|
2 |
|
return $value instanceof MapInterface; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return MapInterface |
61
|
|
|
*/ |
62
|
|
|
function emptyMap(): MapInterface |
63
|
|
|
{ |
64
|
59 |
|
return Functions::emptyMap(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array<Pair> ...$pairs |
69
|
|
|
* @return MapInterface |
70
|
|
|
*/ |
71
|
|
|
function mapOf(...$pairs): MapInterface |
72
|
|
|
{ |
73
|
50 |
|
return Functions::mapOf(...$pairs); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param scalar $key |
78
|
|
|
* @param mixed $value |
79
|
|
|
* @return Pair |
80
|
|
|
*/ |
81
|
|
|
function pair($key, $value): Pair |
82
|
|
|
{ |
83
|
55 |
|
return Functions::pair($key, $value); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
function isMap($value): bool |
87
|
|
|
{ |
88
|
2 |
|
return Functions::isMap($value); |
89
|
|
|
} |
90
|
|
|
|