|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -class MabeEnum_EnumMap implements Iterator, Countable |
| 3 | +namespace MabeEnum; |
| 4 | + |
| 5 | +use SplObjectStorage; |
| 6 | + |
| 7 | +class EnumMap extends SplObjectStorage |
4 | 8 | {
|
5 |
| - private $enums = array(); |
6 |
| - private $values = array(); |
7 |
| - private $position = 0; |
8 |
| - private $enumClass; |
9 | 9 |
|
10 | 10 | public function __construct($enumClass)
|
11 | 11 | {
|
12 | 12 | $this->enumClass = $enumClass;
|
13 | 13 | }
|
14 | 14 |
|
15 |
| - public function attach($enum, $value) |
| 15 | + public function attach($enum, $data = null) |
16 | 16 | {
|
17 | 17 | if (!($enum instanceof $this->enumClass)) {
|
18 |
| - $enum = new $this->enumClass($enum); |
| 18 | + $enum = call_user_func(array($this->enumClass, 'get'), $enum); |
19 | 19 | }
|
20 | 20 |
|
21 |
| - if ($this->contains($enum)) { |
22 |
| - throw new RuntimeException("'{$enum}' already attached to map"); |
23 |
| - } |
24 |
| - |
25 |
| - $ordinal = $enum->getOrdinal(); |
26 |
| - $this->values[$ordinal] = $value; |
27 |
| - $this->enums[$ordinal] = $enum; |
| 21 | + parent::attach($enum, $data); |
28 | 22 | }
|
29 | 23 |
|
30 |
| - public function detach($enum) |
| 24 | + public function contains($enum) |
31 | 25 | {
|
32 | 26 | if (!($enum instanceof $this->enumClass)) {
|
33 |
| - $enum = new $this->enumClass($enum); |
34 |
| - } |
35 |
| - |
36 |
| - if (!$this->contains($enum)) { |
37 |
| - throw new RuntimeException("'{$enum}' not attached to map"); |
| 27 | + $enum = call_user_func(array($this->enumClass, 'get'), $enum); |
38 | 28 | }
|
39 | 29 |
|
40 |
| - $ordinal = $enum->getOrdinal(); |
41 |
| - unset($this->values[$ordinal], $this->enums[$ordinal]); |
| 30 | + return parent::contains($enum); |
42 | 31 | }
|
43 | 32 |
|
44 |
| - public function get($enum) |
| 33 | + public function detach($enum) |
45 | 34 | {
|
46 | 35 | if (!($enum instanceof $this->enumClass)) {
|
47 |
| - $enum = new $this->enumClass($enum); |
| 36 | + $enum = call_user_func(array($this->enumClass, 'get'), $enum); |
48 | 37 | }
|
49 | 38 |
|
50 |
| - if (!$this->contains($enum)) { |
51 |
| - throw new RuntimeException("'{$enum}' not attached to map"); |
52 |
| - } |
53 |
| - |
54 |
| - return $this->values[$enum->getOrdinal()]; |
| 39 | + parent::detach($enum); |
55 | 40 | }
|
56 | 41 |
|
57 |
| - public function contains($enum) |
| 42 | + public function getHash($enum) |
58 | 43 | {
|
59 | 44 | if (!($enum instanceof $this->enumClass)) {
|
60 |
| - $enum = new $this->enumClass($enum); |
| 45 | + $enum = call_user_func(array($this->enumClass, 'get'), $enum); |
61 | 46 | }
|
62 | 47 |
|
63 |
| - return array_key_exists($enum->getOrdinal(), $this->values); |
64 |
| - } |
65 |
| - |
66 |
| - public function currentValue() |
67 |
| - { |
68 |
| - return $this->values[$this->position]; |
| 48 | + return parent::getHash($enum); |
69 | 49 | }
|
70 | 50 |
|
71 |
| - public function currentEnum() |
| 51 | + public function offsetExists($enum) |
72 | 52 | {
|
73 |
| - return $this->enums[$this->position]; |
74 |
| - } |
| 53 | + if (!($enum instanceof $this->enumClass)) { |
| 54 | + $enum = call_user_func(array($this->enumClass, 'get'), $enum); |
| 55 | + } |
75 | 56 |
|
76 |
| - public function currentPosition() |
77 |
| - { |
78 |
| - return $this->position; |
| 57 | + return parent::offsetExists($enum); |
79 | 58 | }
|
80 | 59 |
|
81 |
| - /* Iterator */ |
82 |
| - |
83 |
| - public function current() |
| 60 | + public function offsetGet($enum) |
84 | 61 | {
|
85 |
| - return $this->currentValue(); |
86 |
| - } |
| 62 | + if (!($enum instanceof $this->enumClass)) { |
| 63 | + $enum = call_user_func(array($this->enumClass, 'get'), $enum); |
| 64 | + } |
87 | 65 |
|
88 |
| - public function key() |
89 |
| - { |
90 |
| - return $this->currentEnum()->getValue(); |
| 66 | + return parent::offsetGet($enum); |
91 | 67 | }
|
92 | 68 |
|
93 |
| - public function next() |
| 69 | + public function offsetSet($enum, $data = null) |
94 | 70 | {
|
95 |
| - ++$this->position; |
96 |
| - } |
| 71 | + if (!($enum instanceof $this->enumClass)) { |
| 72 | + $enum = call_user_func(array($this->enumClass, 'get'), $enum); |
| 73 | + } |
97 | 74 |
|
98 |
| - public function valid() |
99 |
| - { |
100 |
| - return ($this->position < $this->count()); |
| 75 | + parent::offsetSet($enum, $data); |
101 | 76 | }
|
102 | 77 |
|
103 |
| - public function rewind() |
| 78 | + public function offsetUnset($enum) |
104 | 79 | {
|
105 |
| - $this->position = 0; |
106 |
| - } |
107 |
| - |
108 |
| - /* Countable */ |
| 80 | + if (!($enum instanceof $this->enumClass)) { |
| 81 | + $enum = call_user_func(array($this->enumClass, 'get'), $enum); |
| 82 | + } |
109 | 83 |
|
110 |
| - public function count() |
111 |
| - { |
112 |
| - return count($this->enums); |
| 84 | + parent::offsetUnset($enum, $data); |
113 | 85 | }
|
114 | 86 | }
|
0 commit comments