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
|
|
|
use Spaark\CompositeUtils\Service\HashProducer; |
18
|
|
|
use Spaark\CompositeUtils\Service\HashProducerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Represents a HashMap which contains mapings from one element to |
22
|
|
|
* another |
23
|
|
|
* |
24
|
|
|
* @generic KeyType |
25
|
|
|
* @generic ValueType |
26
|
|
|
*/ |
27
|
|
|
class HashMap extends AbstractMap |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var HashProducerInterface |
31
|
|
|
*/ |
32
|
|
|
protected static $defaultHashProducer; |
33
|
|
|
|
34
|
46 |
|
protected static function getHashProducer() : HashProducerInterface |
35
|
|
|
{ |
36
|
46 |
|
if (!static::$defaultHashProducer) |
37
|
|
|
{ |
38
|
|
|
static::$defaultHashProducer = new HashProducer(); |
39
|
|
|
} |
40
|
|
|
|
41
|
46 |
|
return static::$defaultHashProducer; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function setDefaultHashProducer |
45
|
|
|
( |
46
|
|
|
HashProducerInterface $hashProducer |
47
|
|
|
) |
48
|
|
|
: HashProducerInterface |
49
|
|
|
{ |
50
|
|
|
static::$defaultHashProducer = $hashProducer; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Pair<KeyType, ValueType>[] |
55
|
|
|
*/ |
56
|
|
|
protected $data = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var HashProducerInterface |
60
|
|
|
*/ |
61
|
|
|
protected $hashProducer; |
62
|
|
|
|
63
|
46 |
|
public function __construct |
64
|
|
|
( |
65
|
|
|
?HashProducerInterface $hashProducer = null |
66
|
|
|
) |
67
|
|
|
{ |
68
|
46 |
|
$this->hashProducer = |
69
|
46 |
|
$hashProducer ?: static::getHashProducer(); |
70
|
46 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
15 |
|
public function getIterator() |
76
|
|
|
{ |
77
|
15 |
|
return new HashMapIterator($this->data); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
46 |
|
public function containsKey($key) : bool |
84
|
|
|
{ |
85
|
46 |
|
return isset($this->data[$this->hashProducer->getHash($key)]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
50 |
|
public function getPair($key) : Pair |
92
|
|
|
{ |
93
|
50 |
|
return $this->data[$this->hashProducer->getHash($key)]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
43 |
|
public function insert(Pair $pair) |
100
|
|
|
{ |
101
|
43 |
|
$this->data[$this->hashProducer->getHash($pair->key)] = $pair; |
|
|
|
|
102
|
43 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritDoc} |
106
|
|
|
*/ |
107
|
2 |
|
public function remove($key) |
108
|
|
|
{ |
109
|
2 |
|
unset($this->data[$this->hashProducer->getHash($key)]); |
110
|
2 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
8 |
|
public function size() : int |
116
|
|
|
{ |
117
|
8 |
|
return count($this->data); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.