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
|
|
|
* Returns an instance of `MapInterface` containing no entries. |
61
|
|
|
* @return MapInterface |
62
|
|
|
*/ |
63
|
|
|
function emptyMap(): MapInterface |
64
|
|
|
{ |
65
|
59 |
|
return Functions::emptyMap(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns an implemenation of MapInterface from the passed pairs. |
70
|
|
|
* @param array<Pair> ...$pairs |
71
|
|
|
* @return MapInterface |
72
|
|
|
*/ |
73
|
|
|
function mapOf(...$pairs): MapInterface |
74
|
|
|
{ |
75
|
50 |
|
return Functions::mapOf(...$pairs); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns a Pair object from `$key` and `$value`. |
80
|
|
|
* @param scalar $key |
81
|
|
|
* @param mixed $value |
82
|
|
|
* @return Pair |
83
|
|
|
*/ |
84
|
|
|
function pair($key, $value): Pair |
85
|
|
|
{ |
86
|
55 |
|
return Functions::pair($key, $value); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns `true` when the passed `$value` is an instance of `MapInterface`, `false` otherwise. |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
function isMap($value): bool |
94
|
|
|
{ |
95
|
2 |
|
return Functions::isMap($value); |
96
|
|
|
} |
97
|
|
|
|