Skip to content

Commit 9a00cad

Browse files
committed
Merge branch 'feature/EnumSet' of github.com:marc-mabe/php-enum
2 parents d9c3229 + 5a10bc6 commit 9a00cad

File tree

12 files changed

+29
-22
lines changed

12 files changed

+29
-22
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2012, Marc Bennewitz
1+
Copyright (c) 2013, Marc Bennewitz
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without modification,

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
strict="true"
1212
>
1313
<testsuite name="php-enum Test-Suite">
14-
<directory>./tests/MabeEnumTest</directory>
14+
<directory>./tests</directory>
1515
</testsuite>
1616
<filter>
1717
<whitelist addUncoveredFilesFromWhitelist="true">

src/MabeEnum/Enum.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Class to implement enumerations for PHP 5 (without SplEnum)
1212
*
1313
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
14-
* @copyright Copyright (c) 2012 Marc Bennewitz
14+
* @copyright Copyright (c) 2013 Marc Bennewitz
1515
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
1616
*/
1717
abstract class Enum
@@ -109,7 +109,7 @@ final public function getOrdinal()
109109
// detect ordinal
110110
$ordinal = 0;
111111
$value = $this->value;
112-
foreach ($this::getConstants() as $constValue) {
112+
foreach (static::getConstants() as $constValue) {
113113
if ($value === $constValue) {
114114
break;
115115
}
@@ -131,13 +131,12 @@ final public function getOrdinal()
131131
final static public function get($value)
132132
{
133133
$class = get_called_class();
134-
$id = $class . '.' . $value;
135-
if (isset(self::$instances[$id])) {
136-
return self::$instances[$id];
134+
if (isset(self::$instances[$class][$value])) {
135+
return self::$instances[$class][$value];
137136
}
138137

139138
$instance = new $class($value);
140-
self::$instances[$id] = $instance;
139+
self::$instances[$class][$value] = $instance;
141140
return $instance;
142141
}
143142

@@ -193,11 +192,9 @@ final static public function clear()
193192
$class = get_called_class();
194193

195194
// clear instantiated enums
196-
$prefix = $class . '.';
197-
$prefixLength = strlen($prefix);
198-
foreach (self::$instances as $id => $enum) {
199-
if (strncasecmp($prefix, $id, $prefixLength) === 0) {
200-
unset(self::$instances[$id]);
195+
foreach (self::$instances as $instanceClass => $enum) {
196+
if (strcasecmp($class, $instanceClass) === 0) {
197+
unset(self::$instances[$instanceClass]);
201198
}
202199
}
203200

src/MabeEnum/EnumMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* EnumMap implementation in base of SplObjectStorage
1111
*
1212
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
13-
* @copyright Copyright (c) 2012 Marc Bennewitz
13+
* @copyright Copyright (c) 2013 Marc Bennewitz
1414
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
1515
*/
1616
class EnumMap extends SplObjectStorage

src/MabeEnum/EnumSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* EnumSet implementation in base of SplObjectStorage
1212
*
1313
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
14-
* @copyright Copyright (c) 2012 Marc Bennewitz
14+
* @copyright Copyright (c) 2013 Marc Bennewitz
1515
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
1616
*/
1717
class EnumSet implements Iterator, Countable

tests/MabeEnumTest/EnumMapTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Unit tests for the class MabeEnum\EnumMap
1414
*
1515
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
16-
* @copyright Copyright (c) 2012 Marc Bennewitz
16+
* @copyright Copyright (c) 2013 Marc Bennewitz
1717
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
1818
*/
1919
class EnumMapTest extends TestCase

tests/MabeEnumTest/EnumSet.php renamed to tests/MabeEnumTest/EnumSetTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Unit tests for the class MabeEnum\EnumSet
1313
*
1414
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
15-
* @copyright Copyright (c) 2012 Marc Bennewitz
15+
* @copyright Copyright (c) 2013 Marc Bennewitz
1616
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
1717
*/
1818
class EnumSetTest extends TestCase
@@ -21,6 +21,7 @@ public function testBasic()
2121
{
2222
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumWithoutDefaultValue');
2323
$this->assertSame('MabeEnumTest\TestAsset\EnumWithoutDefaultValue', $enumSet->getEnumClass());
24+
$this->assertSame(EnumSet::UNIQUE, $enumSet->getFlags());
2425

2526
$enum1 = EnumWithoutDefaultValue::ONE();
2627
$enum2 = EnumWithoutDefaultValue::TWO();
@@ -65,6 +66,7 @@ public function testBasicWithConstantValuesAsEnums()
6566
public function testUnique()
6667
{
6768
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumWithoutDefaultValue', EnumSet::UNIQUE);
69+
$this->assertSame(EnumSet::UNIQUE, $enumSet->getFlags());
6870

6971
$enumSet->attach(EnumWithoutDefaultValue::ONE());
7072
$enumSet->attach(EnumWithoutDefaultValue::ONE);
@@ -78,6 +80,7 @@ public function testUnique()
7880
public function testNotUnique()
7981
{
8082
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumWithoutDefaultValue', 0);
83+
$this->assertSame(0, $enumSet->getFlags());
8184

8285
$enumSet->attach(EnumWithoutDefaultValue::ONE());
8386
$enumSet->attach(EnumWithoutDefaultValue::ONE);
@@ -102,6 +105,7 @@ public function testIterateUnordered()
102105
// an empty enum set needs to be invalid, starting by 0
103106
$this->assertSame(0, $enumSet->count());
104107
$this->assertFalse($enumSet->valid());
108+
$this->assertNull($enumSet->current());
105109

106110
// attach
107111
$enumSet->attach($enum1);
@@ -123,6 +127,7 @@ public function testIterateUnordered()
123127
$this->assertNull($enumSet->next());
124128
$this->assertFalse($enumSet->valid());
125129
$this->assertSame(2, $enumSet->key());
130+
$this->assertNull($enumSet->current());
126131

127132
// rewind will set the iterator position back to 0
128133
$enumSet->rewind();
@@ -141,6 +146,7 @@ public function testIterateOrdered()
141146
// an empty enum set needs to be invalid, starting by 0
142147
$this->assertSame(0, $enumSet->count());
143148
$this->assertFalse($enumSet->valid());
149+
$this->assertNull($enumSet->current());
144150

145151
// attach
146152
$enumSet->attach($enum2);
@@ -162,6 +168,7 @@ public function testIterateOrdered()
162168
$this->assertNull($enumSet->next());
163169
$this->assertFalse($enumSet->valid());
164170
$this->assertSame(2, $enumSet->key());
171+
$this->assertNull($enumSet->current());
165172

166173
// rewind will set the iterator position back to 0
167174
$enumSet->rewind();
@@ -180,6 +187,7 @@ public function testIterateOrderedNotUnique()
180187
// an empty enum set needs to be invalid, starting by 0
181188
$this->assertSame(0, $enumSet->count());
182189
$this->assertFalse($enumSet->valid());
190+
$this->assertNull($enumSet->current());
183191

184192
// attach
185193
$enumSet->attach($enum2);
@@ -215,6 +223,7 @@ public function testIterateOrderedNotUnique()
215223
$this->assertNull($enumSet->next());
216224
$this->assertFalse($enumSet->valid());
217225
$this->assertSame(4, $enumSet->key());
226+
$this->assertNull($enumSet->current());
218227

219228
// rewind will set the iterator position back to 0
220229
$enumSet->rewind();
@@ -247,6 +256,7 @@ public function testIterateAndDetach()
247256
// detach enum of current index if the last index
248257
$enumSet->detach($enumSet->current());
249258
$this->assertFalse($enumSet->valid());
259+
$this->assertNull($enumSet->current());
250260
}
251261

252262
public function testConstructThrowsInvalidArgumentExceptionIfEnumClassDoesNotExtendBaseEnum()

tests/MabeEnumTest/EnumTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Unit tests for the class MabeEnum\Enum
1313
*
1414
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
15-
* @copyright Copyright (c) 2012 Marc Bennewitz
15+
* @copyright Copyright (c) 2013 Marc Bennewitz
1616
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
1717
*/
1818
class EnumTest extends TestCase

tests/MabeEnumTest/TestAsset/EmptyEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Unit tests for the class MabeEnum\Enum
99
*
1010
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
11-
* @copyright Copyright (c) 2012 Marc Bennewitz
11+
* @copyright Copyright (c) 2013 Marc Bennewitz
1212
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
1313
*/
1414
class EmptyEnum extends Enum

tests/MabeEnumTest/TestAsset/EnumAmbiguous.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Unit tests for the class MabeEnum\Enum
99
*
1010
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
11-
* @copyright Copyright (c) 2012 Marc Bennewitz
11+
* @copyright Copyright (c) 2013 Marc Bennewitz
1212
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
1313
*/
1414
class EnumAmbiguous extends Enum

0 commit comments

Comments
 (0)