1
|
|
|
<?php namespace Spaark\CompositeUtils\Model\Collection; |
2
|
|
|
/** |
3
|
|
|
* Spaark Framework |
4
|
|
|
* |
5
|
|
|
* @author Emily Shepherd <[email protected]> |
6
|
|
|
* @copyright 2012-2015 Emily Shepherd |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Represents a HashMap which contains mapings from one element to |
12
|
|
|
* another |
13
|
|
|
* |
14
|
|
|
* This is similar to PHP's existing array system, which supports |
15
|
|
|
* key-value pairs (<code>array('key' => 'value'));</code>) with the |
16
|
|
|
* added benefit that key values can be objects. |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
class HashMap extends Collection |
20
|
|
|
{ |
21
|
|
|
protected $keys = array( ); |
22
|
|
|
|
23
|
9 |
View Code Duplication |
private function getScalar($value) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
return |
26
|
9 |
|
(is_object($value) ? spl_object_hash($value) |
27
|
9 |
|
: (is_array($value) ? implode($value) |
28
|
9 |
|
: ( (string)$value))); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function key() |
32
|
|
|
{ |
33
|
|
|
return $this->keys[parent::key()]; |
34
|
|
|
} |
35
|
|
|
|
36
|
7 |
|
public function offsetExists($key) |
37
|
|
|
{ |
38
|
7 |
|
return parent::offsetExists($this->getScalar($key)); |
39
|
|
|
} |
40
|
|
|
|
41
|
7 |
|
public function offsetGet($offset) |
42
|
|
|
{ |
43
|
7 |
|
return parent::offsetGet($this->getScalar($offset)); |
44
|
|
|
} |
45
|
|
|
|
46
|
9 |
|
public function offsetSet($offset, $value) |
47
|
|
|
{ |
48
|
9 |
|
$hash = $this->getScalar($offset); |
49
|
|
|
|
50
|
9 |
|
$this->keys[$hash] = $offset; |
51
|
9 |
|
$this->data[$hash] = $value; |
52
|
9 |
|
} |
53
|
|
|
|
54
|
|
|
public function offsetUnset($offset) |
55
|
|
|
{ |
56
|
|
|
$hash = $this->getScalar($offset); |
|
|
|
|
57
|
|
|
|
58
|
|
|
unset($this->data[$offset]); |
59
|
|
|
unset($this->keys[$offset]); |
60
|
|
|
} |
61
|
|
|
|
62
|
7 |
|
public function contains($offset) |
63
|
|
|
{ |
64
|
7 |
|
return $this->offsetExists($offset); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.