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 |
26
|
|
|
extends AbstractCollection |
|
|
|
|
27
|
|
|
implements MapInterface |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Adds an element to the Map |
31
|
|
|
* |
32
|
|
|
* @param KeyType $key The key to add |
33
|
|
|
* @param ValueType $value The value to add |
34
|
|
|
*/ |
35
|
32 |
|
public function offsetSet($key, $value) |
36
|
|
|
{ |
37
|
32 |
|
$this->add($key, $value); |
38
|
32 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adds an element to the Map |
42
|
|
|
* |
43
|
|
|
* @param KeyType $key The key to add |
44
|
|
|
* @param ValueType $value The value to add |
45
|
|
|
*/ |
46
|
35 |
|
public function add($key, $value) |
47
|
|
|
{ |
48
|
35 |
|
$this->insert(new Pair($key, $value)); |
49
|
35 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks if a key exists |
53
|
|
|
* |
54
|
|
|
* @param KeyType $key The key to search for |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
|
|
public function offsetExists($key) : bool |
58
|
|
|
{ |
59
|
|
|
return $this->contains($key); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Removes an item from the map |
64
|
|
|
* |
65
|
|
|
* @param KeyType $key The key of the keypair to remove |
66
|
|
|
*/ |
67
|
|
|
public function offsetUnset($key) |
68
|
|
|
{ |
69
|
|
|
$this->remove($key); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets an item from the map, looking it up by the specified key |
74
|
|
|
* |
75
|
|
|
* @param KeyType $key |
76
|
|
|
* @return ValueType |
77
|
|
|
*/ |
78
|
41 |
|
public function offsetGet($key) |
79
|
|
|
{ |
80
|
41 |
|
return $this->get($key); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
41 |
|
public function get($key) |
87
|
|
|
{ |
88
|
41 |
|
return $this->getPair($key)->value; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|