Skip to content

Commit 0324e84

Browse files
committed
Add fromConcat and toArray methods
1 parent 796d535 commit 0324e84

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

src/BitArray/BitArray.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,13 @@ public function count()
244244
}
245245

246246
/**
247-
* Serialize the object
247+
* Transform the object to an array
248248
*
249249
* @return array Array of values
250250
*
251-
* @since 1.0.0
251+
* @since 1.1.0
252252
*/
253-
public function jsonSerialize()
253+
public function toArray()
254254
{
255255
$array = [];
256256

@@ -262,6 +262,18 @@ public function jsonSerialize()
262262
return $array;
263263
}
264264

265+
/**
266+
* Serialize the object
267+
*
268+
* @return array Array of values
269+
*
270+
* @since 1.0.0
271+
*/
272+
public function jsonSerialize()
273+
{
274+
return $this->toArray();
275+
}
276+
265277
/**
266278
* Get an iterator
267279
*
@@ -402,7 +414,7 @@ public static function fromJson($json)
402414
*
403415
* @return BitArray A new BitArray
404416
*
405-
* @since 1.0.0
417+
* @since 1.1.0
406418
*/
407419
public static function fromSlice(BitArray $bits, $offset = 0, $size = null)
408420
{
@@ -456,6 +468,34 @@ public static function fromSlice(BitArray $bits, $offset = 0, $size = null)
456468
return $slice;
457469
}
458470

471+
/**
472+
* Create a new BitArray using the concat operation
473+
*
474+
* @param BitArray $bits1 A bitarray
475+
* @param BitArray $bits2 A bitarray
476+
*
477+
* @return BitArray A new BitArray
478+
*
479+
* @since 1.1.0
480+
*/
481+
public static function fromConcat(BitArray $bits1, BitArray $bits2)
482+
{
483+
$size = $bits1->size + $bits2->size;
484+
$concat = new BitArray($size);
485+
486+
for ($i = 0; $i < $bits1->size; $i++)
487+
{
488+
$concat[$i] = $bits1[$i];
489+
}
490+
491+
for ($i = 0; $i < $bits2->size; $i++)
492+
{
493+
$concat[$i + $bits1->size] = $bits2[$i];
494+
}
495+
496+
return $concat;
497+
}
498+
459499
/**
460500
* Complement the bit array
461501
*

tests/BitArray/BitArrayTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ public function test___get()
292292
* @return void
293293
*
294294
* @covers chdemko\BitArray\BitArray::jsonSerialize
295+
* @covers chdemko\BitArray\BitArray::toArray
295296
*
296297
* @since 1.0.0
297298
*/
@@ -467,7 +468,7 @@ public function test_fromString($string)
467468
* @covers chdemko\BitArray\BitArray::__construct
468469
* @covers chdemko\BitArray\BitArray::__toString
469470
*
470-
* @since 1.0.0
471+
* @since 1.1.0
471472
*/
472473
public function test_fromJson()
473474
{
@@ -484,7 +485,7 @@ public function test_fromJson()
484485
*
485486
* @return array
486487
*
487-
* @since 1.0.0
488+
* @since 1.1.0
488489
*/
489490
public function cases_fromSlice()
490491
{
@@ -580,6 +581,32 @@ public function test_fromSlice($string, $offset, $size, $result)
580581
);
581582
}
582583

584+
/**
585+
* Tests BitArray::fromConcat
586+
*
587+
* @return void
588+
*
589+
* @covers chdemko\BitArray\BitArray::fromConcat
590+
* @covers chdemko\BitArray\BitArray::__construct
591+
* @covers chdemko\BitArray\BitArray::__toString
592+
*
593+
* @since 1.1.0
594+
*/
595+
public function test_fromConcat()
596+
{
597+
$bits1 = BitArray::fromString('1001');
598+
$bits2 = BitArray::fromString('111');
599+
600+
$this->assertEquals(
601+
'1001111',
602+
(string) BitArray::fromConcat($bits1, $bits2)
603+
);
604+
$this->assertEquals(
605+
'1111001',
606+
(string) BitArray::fromConcat($bits2, $bits1)
607+
);
608+
}
609+
583610
/**
584611
* Tests BitArray::applyComplement
585612
*

0 commit comments

Comments
 (0)