Skip to content

Commit 86417b6

Browse files
authored
Merge pull request #63 from marc-mabe/little_bit_endian_bitset
EnumSet: support for Little- and Big-Endian binary bitsets
2 parents bedfbc1 + 7ddb0bf commit 86417b6

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
@@ -200,30 +200,30 @@ public function count()
200200
}
201201

202202
/**
203-
* Get the binary bitset
203+
* Get binary bitset in little-endian order
204204
*
205-
* @return string Returns the binary bitset in big-endian order
205+
* @return string
206206
*/
207-
public function getBitset()
207+
public function getBinaryBitsetLe()
208208
{
209-
return strrev($this->bitset);
209+
return $this->bitset;
210210
}
211211

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

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

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

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

tests/MabeEnumTest/EnumSetTest.php

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

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

267338
$this->assertNull($enumSet->attach($enum2));
268-
$this->assertSame('000000000000000003', \bin2hex($enumSet->getBitset()));
339+
$this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x03", $enumSet->getBitset());
269340
$this->assertTrue($enumSet->contains($enum2));
270341

271342
$this->assertNull($enumSet->attach($enum3));
@@ -285,6 +356,33 @@ public function testGetBitset()
285356
$this->assertSame(3, $enumSet->count());
286357
}
287358

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

300-
public function testSetBitsetShort()
398+
public function testSetBinaryBitsetLeShort()
301399
{
302400
$enumSet = new EnumSet('MabeEnumTest\TestAsset\Enum65');
303-
$enumSet->setBitset("\x0A");
304-
$this->assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x0A", $enumSet->getBitset());
401+
$enumSet->setBinaryBitsetLe("\x0A");
402+
$this->assertSame("\x0A\x00\x00\x00\x00\x00\x00\x00\x00", $enumSet->getBinaryBitsetLe());
305403
}
306404

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

314-
public function testFalseBitsetArgumentExceptionIfNotString()
420+
public function testSetBinaryBitsetBeArgumentExceptionIfNotString()
315421
{
316422
$this->setExpectedException('InvalidArgumentException');
317423

318424
$enum = new EnumSet('MabeEnumTest\TestAsset\Enum65');
319-
$enum->setBitset(0);
425+
$enum->setBinaryBitsetBe(0);
320426
}
321427

322428
public function testCountingEmptyEnumEmptySet()

0 commit comments

Comments
 (0)