1
|
|
|
<?php declare(strict_types=1); |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pinepain/php-object-maps PHP library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code or visit http://opensource.org/licenses/MIT |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace Pinepain\ObjectMaps; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
use Pinepain\ObjectMaps\Exceptions\OutOfBoundsException; |
19
|
|
|
use Pinepain\ObjectMaps\Exceptions\OverflowException; |
20
|
|
|
use Ref\WeakReference; |
21
|
|
|
use function spl_object_hash; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class ObjectMap implements ObjectMapInterface |
25
|
|
|
{ |
26
|
|
|
use ObjectTypeHintTrait; |
27
|
|
|
|
28
|
|
|
protected $behavior = self::DEFAULT; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Bucket[] |
32
|
|
|
*/ |
33
|
|
|
protected $keys = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int $behavior |
37
|
|
|
*/ |
38
|
42 |
|
public function __construct(int $behavior = self::DEFAULT) |
|
|
|
|
39
|
|
|
{ |
40
|
42 |
|
$this->behavior = $behavior; |
41
|
42 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
28 |
|
public function put($key, $value) |
47
|
|
|
{ |
48
|
28 |
|
$this->assertObject($key, 'Key'); |
49
|
27 |
|
$this->assertObject($value, 'Value'); // while we may associate non-object value, for interface compatibility we don't do that |
50
|
|
|
|
51
|
26 |
|
$hash = $this->getHash($key); |
52
|
|
|
|
53
|
26 |
|
if (isset($this->keys[$hash])) { |
54
|
1 |
|
throw new OverflowException('Value with such key already exists'); |
55
|
|
|
} |
56
|
|
|
|
57
|
26 |
|
$bucket = $this->createBucket($key, $value, $hash); |
58
|
|
|
|
59
|
26 |
|
$this->keys[$hash] = $bucket; |
60
|
26 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
9 |
View Code Duplication |
public function get($key) |
|
|
|
|
66
|
|
|
{ |
67
|
9 |
|
$this->assertObject($key, 'Key'); |
68
|
|
|
|
69
|
8 |
|
$hash = $this->getHash($key); |
70
|
|
|
|
71
|
8 |
|
if (!isset($this->keys[$hash])) { |
72
|
2 |
|
throw new OutOfBoundsException('Value with such key not found'); |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
$bucket = $this->keys[$hash]; |
76
|
|
|
|
77
|
6 |
|
return $this->fetchBucketValue($bucket); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
23 |
|
public function has($key): bool |
84
|
|
|
{ |
85
|
23 |
|
$this->assertObject($key, 'Key'); |
86
|
|
|
|
87
|
22 |
|
$hash = $this->getHash($key); |
88
|
|
|
|
89
|
22 |
|
return isset($this->keys[$hash]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
7 |
View Code Duplication |
public function remove($key) |
|
|
|
|
96
|
|
|
{ |
97
|
7 |
|
$this->assertObject($key, 'Key'); |
98
|
|
|
|
99
|
6 |
|
$hash = $this->getHash($key); |
100
|
|
|
|
101
|
6 |
|
if (!isset($this->keys[$hash])) { |
102
|
1 |
|
throw new OutOfBoundsException('Value with such key not found'); |
103
|
|
|
} |
104
|
|
|
|
105
|
5 |
|
$bucket = $this->keys[$hash]; |
106
|
|
|
|
107
|
5 |
|
$this->doRemove($hash); |
108
|
|
|
|
109
|
5 |
|
return $this->fetchBucketValue($bucket); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
16 |
|
public function count() |
116
|
|
|
{ |
117
|
16 |
|
return count($this->keys); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
3 |
|
public function clear() |
124
|
|
|
{ |
125
|
3 |
|
$this->keys = []; |
126
|
3 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param object $value |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
33 |
|
protected function getHash($value) |
134
|
|
|
{ |
135
|
33 |
|
return spl_object_hash($value); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $hash |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
14 |
|
protected function doRemove(string $hash) |
144
|
|
|
{ |
145
|
14 |
|
unset($this->keys[$hash]); |
146
|
14 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param object $key |
150
|
|
|
* @param object $value |
151
|
|
|
* @param string $hash |
152
|
|
|
* |
153
|
|
|
* @return Bucket |
154
|
|
|
*/ |
155
|
26 |
|
protected function createBucket($key, $value, string $hash): Bucket |
156
|
|
|
{ |
157
|
26 |
|
if ($this->behavior & self::WEAK_KEY) { |
158
|
8 |
|
$key = $this->createReference($key, $hash); |
159
|
|
|
} |
160
|
|
|
|
161
|
26 |
|
if ($this->behavior & self::WEAK_VALUE) { |
162
|
8 |
|
$value = $this->createReference($value, $hash); |
163
|
|
|
} |
164
|
|
|
|
165
|
26 |
|
return new Bucket($key, $value); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param Bucket $bucket |
170
|
|
|
* |
171
|
|
|
* @return null|object |
172
|
|
|
*/ |
173
|
11 |
|
protected function fetchBucketValue(Bucket $bucket) |
174
|
|
|
{ |
175
|
11 |
|
if ($this->behavior & self::WEAK_VALUE) { |
176
|
2 |
|
assert($bucket->value instanceof WeakReference); |
177
|
|
|
|
178
|
2 |
|
return $bucket->value->get(); |
179
|
|
|
} |
180
|
|
|
|
181
|
9 |
|
return $bucket->value; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param $obj |
186
|
|
|
* @param string $hash |
187
|
|
|
* |
188
|
|
|
* @return WeakReference |
189
|
|
|
*/ |
190
|
|
|
protected function createReference($obj, string $hash): WeakReference |
191
|
|
|
{ |
192
|
9 |
|
return new WeakReference($obj, function () use ($hash) { |
193
|
9 |
|
$this->doRemove($hash); |
194
|
9 |
|
}); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.