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
|
|
|
|
21
|
|
|
|
22
|
|
|
class ObjectBiMap implements ObjectBiMapInterface |
23
|
|
|
{ |
24
|
|
|
use ObjectTypeHintTrait; |
25
|
|
|
|
26
|
|
|
protected $behavior = self::DEFAULT; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ObjectMap |
30
|
|
|
*/ |
31
|
|
|
protected $keys; |
32
|
|
|
/** |
33
|
|
|
* @var ObjectMap |
34
|
|
|
*/ |
35
|
|
|
protected $values; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $behavior |
39
|
|
|
*/ |
40
|
22 |
|
public function __construct(int $behavior = self::DEFAULT) |
|
|
|
|
41
|
|
|
{ |
42
|
22 |
|
$key_behavior = 0; |
43
|
22 |
|
$value_behavior = 0; |
|
|
|
|
44
|
|
|
|
45
|
22 |
|
if ($behavior & self::WEAK_KEY) { |
|
|
|
|
46
|
5 |
|
$key_behavior |= self::WEAK_KEY; |
47
|
5 |
|
$value_behavior |= self::WEAK_VALUE; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
22 |
|
if ($behavior & self::WEAK_VALUE) { |
|
|
|
|
51
|
5 |
|
$key_behavior |= self::WEAK_VALUE; |
52
|
5 |
|
$value_behavior |= self::WEAK_KEY; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
22 |
|
$this->keys = new ObjectMap($key_behavior); |
|
|
|
|
56
|
22 |
|
$this->values = new ObjectMap($value_behavior); |
|
|
|
|
57
|
|
|
|
58
|
22 |
|
$this->behavior = $behavior; |
|
|
|
|
59
|
22 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
4 |
|
public function values(): ObjectBiMapInterface |
65
|
|
|
{ |
66
|
4 |
|
$new_behavior = 0; |
67
|
|
|
|
68
|
4 |
|
if ($this->behavior & self::WEAK_KEY) { |
69
|
2 |
|
$new_behavior |= self::WEAK_VALUE; |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
if ($this->behavior & self::WEAK_VALUE) { |
73
|
2 |
|
$new_behavior |= self::WEAK_KEY; |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
$new = new static($new_behavior); |
77
|
|
|
|
78
|
4 |
|
$new->keys = $this->values; |
79
|
4 |
|
$new->values = $this->keys; |
80
|
|
|
|
81
|
4 |
|
return $new; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
16 |
|
public function put($key, $value) |
88
|
|
|
{ |
89
|
16 |
|
$this->assertObject($key, 'Key'); |
90
|
|
|
|
91
|
15 |
|
if ($this->keys->has($key)) { |
92
|
1 |
|
throw new OverflowException('Value with such key already exists'); |
93
|
|
|
} |
94
|
|
|
|
95
|
15 |
|
$this->assertObject($value, 'Value'); |
96
|
|
|
|
97
|
14 |
|
if ($this->values->has($value)) { |
98
|
|
|
throw new OverflowException('Key with such value already exists'); |
99
|
|
|
} |
100
|
|
|
|
101
|
14 |
|
$this->keys->put($key, $value); |
102
|
14 |
|
$this->values->put($value, $key); |
103
|
14 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
3 |
|
public function get($key) |
109
|
|
|
{ |
110
|
3 |
|
$this->assertObject($key, 'Key'); |
111
|
|
|
|
112
|
2 |
|
return $this->keys->get($key); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
|
119
|
6 |
|
public function has($key): bool |
120
|
|
|
{ |
121
|
6 |
|
$this->assertObject($key, 'Key'); |
122
|
|
|
|
123
|
5 |
|
return $this->keys->has($key); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
4 |
|
public function remove($key) |
130
|
|
|
{ |
131
|
4 |
|
$this->assertObject($key, 'Key'); |
132
|
|
|
|
133
|
3 |
|
if (!$this->keys->has($key)) { |
134
|
1 |
|
throw new OutOfBoundsException('Value with such key not found'); |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
$value = $this->keys->remove($key); |
138
|
|
|
|
139
|
2 |
|
if (!$this->values->has($value)) { |
140
|
|
|
// UNEXPECTED |
141
|
|
|
throw new OutOfBoundsException('Key with such value not found'); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
$this->values->remove($value); |
145
|
|
|
|
146
|
2 |
|
return $value; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
10 |
|
public function count() |
153
|
|
|
{ |
154
|
10 |
|
return count($this->keys); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
2 |
|
public function clear() |
161
|
|
|
{ |
162
|
2 |
|
$this->keys->clear(); |
163
|
2 |
|
$this->values->clear(); |
164
|
2 |
|
} |
165
|
|
|
} |
166
|
|
|
|
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.