Skip to content

Commit 4a76a7e

Browse files
committed
first something working version of EnumMap
1 parent 36c8081 commit 4a76a7e

File tree

2 files changed

+156
-18
lines changed

2 files changed

+156
-18
lines changed

src/MabeEnum/EnumMap.php

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ class MabeEnum_EnumMap implements Iterator, Countable
44
{
55
private $enums = array();
66
private $values = array();
7-
7+
private $position = 0;
88
private $enumClass;
99

1010
public function __construct($enumClass)
1111
{
12-
$reflectionClass = new ReflectionClass($enumClass);
13-
if (!reflectionClass->isInstanceOf('MabeEnum_Enum')) {
14-
throw new InvalidArgumentException("'{$enumClass}' have to be an instance of 'MabeEnum_Enum'");
15-
}
16-
17-
$this->enumClass = $enumClass;
12+
$this->enumClass = $enumClass;
1813
}
1914

2015
public function attach($enum, $value)
@@ -32,6 +27,20 @@ public function attach($enum, $value)
3227
$this->enums[$ordinal] = $enum;
3328
}
3429

30+
public function detach($enum)
31+
{
32+
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");
38+
}
39+
40+
$ordinal = $enum->getOrdinal();
41+
unset($this->values[$ordinal], $this->enums[$ordinal]);
42+
}
43+
3544
public function get($enum)
3645
{
3746
if (!($enum instanceof $this->enumClass)) {
@@ -54,28 +63,52 @@ public function contains($enum)
5463
return array_key_exists($enum->getOrdinal(), $this->values);
5564
}
5665

57-
public function detach($enum)
66+
public function currentValue()
5867
{
59-
if (!($enum instanceof $this->enumClass)) {
60-
$enum = new $this->enumClass($enum);
61-
}
68+
return $this->values[$this->position];
69+
}
6270

63-
if (!$this->contains($enum)) {
64-
throw new RuntimeException("'{$enum}' not attached to map");
65-
}
71+
public function currentEnum()
72+
{
73+
return $this->enums[$this->position];
74+
}
6675

67-
$ordinal = $enum->getOrdinal();
68-
unset($this->values[$ordinal], $this->enums[$ordinal]);
76+
public function currentPosition()
77+
{
78+
return $this->position;
6979
}
7080

7181
/* Iterator */
7282

73-
83+
public function current()
84+
{
85+
return $this->currentValue();
86+
}
87+
88+
public function key()
89+
{
90+
return $this->currentEnum()->getValue();
91+
}
92+
93+
public function next()
94+
{
95+
++$this->position;
96+
}
97+
98+
public function valid()
99+
{
100+
return ($this->position < $this->count());
101+
}
102+
103+
public function rewind()
104+
{
105+
$this->position = 0;
106+
}
74107

75108
/* Countable */
76109

77110
public function count()
78111
{
79-
return count($this->map);
112+
return count($this->enums);
80113
}
81114
}

tests/MabeEnumTest/EnumMapTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/**
4+
* Unit tests for the class MabeEnum_Enum
5+
*
6+
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
7+
* @copyright Copyright (c) 2012 Marc Bennewitz
8+
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
9+
*/
10+
class MabeEnumTest_EnumMapTest extends PHPUnit_Framework_TestCase
11+
{
12+
public function testBasic()
13+
{
14+
$enumMap = new MabeEnum_EnumMap('MabeEnumTest_TestAsset_EnumWithoutDefaultValue');
15+
16+
$enum1 = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
17+
$value1 = 'value2';
18+
19+
$enum2 = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO);
20+
$value2 = 'value2';
21+
22+
$this->assertFalse($enumMap->contains($enum1));
23+
$this->assertNull($enumMap->attach($enum1, $value1));
24+
$this->assertTrue($enumMap->contains($enum1));
25+
$this->assertSame($value1, $enumMap->get($enum1));
26+
27+
$this->assertFalse($enumMap->contains($enum2));
28+
$this->assertNull($enumMap->attach($enum2, $value2));
29+
$this->assertTrue($enumMap->contains($enum2));
30+
$this->assertSame($value2, $enumMap->get($enum2));
31+
32+
$this->assertNull($enumMap->detach($enum1));
33+
$this->assertFalse($enumMap->contains($enum1));
34+
35+
$this->assertNull($enumMap->detach($enum2));
36+
$this->assertFalse($enumMap->contains($enum2));
37+
}
38+
39+
public function testBasicWithConstantValuesAsEnums()
40+
{
41+
$enumMap = new MabeEnum_EnumMap('MabeEnumTest_TestAsset_EnumWithoutDefaultValue');
42+
43+
$enum1 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE;
44+
$value1 = 'value2';
45+
46+
$enum2 = MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO;
47+
$value2 = 'value2';
48+
49+
$this->assertFalse($enumMap->contains($enum1));
50+
$this->assertNull($enumMap->attach($enum1, $value1));
51+
$this->assertTrue($enumMap->contains($enum1));
52+
$this->assertSame($value1, $enumMap->get($enum1));
53+
54+
$this->assertFalse($enumMap->contains($enum2));
55+
$this->assertNull($enumMap->attach($enum2, $value2));
56+
$this->assertTrue($enumMap->contains($enum2));
57+
$this->assertSame($value2, $enumMap->get($enum2));
58+
59+
$this->assertNull($enumMap->detach($enum1));
60+
$this->assertFalse($enumMap->contains($enum1));
61+
62+
$this->assertNull($enumMap->detach($enum2));
63+
$this->assertFalse($enumMap->contains($enum2));
64+
}
65+
66+
public function testIterate()
67+
{
68+
$enumMap = new MabeEnum_EnumMap('MabeEnumTest_TestAsset_EnumWithoutDefaultValue');
69+
70+
$enum1 = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::ONE);
71+
$value1 = 'value2';
72+
73+
$enum2 = new MabeEnumTest_TestAsset_EnumWithoutDefaultValue(MabeEnumTest_TestAsset_EnumWithoutDefaultValue::TWO);
74+
$value2 = 'value2';
75+
76+
// an empty enum map needs to be invalid, starting by 0
77+
$this->assertSame(0, $enumMap->count());
78+
$this->assertFalse($enumMap->valid());
79+
80+
// attach in revert order shouldn't change ordering of iteration
81+
$enumMap->attach($enum2, $value2);
82+
$enumMap->attach($enum1, $value1);
83+
84+
// a not empty enum map should be valid, starting by 0 (if not iterated)
85+
$this->assertSame(2, $enumMap->count());
86+
$this->assertTrue($enumMap->valid());
87+
$this->assertSame(0, $enumMap->currentPosition());
88+
$this->assertSame($enum1, $enumMap->key());
89+
$this->assertSame($value1, $enumMap->current());
90+
91+
// go to the next element (last)
92+
$this->assertNull($enumMap->next());
93+
$this->assertTrue($enumMap->valid());
94+
$this->assertSame(1, $enumMap->currentPosition());
95+
$this->assertSame($enum2, $enumMap->key());
96+
$this->assertSame($value2, $enumMap->current());
97+
98+
// go to the next element (out of range)
99+
$this->assertNull($enumMap->next());
100+
$this->assertFalse($enumMap->valid());
101+
$this->assertSame(2, $enumMap->currentPosition());
102+
//$this->assertSame($enum2, $enumMap->currentEnum());
103+
//$this->assertSame($value2, $enumMap->currentValue());
104+
}
105+
}

0 commit comments

Comments
 (0)