Skip to content

Commit 7ddb0bf

Browse files
committed
EnumSet: support for Little- and Big-Endian binary bitsets
1 parent 7d99150 commit 7ddb0bf

File tree

2 files changed

+176
-17
lines changed

2 files changed

+176
-17
lines changed

src/EnumSet.php

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,30 +199,30 @@ public function count()
199199
}
200200

201201
/**
202-
* Get the binary bitset
202+
* Get binary bitset in little-endian order
203203
*
204-
* @return string Returns the binary bitset in big-endian order
204+
* @return string
205205
*/
206-
public function getBitset()
206+
public function getBinaryBitsetLe()
207207
{
208-
return strrev($this->bitset);
208+
return $this->bitset;
209209
}
210210

211211
/**
212-
* Set the bitset.
212+
* Set binary bitset in little-endian order
213+
*
213214
* NOTE: It resets the current position of the iterator
214215
*
215-
* @param string $bitset The binary bitset in big-endian order
216+
* @param string $bitset
216217
* @return void
217218
* @throws InvalidArgumentException On a non string is given as Parameter
218219
*/
219-
public function setBitset($bitset)
220+
public function setBinaryBitsetLe($bitset)
220221
{
221222
if (!is_string($bitset)) {
222223
throw new InvalidArgumentException('Bitset must be a string');
223224
}
224225

225-
$bitset = strrev($bitset);
226226
$size = ceil($this->ordinalMax / 8);
227227
$sizeIn = strlen($bitset);
228228

@@ -238,6 +238,59 @@ public function setBitset($bitset)
238238
$this->rewind();
239239
}
240240

241+
/**
242+
* Get binary bitset in big-endian order
243+
*
244+
* @return string
245+
*/
246+
public function getBinaryBitsetBe()
247+
{
248+
return strrev($this->bitset);
249+
}
250+
251+
/**
252+
* Set binary bitset in big-endian order
253+
*
254+
* NOTE: It resets the current position of the iterator
255+
*
256+
* @param string $bitset
257+
* @return void
258+
* @throws InvalidArgumentException On a non string is given as Parameter
259+
*/
260+
public function setBinaryBitsetBe($bitset)
261+
{
262+
if (!is_string($bitset)) {
263+
throw new InvalidArgumentException('Bitset must be a string');
264+
}
265+
266+
$this->setBinaryBitsetLe(strrev($bitset));
267+
}
268+
269+
/**
270+
* Get the binary bitset
271+
*
272+
* @return string Returns the binary bitset in big-endian order
273+
* @deprecated Please use getBinaryBitsetBe() instead
274+
*/
275+
public function getBitset()
276+
{
277+
return $this->getBinaryBitsetBe();
278+
}
279+
280+
/**
281+
* Set the bitset.
282+
* NOTE: It resets the current position of the iterator
283+
*
284+
* @param string $bitset The binary bitset in big-endian order
285+
* @return void
286+
* @throws InvalidArgumentException On a non string is given as Parameter
287+
* @deprecated Please use setBinaryBitsetBe() instead
288+
*/
289+
public function setBitset($bitset)
290+
{
291+
$this->setBinaryBitsetBE($bitset);
292+
}
293+
241294
/**
242295
* Get a bit at the given ordinal number
243296
*

tests/MabeEnumTest/EnumSetTest.php

Lines changed: 115 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,77 @@ public function test65EnumerationsSet()
250250
$this->assertTrue($enum->valid());
251251
}
252252

253+
public function testGetBinaryBitsetLe()
254+
{
255+
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
256+
257+
$enum1 = Enum65::ONE;
258+
$enum2 = Enum65::TWO;
259+
$enum3 = Enum65::SIXTYFIVE;
260+
$enum4 = Enum65::SIXTYFOUR;
261+
262+
$this->assertNull($enumSet->attach($enum1));
263+
$this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
264+
$this->assertTrue($enumSet->contains($enum1));
265+
266+
$this->assertNull($enumSet->attach($enum2));
267+
$this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
268+
$this->assertTrue($enumSet->contains($enum2));
269+
270+
$this->assertNull($enumSet->attach($enum3));
271+
$this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetLe());
272+
$this->assertTrue($enumSet->contains($enum3));
273+
274+
$this->assertNull($enumSet->attach($enum4));
275+
$this->assertSame("\x03\x00\x00\x00\x00\x00\x00\x80\x01", $enumSet->getBinaryBitsetLe());
276+
$this->assertTrue($enumSet->contains($enum4));
277+
278+
$this->assertSame(4, $enumSet->count());
279+
280+
$this->assertNull($enumSet->detach($enum2));
281+
$this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x80\x01", $enumSet->getBinaryBitsetLe());
282+
$this->assertFalse($enumSet->contains($enum2));
283+
284+
$this->assertSame(3, $enumSet->count());
285+
}
286+
287+
public function testGetBinaryBitsetBe()
288+
{
289+
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
290+
291+
$enum1 = Enum65::ONE;
292+
$enum2 = Enum65::TWO;
293+
$enum3 = Enum65::SIXTYFIVE;
294+
$enum4 = Enum65::SIXTYFOUR;
295+
296+
$this->assertNull($enumSet->attach($enum1));
297+
$this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetBe());
298+
$this->assertTrue($enumSet->contains($enum1));
299+
300+
$this->assertNull($enumSet->attach($enum2));
301+
$this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe());
302+
$this->assertTrue($enumSet->contains($enum2));
303+
304+
$this->assertNull($enumSet->attach($enum3));
305+
$this->assertSame("\x01\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe());
306+
$this->assertTrue($enumSet->contains($enum3));
307+
308+
$this->assertNull($enumSet->attach($enum4));
309+
$this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBinaryBitsetBe());
310+
$this->assertTrue($enumSet->contains($enum4));
311+
312+
$this->assertSame(4, $enumSet->count());
313+
314+
$this->assertNull($enumSet->detach($enum2));
315+
$this->assertSame("\x01\x80\x00\x00\x00\x00\x00\x00\x01", $enumSet->getBinaryBitsetBe());
316+
$this->assertFalse($enumSet->contains($enum2));
317+
318+
$this->assertSame(3, $enumSet->count());
319+
}
320+
321+
/**
322+
* @deprecated
323+
*/
253324
public function testGetBitset()
254325
{
255326
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
@@ -264,7 +335,7 @@ public function testGetBitset()
264335
$this->assertTrue($enumSet->contains($enum1));
265336

266337
$this->assertNull($enumSet->attach($enum2));
267-
$this->assertSame('000000000000000003', \bin2hex($enumSet->getBitset()));
338+
$this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBitset());
268339
$this->assertTrue($enumSet->contains($enum2));
269340

270341
$this->assertNull($enumSet->attach($enum3));
@@ -284,6 +355,33 @@ public function testGetBitset()
284355
$this->assertSame(3, $enumSet->count());
285356
}
286357

358+
public function testSetBinaryBitsetLe()
359+
{
360+
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
361+
$enumSet->setBinaryBitsetLe("\x01\x00\x00\x00\x00\x00\x00\x80\x01");
362+
363+
$this->assertTrue($enumSet->contains(Enum65::ONE));
364+
$this->assertFalse($enumSet->contains(Enum65::TWO));
365+
$this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE));
366+
$this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR));
367+
$this->assertTrue($enumSet->count() == 3);
368+
}
369+
370+
public function testSetBinaryBitsetBe()
371+
{
372+
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
373+
$enumSet->setBinaryBitsetBe("\x01\x80\x00\x00\x00\x00\x00\x00\x01");
374+
375+
$this->assertTrue($enumSet->contains(Enum65::ONE));
376+
$this->assertFalse($enumSet->contains(Enum65::TWO));
377+
$this->assertTrue($enumSet->contains(Enum65::SIXTYFIVE));
378+
$this->assertTrue($enumSet->contains(Enum65::SIXTYFOUR));
379+
$this->assertTrue($enumSet->count() == 3);
380+
}
381+
382+
/**
383+
* @deprecated
384+
*/
287385
public function testSetBitset()
288386
{
289387
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
@@ -296,25 +394,33 @@ public function testSetBitset()
296394
$this->assertTrue($enumSet->count() == 3);
297395
}
298396

299-
public function testSetBitsetShort()
397+
public function testSetBinaryBitsetLeShort()
300398
{
301399
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
302-
$enumSet->setBitset("\x0A");
303-
$this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x0A", $enumSet->getBitset());
400+
$enumSet->setBinaryBitsetLe("\x0A");
401+
$this->assertSame("\x0A\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
304402
}
305403

306-
public function testSetBitsetLong()
404+
public function testSetBinaryBitsetLeLong()
307405
{
308406
$enumSet = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
309-
$enumSet->setBitset("\xFF\xFF\xFF\xFF\xFF\x0A");
310-
$this->assertSame("\xFF\x0A", $enumSet->getBitset());
407+
$enumSet->setBinaryBitsetLe("\x0A\xFF\xFF\xFF\xFF\xFF");
408+
$this->assertSame("\x0A\xFF", $enumSet->getBinaryBitsetLe());
409+
}
410+
411+
public function testSetBinaryBitsetLeArgumentExceptionIfNotString()
412+
{
413+
$this->setExpectedException('InvalidArgumentException');
414+
415+
$enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');
416+
$enum->setBinaryBitsetLe(0);
311417
}
312418

313-
public function testFalseBitsetArgumentExceptionIfNotString()
419+
public function testSetBinaryBitsetBeArgumentExceptionIfNotString()
314420
{
315421
$this->setExpectedException('InvalidArgumentException');
316422

317423
$enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');
318-
$enum->setBitset(0);
424+
$enum->setBinaryBitsetBe(0);
319425
}
320426
}

0 commit comments

Comments
 (0)