Skip to content

Commit

Permalink
Make countable
Browse files Browse the repository at this point in the history
  • Loading branch information
RobQuistNL committed Apr 23, 2023
1 parent 497dc4f commit 08e878e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion 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 implements \Stringable //@todo might implement ArrayAccess or Traversable
class ByteArray implements \Stringable, \Countable //@todo might implement ArrayAccess or Traversable
{
private $array = [];

Expand Down Expand Up @@ -89,4 +89,9 @@ private static function assertValidByte($value): void
);
}
}

public function count(): int
{
return count($this->array);
}
}
18 changes: 18 additions & 0 deletions tests/ByteArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,22 @@ public static function provideAppend() : array
[ByteArray::fromString('🚀'), ByteArray::fromString('🐸'), ByteArray::fromString('🚀🐸')],
];
}

/**
* @dataProvider provideCount
*/
public function testCount(ByteArray $array, int $expectedCount) : void
{
$this->assertSame($expectedCount, $array->count());
$this->assertSame($expectedCount, count($array));
}

public static function provideCount() : array
{
return [
[ByteArray::fromArray([]), 0],
[ByteArray::fromArray([123]), 1],
[ByteArray::fromString('🚀'), 4],
];
}
}

0 comments on commit 08e878e

Please sign in to comment.