|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) Nate Brunette. |
|
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Tebru\Collection; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractMap |
|
11
|
|
|
* |
|
12
|
|
|
* A skeletal implementation of [@see MapInterface] |
|
13
|
|
|
* |
|
14
|
|
|
* @author Nate Brunette <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractMap implements MapInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Adds all the mappings from specified map to this map |
|
20
|
|
|
* |
|
21
|
|
|
* @param MapInterface $map |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
public function putAll(MapInterface $map): void |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var MapEntry $entrySet */ |
|
27
|
|
|
foreach ($map->entrySet() as $entrySet) { |
|
28
|
|
|
$this->put($entrySet->key, $entrySet->value); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Adds all the mappings from specified array to this map |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $map |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function putAllArray(array $map): void |
|
39
|
|
|
{ |
|
40
|
|
|
/** @var MapEntry $entrySet */ |
|
41
|
|
|
foreach ($map as $key => $value) { |
|
42
|
|
|
$this->put($key, $value); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Find the first [@see MapEntry] in map |
|
48
|
|
|
* |
|
49
|
|
|
* The closure will get passed a MapEntry. Returning true will end the |
|
50
|
|
|
* loop and return that MapEntry |
|
51
|
|
|
* |
|
52
|
|
|
* @param callable $find |
|
53
|
|
|
* @return MapEntry|null |
|
54
|
|
|
*/ |
|
55
|
|
|
public function find(callable $find): ?MapEntry |
|
56
|
|
|
{ |
|
57
|
|
|
/** @var MapEntry $mapEntry */ |
|
58
|
|
|
foreach ($this->entrySet() as $mapEntry) { |
|
59
|
|
|
if (true === $find($mapEntry)) { |
|
60
|
|
|
return $mapEntry; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Use a closure to determine existence in the map |
|
69
|
|
|
* |
|
70
|
|
|
* The closure will get passed a [@see MapEntry]. Returning true from the |
|
71
|
|
|
* closure will return true from this method. |
|
72
|
|
|
* |
|
73
|
|
|
* @param callable $exists |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function exists(callable $exists): bool |
|
77
|
|
|
{ |
|
78
|
|
|
/** @var MapEntry $mapEntry */ |
|
79
|
|
|
foreach ($this->entrySet() as $mapEntry) { |
|
80
|
|
|
if (true === $exists($mapEntry)) { |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|