1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Composite Utils package. |
4
|
|
|
* |
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the |
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @package spaark/composite-utils |
11
|
|
|
* @author Emily Shepherd <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Model\Collection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Represents an abstract collection which maps one value to another |
19
|
|
|
* |
20
|
|
|
* These are stored as pairs |
21
|
|
|
* |
22
|
|
|
* @generic KeyType |
23
|
|
|
* @generic ValueType |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractMap extends AbstractCollection |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Insert a new key value Pair into the Map |
29
|
|
|
* |
30
|
|
|
* @param Pair<KeyType, ValueType> $pair |
31
|
|
|
*/ |
32
|
|
|
abstract public function insert(Pair $pair); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Gets the item, looking it up with the specified key |
36
|
|
|
* |
37
|
|
|
* @param KeyType $item The key to search by |
38
|
|
|
* @return ValueType The item |
39
|
|
|
*/ |
40
|
|
|
abstract public function get($item); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Removes an item, looking it up with the specified key |
44
|
|
|
* |
45
|
|
|
* @param KeyType $item The key of the keypair to remove |
46
|
|
|
*/ |
47
|
|
|
abstract public function remove($item); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Checks if the specified key exists in this map |
51
|
|
|
* |
52
|
|
|
* @param KeyType $key The key to search for |
53
|
|
|
* @return boolean |
54
|
|
|
*/ |
55
|
|
|
abstract public function containsKey($key) : bool; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Adds an element to the Map |
59
|
|
|
* |
60
|
|
|
* @param KeyType $key The key to add |
61
|
|
|
* @param ValueType $value The value to add |
62
|
|
|
*/ |
63
|
19 |
|
public function offsetSet($key, $value) |
64
|
|
|
{ |
65
|
19 |
|
$this->add($key, $value); |
66
|
19 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Adds an element to the Map |
70
|
|
|
* |
71
|
|
|
* @param KeyType $key The key to add |
72
|
|
|
* @param ValueType $value The value to add |
73
|
|
|
*/ |
74
|
22 |
|
public function add($key, $value) |
75
|
|
|
{ |
76
|
22 |
|
$this->insert(new Pair($key, $value)); |
77
|
22 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Checks if a key exists |
81
|
|
|
* |
82
|
|
|
* @param KeyType $key The key to search for |
83
|
|
|
* @return boolean |
84
|
|
|
*/ |
85
|
|
|
public function offsetExists($key) : bool |
86
|
|
|
{ |
87
|
|
|
return $this->contains($key); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Removes an item from the map |
92
|
|
|
* |
93
|
|
|
* @param KeyType $key The key of the keypair to remove |
94
|
|
|
*/ |
95
|
|
|
public function offsetUnset($key) |
96
|
|
|
{ |
97
|
|
|
$this->remove($key); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets an item from the map, looking it up by the specified key |
102
|
|
|
* |
103
|
|
|
* @param KeyType $key |
104
|
|
|
* @return ValueType |
105
|
|
|
*/ |
106
|
33 |
|
public function offsetGet($key) |
107
|
|
|
{ |
108
|
33 |
|
return $this->get($key); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|