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\ListCollection\ArrayList; |
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
|
|
|
class OrderedMap extends AbstractMap |
28
|
|
|
{ |
29
|
|
|
private $map; |
30
|
|
|
|
31
|
|
|
private $list; |
32
|
|
|
|
33
|
29 |
|
public function __construct(Map $map, ArrayList $list) |
34
|
|
|
{ |
35
|
29 |
|
$this->map = $map; |
36
|
29 |
|
$this->list = $list; |
37
|
29 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Adds an element to the Map |
41
|
|
|
* |
42
|
|
|
* @param KeyType $key The key to add |
|
|
|
|
43
|
|
|
* @param ValueType $value The value to add |
|
|
|
|
44
|
|
|
*/ |
45
|
6 |
|
public function insert(Pair $pair) |
46
|
|
|
{ |
47
|
6 |
|
if (!$pair instanceof OrderedPair) |
48
|
|
|
{ |
49
|
6 |
|
$pair = new OrderedPair |
50
|
|
|
( |
51
|
6 |
|
$this->size(), |
52
|
6 |
|
$pair->key, |
|
|
|
|
53
|
6 |
|
$pair->value |
|
|
|
|
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
$this->map->insert($pair); |
58
|
6 |
|
$this->list->add($pair); |
|
|
|
|
59
|
6 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Checks if a key exists |
63
|
|
|
* |
64
|
|
|
* @param KeyType $key The key to search for |
65
|
|
|
* @return boolean |
66
|
|
|
*/ |
67
|
1 |
|
public function containsKey($key) : bool |
68
|
|
|
{ |
69
|
1 |
|
return $this->map->containsKey($key); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Removes an item from the map |
74
|
|
|
* |
75
|
|
|
* @param KeyType $key The key of the keypair to remove |
76
|
|
|
*/ |
77
|
1 |
|
public function remove($key) |
78
|
|
|
{ |
79
|
1 |
|
$pair = $this->getPair($key); |
80
|
|
|
|
81
|
1 |
|
$this->map->remove($pair->key); |
|
|
|
|
82
|
1 |
|
$this->list->remove($pair->index); |
|
|
|
|
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets an item from the map, looking it up by the specified key |
87
|
|
|
* |
88
|
|
|
* @param KeyType $key |
89
|
|
|
* @return OrderedPair |
90
|
|
|
*/ |
91
|
2 |
|
public function getPair($key) : Pair |
92
|
|
|
{ |
93
|
2 |
|
return is_int($key) |
94
|
|
|
? $this->list->get($key) |
95
|
2 |
|
: $this->map->getPair($key); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
public function getIterator() |
99
|
|
|
{ |
100
|
3 |
|
return $this->map->getIterator(); |
101
|
|
|
} |
102
|
|
|
|
103
|
7 |
|
public function size() : int |
104
|
|
|
{ |
105
|
7 |
|
return $this->list->size(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function indexOfKey($key) |
109
|
|
|
{ |
110
|
|
|
return $this->getPair($key)->index; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.