|
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 a HashMap which contains mapings from one element to |
|
19
|
|
|
* another |
|
20
|
|
|
* |
|
21
|
|
|
* @generic KeyType |
|
22
|
|
|
* @generic ValueType |
|
23
|
|
|
*/ |
|
24
|
|
|
class HashMap extends AbstractMap |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Pair<KeyType, ValueType>[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $data; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritDoc} |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function getIterator() |
|
35
|
|
|
{ |
|
36
|
1 |
|
return new HashMapIterator($this->data); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritDoc} |
|
41
|
|
|
*/ |
|
42
|
30 |
|
public function containsKey($key) : bool |
|
43
|
|
|
{ |
|
44
|
30 |
|
return isset($this->data[$this->getScalar($key)]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
*/ |
|
50
|
33 |
|
public function get($key) |
|
51
|
|
|
{ |
|
52
|
33 |
|
return $this->data[$this->getScalar($key)]->value; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritDoc} |
|
57
|
|
|
*/ |
|
58
|
22 |
|
public function insert(Pair $pair) |
|
59
|
|
|
{ |
|
60
|
22 |
|
$this->data[$this->getScalar($pair->key)] = $pair; |
|
|
|
|
|
|
61
|
22 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritDoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function remove($key) |
|
67
|
|
|
{ |
|
68
|
|
|
unset($this->data[$this->getScalar($key)]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritDoc} |
|
73
|
|
|
*/ |
|
74
|
6 |
|
public function size() : int |
|
75
|
|
|
{ |
|
76
|
6 |
|
return count($this->data); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns a good scalar value to use for a native PHP array |
|
81
|
|
|
* |
|
82
|
|
|
* @param KeyType $value The key to convert to a scalar |
|
83
|
|
|
* @return string Scalar value |
|
84
|
|
|
*/ |
|
85
|
35 |
|
private function getScalar($value) |
|
86
|
|
|
{ |
|
87
|
35 |
|
switch (gettype($value)) |
|
88
|
|
|
{ |
|
89
|
35 |
|
case 'object': |
|
90
|
|
|
return spl_object_hash($value); |
|
91
|
35 |
|
case 'array': |
|
92
|
|
|
return implode($value); |
|
93
|
|
|
default: |
|
94
|
35 |
|
return (string)$value; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
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@propertyannotation 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.