Skip to content

Commit

Permalink
Adding support for using strings as input and output
Browse files Browse the repository at this point in the history
  • Loading branch information
RobQuistNL committed Apr 22, 2023
1 parent a3b847e commit a10bbae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/ByteArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* A class to hold an array of bytes
*/
class ByteArray //@todo might implement ArrayAccess or Traversable
class ByteArray implements \Stringable //@todo might implement ArrayAccess or Traversable
{
private $array = [];

Expand All @@ -22,11 +22,14 @@ private function __construct()
* Returns a PHP native array with bytes. Internally, these are integers, but they will always be >=0 and <=254
* @return byte[]
*/
public function getArray(): array
public function toArray(): array
{
return $this->array;
}

/**
* Create a ByteArray from an array. Each element has to be a byte as type <int>
*/
public static function fromArray(array $array): self
{
$self = new self();
Expand All @@ -38,6 +41,23 @@ public static function fromArray(array $array): self
return $self;
}

/**
* Create a byte array from string. Assumes every character in the string to be an unsigned char.
*/
public static function fromString(string $string): self
{
$unpacked = unpack('C*', $string);
if ($unpacked === false) {
throw new InvalidByteException('Could not unpack string');
}
return self::fromArray($unpacked);
}

public function __toString(): string
{
return pack("C*", ...$this->array);
}

/**
* @throws InvalidByteException if $value is not a valid byte
*/
Expand Down
27 changes: 26 additions & 1 deletion tests/ByteArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ByteArrayTest extends AbstractTestCase
public function testValidByteArrays(array $array) : void
{
$byteArray = ByteArray::fromArray($array);
$this->assertEquals($array, $byteArray->getArray());
$this->assertEquals($array, $byteArray->toArray());
}

public static function provideValidByteArrays() : array
Expand Down Expand Up @@ -55,4 +55,29 @@ public static function provideInvalidByteArrays() : array
[[0, 1, 2, 3, 0.59438, 28, 40], "0.59438 is not a valid byte (must be <int>)"],
];
}

/**
* @dataProvider provideString
*/
public function testString(string $string, array $array) : void
{
$byteArray = ByteArray::fromString($string);
$this->assertEquals(
$array,
$byteArray->toArray()
);
$this->assertSame($string, (string) $byteArray);
}

public static function provideString() : array
{
return [
['', []], // Empty string = Empty result
[chr(0), [0]], // null byte = 0x00
[str_repeat(chr(13), 4), [13, 13, 13, 13]], // A list of bytes
['Hello World!', [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]], // ASCII characters
['é', [195, 169]], // Check some UTF-8 encoding, should be 2 bytes
['🚀', [240, 159, 154, 128]], // This emoji should be 4 bytes (unicode)
];
}
}

0 comments on commit a10bbae

Please sign in to comment.