Skip to content

Commit ac23ee1

Browse files
committed
fixed #70: removed iterator flags for EnumMap
From now on * the iterator key will be the enumerator instance * the iterator value will be the attached item value As using objects as iterator keys requires PHP-5-5 this will be the new min PHP version.
1 parent 848250b commit ac23ee1

File tree

5 files changed

+24
-285
lines changed

5 files changed

+24
-285
lines changed

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ env:
1616
matrix:
1717
fast_finish: true
1818
include:
19-
- php: 5.3.3
20-
- php: 5.3
21-
- php: 5.4
19+
- php: 5.5
2220
env:
2321
- CODE_COVERAGE="1"
24-
- php: 5.5
2522
- php: 5.6
2623
env:
2724
- PHPDOC="1"

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,10 @@ $enumMap->detach(UserStatus::INACTIVE);
237237
$enumMap->detach(UserStatus::DELETED());
238238

239239
// iterate
240-
var_dump(iterator_to_array($enumMap)); // array(0 => UserStatus{$value=1});
241-
242-
// define key and value used for iteration
243-
$enumMap->setFlags(EnumMap::KEY_AS_NAME | EnumMap::CURRENT_AS_DATA);
244-
var_dump(iterator_to_array($enumMap)); // array('ACTIVE' => 'aktiv');
240+
foreach ($enumMap as $enum => $value) {
241+
var_dump(get_class($enum)); // UserStatus
242+
var_dump(gettype($value)) // string
243+
}
245244
```
246245

247246
## Serializing

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "marc-mabe/php-enum",
3-
"description": "Simple and fast implementation of enumerations with native PHP 5.3 and upper",
3+
"description": "Simple and fast implementation of enumerations with native PHP 5.5 and upper",
44
"type": "library",
55
"keywords": [
66
"enum",
@@ -21,7 +21,7 @@
2121
}],
2222
"license": "BSD-3-Clause",
2323
"require": {
24-
"php": ">=5.3",
24+
"php": ">=5.5",
2525
"ext-reflection": "*"
2626
},
2727
"suggest": {

src/EnumMap.php

Lines changed: 8 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use SplObjectStorage;
66
use InvalidArgumentException;
7-
use RuntimeException;
87

98
/**
109
* EnumMap implementation in base of SplObjectStorage
@@ -15,72 +14,18 @@
1514
*/
1615
class EnumMap extends SplObjectStorage
1716
{
18-
19-
/**
20-
* key()-behaviour: return the iterator position
21-
*/
22-
const KEY_AS_INDEX = 1;
23-
24-
/**
25-
* key()-behaviour: return the name of the current element
26-
*/
27-
const KEY_AS_NAME = 2;
28-
29-
/**
30-
* key()-behaviour: return the value of the current element
31-
*/
32-
const KEY_AS_VALUE = 3;
33-
34-
/**
35-
* key()-behaviour: return the ordinal number of the current element
36-
*/
37-
const KEY_AS_ORDINAL = 4;
38-
39-
/**
40-
* current()-behaviour: return the instance of the enumerator
41-
*/
42-
const CURRENT_AS_ENUM = 8;
43-
44-
/**
45-
* current()-behaviour: return the data mapped the enumerator
46-
*/
47-
const CURRENT_AS_DATA = 16;
48-
49-
/**
50-
* current()-behaviour: return the name of the enumerator
51-
*/
52-
const CURRENT_AS_NAME = 24;
53-
54-
/**
55-
* current()-behaviour: return the value of the enumerator
56-
*/
57-
const CURRENT_AS_VALUE = 32;
58-
59-
/**
60-
* current()-behaviour: return the ordinal number of the enumerator
61-
*/
62-
const CURRENT_AS_ORDINAL = 40;
63-
6417
/**
6518
* The classname of the enumeration type
6619
* @var string
6720
*/
6821
private $enumeration;
6922

70-
/**
71-
* Flags to define behaviors
72-
* (Default = KEY_AS_INDEX | CURRENT_AS_ENUM)
73-
* @var int
74-
*/
75-
private $flags = 9;
76-
7723
/**
7824
* Constructor
79-
* @param string $enumeration The classname of the enumeration type
80-
* @param int|null $flags Behaviour flags, see KEY_AS_* and CURRENT_AS_* constants
25+
* @param string $enumeration The classname of the enumeration type
8126
* @throws InvalidArgumentException
8227
*/
83-
public function __construct($enumeration, $flags = null)
28+
public function __construct($enumeration)
8429
{
8530
if (!is_subclass_of($enumeration, __NAMESPACE__ . '\Enum')) {
8631
throw new InvalidArgumentException(sprintf(
@@ -89,10 +34,6 @@ public function __construct($enumeration, $flags = null)
8934
));
9035
}
9136
$this->enumeration = $enumeration;
92-
93-
if ($flags !== null) {
94-
$this->setFlags($flags);
95-
}
9637
}
9738

9839
/**
@@ -104,47 +45,6 @@ public function getEnumeration()
10445
return $this->enumeration;
10546
}
10647

107-
/**
108-
* Set behaviour flags
109-
* see KEY_AS_* and CURRENT_AS_* constants
110-
* @param int $flags
111-
* @return void
112-
* @throws InvalidArgumentException On invalid or unsupported flags
113-
*/
114-
public function setFlags($flags)
115-
{
116-
$flags = (int)$flags;
117-
118-
$keyFlag = $flags & 7;
119-
if ($keyFlag > 4) {
120-
throw new InvalidArgumentException(
121-
"Unsupported flag given for key() behavior"
122-
);
123-
} elseif (!$keyFlag) {
124-
$keyFlag = $this->flags & 7;
125-
}
126-
127-
$currentFlag = $flags & 56;
128-
if ($currentFlag > 40) {
129-
throw new InvalidArgumentException(
130-
"Unsupported flag given for current() behavior"
131-
);
132-
} elseif (!$currentFlag) {
133-
$currentFlag = $this->flags & 56;
134-
}
135-
136-
$this->flags = $keyFlag | $currentFlag;
137-
}
138-
139-
/**
140-
* Get the behaviour flags
141-
* @return int
142-
*/
143-
public function getFlags()
144-
{
145-
return $this->flags;
146-
}
147-
14848
/**
14949
* Attach a new enumerator or overwrite an existing one
15050
* @param Enum|null|boolean|int|float|string $enumerator
@@ -188,13 +88,12 @@ public function detach($enumerator)
18888

18989
/**
19090
* Get a unique identifier for the given enumerator
191-
* @param Enum|scalar $enumerator
91+
* @param Enum|null|boolean|int|float|string $enumerator
19292
* @return string
19393
* @throws InvalidArgumentException On an invalid given enumerator
19494
*/
19595
public function getHash($enumerator)
19696
{
197-
// getHash is available since PHP 5.4
19897
$enumeration = $this->enumeration;
19998
return spl_object_hash($enumeration::get($enumerator));
20099
}
@@ -250,46 +149,20 @@ public function offsetUnset($enumerator)
250149
}
251150

252151
/**
253-
* Get the current item
254-
* The return value varied by the behaviour of the current flag
152+
* Get the current value
255153
* @return mixed
256154
*/
257155
public function current()
258156
{
259-
switch ($this->flags & 120) {
260-
case self::CURRENT_AS_ENUM:
261-
return parent::current();
262-
case self::CURRENT_AS_DATA:
263-
return parent::getInfo();
264-
case self::CURRENT_AS_VALUE:
265-
return parent::current()->getValue();
266-
case self::CURRENT_AS_NAME:
267-
return parent::current()->getName();
268-
case self::CURRENT_AS_ORDINAL:
269-
return parent::current()->getOrdinal();
270-
default:
271-
throw new RuntimeException('Invalid current flag');
272-
}
157+
return parent::getInfo();
273158
}
274159

275160
/**
276-
* Get the current item-key
277-
* The return value varied by the behaviour of the key flag
278-
* @return null|boolean|int|float|string
161+
* Get the current key
162+
* @return Enum|null
279163
*/
280164
public function key()
281165
{
282-
switch ($this->flags & 7) {
283-
case self::KEY_AS_INDEX:
284-
return parent::key();
285-
case self::KEY_AS_NAME:
286-
return parent::current()->getName();
287-
case self::KEY_AS_ORDINAL:
288-
return parent::current()->getOrdinal();
289-
case self::KEY_AS_VALUE:
290-
return parent::current()->getValue();
291-
default:
292-
throw new RuntimeException('Invalid key flag');
293-
}
166+
return parent::current();
294167
}
295168
}

0 commit comments

Comments
 (0)