1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Phiremock. |
4
|
|
|
* |
5
|
|
|
* Phiremock is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* Phiremock is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with Phiremock. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Server\Utils\DataStructures; |
20
|
|
|
|
21
|
|
|
use ArrayIterator; |
22
|
|
|
use BadMethodCallException; |
23
|
|
|
use InvalidArgumentException; |
24
|
|
|
|
25
|
|
|
class StringObjectArrayMap implements Map |
26
|
|
|
{ |
27
|
|
|
private $mapData; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->clean(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getIterator() |
35
|
|
|
{ |
36
|
|
|
return new ArrayIterator($this->mapData); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function set($key, $value) |
40
|
|
|
{ |
41
|
|
|
if (!\is_string($key)) { |
42
|
|
|
throw new InvalidArgumentException('Expected key to be string. Got: ' . \gettype($key)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!\is_object($value)) { |
46
|
|
|
throw new InvalidArgumentException('Expected value to be object. Got: ' . \gettype($key)); |
47
|
|
|
} |
48
|
|
|
$this->mapData[$key] = $value; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function get($key) |
52
|
|
|
{ |
53
|
|
|
if (!$this->has($key)) { |
54
|
|
|
throw new BadMethodCallException('Calling get for an absent key: ' . $key); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->mapData[$key]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function has($key) |
61
|
|
|
{ |
62
|
|
|
if (!\is_string($key)) { |
63
|
|
|
throw new InvalidArgumentException('Expected key to be string. Got: ' . \gettype($key)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return isset($this->mapData[$key]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function clean() |
70
|
|
|
{ |
71
|
|
|
$this->mapData = []; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function delete($key) |
75
|
|
|
{ |
76
|
|
|
if (!$this->has($key)) { |
77
|
|
|
throw new BadMethodCallException('Calling delete for an absent key: ' . $key); |
78
|
|
|
} |
79
|
|
|
unset($this->mapData[$key]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|