Skip to content

Commit 3dc462f

Browse files
committed
EnumMap extends SplObjectHash
1 parent ed9a5ec commit 3dc462f

File tree

2 files changed

+59
-80
lines changed

2 files changed

+59
-80
lines changed

src/MabeEnum/EnumMap.php

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,86 @@
11
<?php
22

3-
class MabeEnum_EnumMap implements Iterator, Countable
3+
namespace MabeEnum;
4+
5+
use SplObjectStorage;
6+
7+
class EnumMap extends SplObjectStorage
48
{
5-
private $enums = array();
6-
private $values = array();
7-
private $position = 0;
8-
private $enumClass;
99

1010
public function __construct($enumClass)
1111
{
1212
$this->enumClass = $enumClass;
1313
}
1414

15-
public function attach($enum, $value)
15+
public function attach($enum, $data = null)
1616
{
1717
if (!($enum instanceof $this->enumClass)) {
18-
$enum = new $this->enumClass($enum);
18+
$enum = call_user_func(array($this->enumClass, 'get'), $enum);
1919
}
2020

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);
2822
}
2923

30-
public function detach($enum)
24+
public function contains($enum)
3125
{
3226
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);
3828
}
3929

40-
$ordinal = $enum->getOrdinal();
41-
unset($this->values[$ordinal], $this->enums[$ordinal]);
30+
return parent::contains($enum);
4231
}
4332

44-
public function get($enum)
33+
public function detach($enum)
4534
{
4635
if (!($enum instanceof $this->enumClass)) {
47-
$enum = new $this->enumClass($enum);
36+
$enum = call_user_func(array($this->enumClass, 'get'), $enum);
4837
}
4938

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);
5540
}
5641

57-
public function contains($enum)
42+
public function getHash($enum)
5843
{
5944
if (!($enum instanceof $this->enumClass)) {
60-
$enum = new $this->enumClass($enum);
45+
$enum = call_user_func(array($this->enumClass, 'get'), $enum);
6146
}
6247

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);
6949
}
7050

71-
public function currentEnum()
51+
public function offsetExists($enum)
7252
{
73-
return $this->enums[$this->position];
74-
}
53+
if (!($enum instanceof $this->enumClass)) {
54+
$enum = call_user_func(array($this->enumClass, 'get'), $enum);
55+
}
7556

76-
public function currentPosition()
77-
{
78-
return $this->position;
57+
return parent::offsetExists($enum);
7958
}
8059

81-
/* Iterator */
82-
83-
public function current()
60+
public function offsetGet($enum)
8461
{
85-
return $this->currentValue();
86-
}
62+
if (!($enum instanceof $this->enumClass)) {
63+
$enum = call_user_func(array($this->enumClass, 'get'), $enum);
64+
}
8765

88-
public function key()
89-
{
90-
return $this->currentEnum()->getValue();
66+
return parent::offsetGet($enum);
9167
}
9268

93-
public function next()
69+
public function offsetSet($enum, $data = null)
9470
{
95-
++$this->position;
96-
}
71+
if (!($enum instanceof $this->enumClass)) {
72+
$enum = call_user_func(array($this->enumClass, 'get'), $enum);
73+
}
9774

98-
public function valid()
99-
{
100-
return ($this->position < $this->count());
75+
parent::offsetSet($enum, $data);
10176
}
10277

103-
public function rewind()
78+
public function offsetUnset($enum)
10479
{
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+
}
10983

110-
public function count()
111-
{
112-
return count($this->enums);
84+
parent::offsetUnset($enum, $data);
11385
}
11486
}

tests/MabeEnumTest/EnumMapTest.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
<?php
22

3+
namespace MabeEnumTest;
4+
5+
use MabeEnum\Enum;
6+
use MabeEnum\EnumMap;
7+
use MabeEnumTest\TestAsset\EnumWithoutDefaultValue;
8+
use PHPUnit_Framework_TestCase as TestCase;
9+
310
/**
4-
* Unit tests for the class MabeEnum_Enum
11+
* Unit tests for the class MabeEnum\EnumMap
512
*
613
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
714
* @copyright Copyright (c) 2012 Marc Bennewitz
815
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
916
*/
10-
class MabeEnumTest_EnumMapTest extends PHPUnit_Framework_TestCase
17+
class EnumMapTest extends TestCase
1118
{
1219
public function testBasic()
1320
{
14-
$enumMap = new MabeEnum_EnumMap('MabeEnumTest_TestAsset_EnumWithoutDefaultValue');
21+
$enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumWithoutDefaultValue');
1522

16-
$enum1 = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
23+
$enum1 = EnumWithoutDefaultValue::ONE();
1724
$value1 = 'value2';
1825

19-
$enum2 = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO);
26+
$enum2 = EnumWithoutDefaultValue::TWO();
2027
$value2 = 'value2';
2128

2229
$this->assertFalse($enumMap->contains($enum1));
2330
$this->assertNull($enumMap->attach($enum1, $value1));
2431
$this->assertTrue($enumMap->contains($enum1));
25-
$this->assertSame($value1, $enumMap->get($enum1));
32+
$this->assertSame($value1, $enumMap[$enum1]);
2633

2734
$this->assertFalse($enumMap->contains($enum2));
2835
$this->assertNull($enumMap->attach($enum2, $value2));
2936
$this->assertTrue($enumMap->contains($enum2));
30-
$this->assertSame($value2, $enumMap->get($enum2));
37+
$this->assertSame($value2, $enumMap[$enum2]);
3138

3239
$this->assertNull($enumMap->detach($enum1));
3340
$this->assertFalse($enumMap->contains($enum1));
@@ -38,23 +45,23 @@ public function testBasic()
3845

3946
public function testBasicWithConstantValuesAsEnums()
4047
{
41-
$enumMap = new MabeEnum_EnumMap('MabeEnumTest_TestAsset_EnumWithoutDefaultValue');
48+
$enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumWithoutDefaultValue');
4249

43-
$enum1 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE;
50+
$enum1 = EnumWithoutDefaultValue::ONE;
4451
$value1 = 'value2';
4552

46-
$enum2 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO;
53+
$enum2 = EnumWithoutDefaultValue::TWO;
4754
$value2 = 'value2';
4855

4956
$this->assertFalse($enumMap->contains($enum1));
5057
$this->assertNull($enumMap->attach($enum1, $value1));
5158
$this->assertTrue($enumMap->contains($enum1));
52-
$this->assertSame($value1, $enumMap->get($enum1));
59+
$this->assertSame($value1, $enumMap[$enum1]);
5360

5461
$this->assertFalse($enumMap->contains($enum2));
5562
$this->assertNull($enumMap->attach($enum2, $value2));
5663
$this->assertTrue($enumMap->contains($enum2));
57-
$this->assertSame($value2, $enumMap->get($enum2));
64+
$this->assertSame($value2, $enumMap[$enum2]);
5865

5966
$this->assertNull($enumMap->detach($enum1));
6067
$this->assertFalse($enumMap->contains($enum1));
@@ -65,12 +72,12 @@ public function testBasicWithConstantValuesAsEnums()
6572

6673
public function testIterate()
6774
{
68-
$enumMap = new MabeEnum_EnumMap('MabeEnumTest_TestAsset_EnumWithoutDefaultValue');
75+
$enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumWithoutDefaultValue');
6976

70-
$enum1 = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
77+
$enum1 = EnumWithoutDefaultValue::ONE();
7178
$value1 = 'value2';
7279

73-
$enum2 = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO);
80+
$enum2 = EnumWithoutDefaultValue::TWO();
7481
$value2 = 'value2';
7582

7683
// an empty enum map needs to be invalid, starting by 0

0 commit comments

Comments
 (0)