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
|
3 |
|
public function getIterator() |
35
|
|
|
{ |
36
|
3 |
|
return new HashMapIterator($this->data); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
31 |
|
public function containsKey($key) : bool |
43
|
|
|
{ |
44
|
31 |
|
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
|
26 |
|
public function insert(Pair $pair) |
59
|
|
|
{ |
60
|
26 |
|
$this->data[$this->getScalar($pair->key)] = $pair; |
|
|
|
|
61
|
26 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
1 |
|
public function remove($key) |
67
|
|
|
{ |
68
|
1 |
|
unset($this->data[$this->getScalar($key)]); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
7 |
|
public function size() : int |
75
|
|
|
{ |
76
|
7 |
|
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
|
39 |
|
private function getScalar($value) |
86
|
|
|
{ |
87
|
39 |
|
switch (gettype($value)) |
88
|
|
|
{ |
89
|
39 |
|
case 'object': |
90
|
|
|
return spl_object_hash($value); |
91
|
39 |
|
case 'array': |
92
|
|
|
return implode($value); |
93
|
|
|
default: |
94
|
39 |
|
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@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.