Skip to content

Commit 734061e

Browse files
committed
Wording: enum vs. enumeration vs. enumerator
1 parent bef749d commit 734061e

File tree

6 files changed

+119
-86
lines changed

6 files changed

+119
-86
lines changed

src/MabeEnum/Enum.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ final public function getOrdinal()
141141
/**
142142
* Compare this enumerator against another and check if it's the same.
143143
*
144-
* @param mixed $enum
144+
* @param mixed $enumerator
145145
* @return bool
146146
*/
147-
final public function is($enum)
147+
final public function is($enumerator)
148148
{
149-
return $this === $enum || $this->value === $enum;
149+
return $this === $enumerator || $this->value === $enumerator;
150150
}
151151

152152
/**

src/MabeEnum/EnumMap.php

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ class EnumMap extends SplObjectStorage
6262
const CURRENT_AS_ORDINAL = 40;
6363

6464
/**
65-
* The classname of an enumeration this map is for
65+
* The classname of the enumeration type
6666
* @var string
6767
*/
68-
private $enumClass;
68+
private $enumeration;
6969

7070
/**
7171
* Flags to define behaviors
@@ -76,32 +76,42 @@ class EnumMap extends SplObjectStorage
7676

7777
/**
7878
* Constructor
79-
* @param string $enumClass The classname of an enumeration the map is for
80-
* @param int|null $flags Behaviour flags, see KEY_AS_* and CURRENT_AS_* constants
79+
* @param string $enumeration The classname of the enumeration type
80+
* @param int|null $flags Behaviour flags, see KEY_AS_* and CURRENT_AS_* constants
8181
* @throws InvalidArgumentException
8282
*/
83-
public function __construct($enumClass, $flags = null)
83+
public function __construct($enumeration, $flags = null)
8484
{
85-
if (!is_subclass_of($enumClass, __NAMESPACE__ . '\Enum')) {
85+
if (!is_subclass_of($enumeration, __NAMESPACE__ . '\Enum')) {
8686
throw new InvalidArgumentException(sprintf(
8787
"This EnumMap can handle subclasses of '%s' only",
8888
__NAMESPACE__ . '\Enum'
8989
));
9090
}
91-
$this->enumClass = $enumClass;
91+
$this->enumeration = $enumeration;
9292

9393
if ($flags !== null) {
9494
$this->setFlags($flags);
9595
}
9696
}
9797

9898
/**
99-
* Get the classname of the enumeration this map is for
99+
* Get the classname of the enumeration
100100
* @return string
101+
* @deprecated Please use getEnumeration() instead
101102
*/
102103
public function getEnumClass()
103104
{
104-
return $this->enumClass;
105+
return $this->getEnumeration();
106+
}
107+
108+
/**
109+
* Get the classname of the enumeration
110+
* @return string
111+
*/
112+
public function getEnumeration()
113+
{
114+
return $this->enumeration;
105115
}
106116

107117
/**
@@ -147,27 +157,27 @@ public function getFlags()
147157

148158
/**
149159
* Attach a new enumerator or overwrite an existing one
150-
* @param Enum|null|boolean|int|float|string $enum
160+
* @param Enum|null|boolean|int|float|string $enumerator
151161
* @param mixed $data
152162
* @return void
153-
* @throws InvalidArgumentException On an invalid given enum
163+
* @throws InvalidArgumentException On an invalid given enumerator
154164
*/
155-
public function attach($enum, $data = null)
165+
public function attach($enumerator, $data = null)
156166
{
157-
$enumClass = $this->enumClass;
158-
parent::attach($enumClass::get($enum), $data);
167+
$enumeration = $this->enumeration;
168+
parent::attach($enumeration::get($enumerator), $data);
159169
}
160170

161171
/**
162172
* Test if the given enumerator exists
163-
* @param Enum|null|boolean|int|float|string $enum
173+
* @param Enum|null|boolean|int|float|string $enumerator
164174
* @return boolean
165175
*/
166-
public function contains($enum)
176+
public function contains($enumerator)
167177
{
168178
try {
169-
$enumClass = $this->enumClass;
170-
return parent::contains($enumClass::get($enum));
179+
$enumeration = $this->enumeration;
180+
return parent::contains($enumeration::get($enumerator));
171181
} catch (InvalidArgumentException $e) {
172182
// On an InvalidArgumentException the given argument can't be contained in this map
173183
return false;
@@ -176,77 +186,77 @@ public function contains($enum)
176186

177187
/**
178188
* Detach an enumerator
179-
* @param Enum|null|boolean|int|float|string $enum
189+
* @param Enum|null|boolean|int|float|string $enumerator
180190
* @return void
181-
* @throws InvalidArgumentException On an invalid given enum
191+
* @throws InvalidArgumentException On an invalid given enumerator
182192
*/
183-
public function detach($enum)
193+
public function detach($enumerator)
184194
{
185-
$enumClass = $this->enumClass;
186-
parent::detach($enumClass::get($enum));
195+
$enumeration = $this->enumeration;
196+
parent::detach($enumeration::get($enumerator));
187197
}
188198

189199
/**
190200
* Get a unique identifier for the given enumerator
191-
* @param Enum|scalar $enum
201+
* @param Enum|scalar $enumerator
192202
* @return string
193-
* @throws InvalidArgumentException On an invalid given enum
203+
* @throws InvalidArgumentException On an invalid given enumerator
194204
*/
195-
public function getHash($enum)
205+
public function getHash($enumerator)
196206
{
197207
// getHash is available since PHP 5.4
198-
$enumClass = $this->enumClass;
199-
return spl_object_hash($enumClass::get($enum));
208+
$enumeration = $this->enumeration;
209+
return spl_object_hash($enumeration::get($enumerator));
200210
}
201211

202212
/**
203213
* Test if the given enumerator exists
204-
* @param Enum|null|boolean|int|float|string $enum
214+
* @param Enum|null|boolean|int|float|string $enumerator
205215
* @return boolean
206216
* @see contains()
207217
*/
208-
public function offsetExists($enum)
218+
public function offsetExists($enumerator)
209219
{
210-
return $this->contains($enum);
220+
return $this->contains($enumerator);
211221
}
212222

213223
/**
214224
* Get mapped data for the given enumerator
215-
* @param Enum|null|boolean|int|float|string $enum
225+
* @param Enum|null|boolean|int|float|string $enumerator
216226
* @return mixed
217-
* @throws InvalidArgumentException On an invalid given enum
227+
* @throws InvalidArgumentException On an invalid given enumerator
218228
*/
219-
public function offsetGet($enum)
229+
public function offsetGet($enumerator)
220230
{
221-
$enumClass = $this->enumClass;
222-
return parent::offsetGet($enumClass::get($enum));
231+
$enumeration = $this->enumeration;
232+
return parent::offsetGet($enumeration::get($enumerator));
223233
}
224234

225235
/**
226236
* Attach a new enumerator or overwrite an existing one
227-
* @param Enum|null|boolean|int|float|string $enum
237+
* @param Enum|null|boolean|int|float|string $enumerator
228238
* @param mixed $data
229239
* @return void
230-
* @throws InvalidArgumentException On an invalid given enum
240+
* @throws InvalidArgumentException On an invalid given enumerator
231241
* @see attach()
232242
*/
233-
public function offsetSet($enum, $data = null)
243+
public function offsetSet($enumerator, $data = null)
234244
{
235-
$enumClass = $this->enumClass;
236-
parent::offsetSet($enumClass::get($enum), $data);
245+
$enumeration = $this->enumeration;
246+
parent::offsetSet($enumeration::get($enumerator), $data);
237247
}
238248

239249
/**
240250
* Detach an existing enumerator
241-
* @param Enum|null|boolean|int|float|string $enum
251+
* @param Enum|null|boolean|int|float|string $enumerator
242252
* @return void
243-
* @throws InvalidArgumentException On an invalid given enum
253+
* @throws InvalidArgumentException On an invalid given enumerator
244254
* @see detach()
245255
*/
246-
public function offsetUnset($enum)
256+
public function offsetUnset($enumerator)
247257
{
248-
$enumClass = $this->enumClass;
249-
parent::offsetUnset($enumClass::get($enum));
258+
$enumeration = $this->enumeration;
259+
parent::offsetUnset($enumeration::get($enumerator));
250260
}
251261

252262
/**

src/MabeEnum/EnumSet.php

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
class EnumSet implements Iterator, Countable
1818
{
1919
/**
20-
* Enumeration class
20+
* The classname of the Enumeration
2121
* @var string
2222
*/
23-
private $enumClass;
23+
private $enumeration;
2424

2525
/**
2626
* BitSet of all attached enumerations
@@ -43,27 +43,27 @@ class EnumSet implements Iterator, Countable
4343
/**
4444
* Constructor
4545
*
46-
* @param string $enumClass Classname of an enumeration the set is for
46+
* @param string $enumeration The classname of the enumeration
4747
* @throws InvalidArgumentException
4848
*/
49-
public function __construct($enumClass)
49+
public function __construct($enumeration)
5050
{
51-
if (!is_subclass_of($enumClass, __NAMESPACE__ . '\Enum')) {
51+
if (!is_subclass_of($enumeration, __NAMESPACE__ . '\Enum')) {
5252
throw new InvalidArgumentException(sprintf(
5353
"This EnumSet can handle subclasses of '%s' only",
5454
__NAMESPACE__ . '\Enum'
5555
));
5656
}
5757

58-
$this->enumClass = $enumClass;
59-
$this->ordinalMax = count($enumClass::getConstants());
58+
$this->enumeration = $enumeration;
59+
$this->ordinalMax = count($enumeration::getConstants());
6060

6161
if (PHP_INT_SIZE * 8 < $this->ordinalMax) {
6262
throw new OutOfRangeException(sprintf(
63-
"Your system can handle up to %u enumeration values within an EnumSet"
64-
. " but the given enumeration class '%s' has defined %u enumeration values",
63+
"Your system can handle up to %u enumerators within an EnumSet"
64+
. " but the given enumeration '%s' has defined %u enumerators",
6565
PHP_INT_SIZE * 8,
66-
$enumClass,
66+
$enumeration,
6767
$this->ordinalMax
6868
));
6969
}
@@ -72,65 +72,75 @@ public function __construct($enumClass)
7272
/**
7373
* Get the classname of enumeration this set is for
7474
* @return string
75+
* @deprecated Please use getEnumeration() instead
7576
*/
7677
public function getEnumClass()
7778
{
78-
return $this->enumClass;
79+
return $this->getEnumeration();
7980
}
8081

8182
/**
82-
* Attach a new enumeration or overwrite an existing one
83-
* @param Enum|null|boolean|int|float|string $enum
83+
* Get the classname of the enumeration
84+
* @return string
85+
*/
86+
public function getEnumeration()
87+
{
88+
return $this->enumeration;
89+
}
90+
91+
/**
92+
* Attach a new enumerator or overwrite an existing one
93+
* @param Enum|null|boolean|int|float|string $enumerator
8494
* @return void
85-
* @throws InvalidArgumentException On an invalid given enum
95+
* @throws InvalidArgumentException On an invalid given enumerator
8696
*/
87-
public function attach($enum)
97+
public function attach($enumerator)
8898
{
89-
$enumClass = $this->enumClass;
90-
$this->bitset |= 1 << $enumClass::get($enum)->getOrdinal();
99+
$enumeration = $this->enumeration;
100+
$this->bitset |= 1 << $enumeration::get($enumerator)->getOrdinal();
91101
}
92102

93103
/**
94-
* Detach all enumerations same as the given enum
95-
* @param Enum|null|boolean|int|float|string $enum
104+
* Detach the given enumerator
105+
* @param Enum|null|boolean|int|float|string $enumerator
96106
* @return void
97-
* @throws InvalidArgumentException On an invalid given enum
107+
* @throws InvalidArgumentException On an invalid given enumerator
98108
*/
99-
public function detach($enum)
109+
public function detach($enumerator)
100110
{
101-
$enumClass = $this->enumClass;
102-
$this->bitset &= ~(1 << $enumClass::get($enum)->getOrdinal());
111+
$enumeration = $this->enumeration;
112+
$this->bitset &= ~(1 << $enumeration::get($enumerator)->getOrdinal());
103113
}
104114

105115
/**
106-
* Test if the given enumeration exists
107-
* @param Enum|null|boolean|int|float|string $enum
116+
* Test if the given enumerator was attached
117+
* @param Enum|null|boolean|int|float|string $enumerator
108118
* @return boolean
109119
*/
110-
public function contains($enum)
120+
public function contains($enumerator)
111121
{
112-
$enumClass = $this->enumClass;
113-
return (bool)($this->bitset & (1 << $enumClass::get($enum)->getOrdinal()));
122+
$enumeration = $this->enumeration;
123+
return (bool)($this->bitset & (1 << $enumeration::get($enumerator)->getOrdinal()));
114124
}
115125

116126
/* Iterator */
117127

118128
/**
119-
* Get current Enum
120-
* @return Enum|null Returns current Enum or NULL on an invalid iterator position
129+
* Get the current enumerator
130+
* @return Enum|null Returns the current enumerator or NULL on an invalid iterator position
121131
*/
122132
public function current()
123133
{
124134
if ($this->valid()) {
125-
$enumClass = $this->enumClass;
126-
return $enumClass::getByOrdinal($this->ordinal);
135+
$enumeration = $this->enumeration;
136+
return $enumeration::getByOrdinal($this->ordinal);
127137
}
128138

129139
return null;
130140
}
131141

132142
/**
133-
* Get ordinal number of current iterator position
143+
* Get the ordinal number of the current iterator position
134144
* @return int
135145
*/
136146
public function key()

tests/MabeEnumTest/EnumMapTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class EnumMapTest extends TestCase
2121
public function testBasic()
2222
{
2323
$enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumBasic');
24-
$this->assertSame('MabeEnumTest\TestAsset\EnumBasic', $enumMap->getEnumClass());
24+
$this->assertSame('MabeEnumTest\TestAsset\EnumBasic', $enumMap->getEnumeration());
2525

2626
$enum1 = EnumBasic::ONE();
2727
$value1 = 'value1';
@@ -48,6 +48,12 @@ public function testBasic()
4848
$this->assertFalse($enumMap->contains($enum2));
4949
}
5050

51+
public function testDeprecatedGetEnumClass()
52+
{
53+
$enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumBasic');
54+
$this->assertSame('MabeEnumTest\TestAsset\EnumBasic', $enumMap->getEnumClass());
55+
}
56+
5157
public function testBasicWithConstantValuesAsEnums()
5258
{
5359
$enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumBasic');

0 commit comments

Comments
 (0)