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