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
|
|
|
class OrderedMap extends AbstractMap |
26
|
|
|
{ |
27
|
|
|
private $map; |
28
|
|
|
|
29
|
|
|
private $list; |
30
|
|
|
|
31
|
29 |
|
public function __construct(AbstractMap $map, AbstractList $list) |
32
|
|
|
{ |
33
|
29 |
|
$this->map = $map; |
34
|
29 |
|
$this->list = $list; |
35
|
29 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Adds an element to the Map |
39
|
|
|
* |
40
|
|
|
* @param KeyType $key The key to add |
|
|
|
|
41
|
|
|
* @param ValueType $value The value to add |
|
|
|
|
42
|
|
|
*/ |
43
|
25 |
|
public function insert(Pair $pair) |
44
|
|
|
{ |
45
|
25 |
|
if (!$pair instanceof OrderedPair) |
46
|
|
|
{ |
47
|
25 |
|
$pair = new OrderedPair |
48
|
|
|
( |
49
|
25 |
|
$this->size(), |
50
|
25 |
|
$pair->key, |
|
|
|
|
51
|
25 |
|
$pair->value |
|
|
|
|
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
25 |
|
$this->map->insert($pair); |
56
|
25 |
|
$this->list->add($pair); |
|
|
|
|
57
|
25 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Checks if a key exists |
61
|
|
|
* |
62
|
|
|
* @param KeyType $key The key to search for |
63
|
|
|
* @return boolean |
64
|
|
|
*/ |
65
|
24 |
|
public function containsKey($key) : bool |
66
|
|
|
{ |
67
|
24 |
|
return $this->map->containsKey($key); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Removes an item from the map |
72
|
|
|
* |
73
|
|
|
* @param KeyType $key The key of the keypair to remove |
74
|
|
|
*/ |
75
|
1 |
|
public function remove($key) |
76
|
|
|
{ |
77
|
1 |
|
$pair = $this->getPair($key); |
78
|
|
|
|
79
|
1 |
|
$this->map->remove($pair->key); |
|
|
|
|
80
|
1 |
|
$this->list->remove($pair->index); |
|
|
|
|
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets an item from the map, looking it up by the specified key |
85
|
|
|
* |
86
|
|
|
* @param KeyType $key |
87
|
|
|
* @return OrderedPair |
88
|
|
|
*/ |
89
|
5 |
|
public function getPair($key) : Pair |
90
|
|
|
{ |
91
|
5 |
|
return is_int($key) |
92
|
|
|
? $this->list->get($key) |
93
|
5 |
|
: $this->map->getPair($key); |
94
|
|
|
} |
95
|
|
|
|
96
|
7 |
|
public function getIterator() |
97
|
|
|
{ |
98
|
7 |
|
return $this->map->getIterator(); |
99
|
|
|
} |
100
|
|
|
|
101
|
27 |
|
public function size() : int |
102
|
|
|
{ |
103
|
27 |
|
return $this->list->size(); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
public function indexOfKey($key) |
107
|
|
|
{ |
108
|
1 |
|
return $this->getPair($key)->index; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.