Skip to content

Commit 01f393b

Browse files
author
Thomas Kerin
committed
Require Buffer for Parser constructor
1 parent 5c2c8c0 commit 01f393b

3 files changed

Lines changed: 24 additions & 33 deletions

File tree

src/Buffertools/Parser.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,19 @@ class Parser
2626
/**
2727
* Instantiate class, optionally taking Buffer or HEX.
2828
*
29-
* @param null|string|BufferInterface $input
29+
* @param BufferInterface $input
3030
*/
31-
public function __construct($input = null)
31+
public function __construct(BufferInterface $input = null)
3232
{
33-
if (null === $input) {
34-
$input = '';
35-
}
33+
$this->position = 0;
3634

37-
if (is_string($input)) {
38-
$bin = Buffer::hex($input, null)->getBinary();
39-
} elseif ($input instanceof BufferInterface) {
40-
$bin = $input->getBinary();
35+
if (null === $input) {
36+
$this->string = '';
37+
$this->size = 0;
4138
} else {
42-
throw new \InvalidArgumentException("Invalid argument to Parser");
39+
$this->string = $input->getBinary();
40+
$this->size = $input->getSize();
4341
}
44-
45-
$this->string = $bin;
46-
$this->position = 0;
47-
$this->size = strlen($this->string);
4842
}
4943

5044
/**

tests/ParserTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ public function testGetBufferEmptyNull()
3939

4040
public function testWriteBytes()
4141
{
42-
$bytes = '41424344';
42+
$bytes = Buffer::hex('41424344');
43+
4344
$parser = new Parser();
44-
$parser->writeBytes(4, Buffer::hex($bytes));
45-
$returned = $parser->getBuffer()->getHex();
46-
$this->assertSame($returned, '41424344');
45+
$parser->writeBytes(4, $bytes);
46+
$this->assertTrue($parser->getBuffer()->equals($bytes));
4747
}
4848

4949
public function testWriteBytesFlip()
5050
{
51-
$bytes = '41424344';
51+
$bytes = Buffer::hex('41424344');
5252
$parser = new Parser();
53-
$parser->writeBytes(4, Buffer::hex($bytes), true);
54-
$returned = $parser->getBuffer()->getHex();
55-
$this->assertSame($returned, '44434241');
53+
$parser->writeBytes(4, $bytes, true);
54+
55+
$this->assertEquals('44434241', $parser->getBuffer()->getHex());
5656
}
5757

5858
public function testWriteBytesPadded()
@@ -71,26 +71,24 @@ public function testWriteBytesFlipPadded()
7171

7272
public function testReadBytes()
7373
{
74-
$bytes = '41424344';
74+
$bytes = Buffer::hex('41424344');
7575

7676
$parser = new Parser($bytes);
7777
$read = $parser->readBytes(4);
7878
$this->assertInstanceOf(Buffer::class, $read);
7979

80-
$hex = $read->getHex();
81-
$this->assertSame($bytes, $hex);
80+
$this->assertTrue($read->equals($bytes));
8281
}
8382

8483
public function testReadBytesFlip()
8584
{
86-
$bytes = '41424344';
85+
$bytes = Buffer::hex('41424344');
8786

8887
$parser = new Parser($bytes);
8988
$read = $parser->readBytes(4, true);
9089
$this->assertInstanceOf(Buffer::class, $read);
9190

92-
$hex = $read->getHex();
93-
$this->assertSame('44434241', $hex);
91+
$this->assertEquals($bytes->flip()->getHex(), $read->getHex());
9492
}
9593

9694
/**
@@ -103,16 +101,15 @@ public function testReadBytesEmpty()
103101
// and length is zero.
104102

105103
$parser = new Parser();
106-
$data = $parser->readBytes(0);
107-
$this->assertFalse(!!$data);
104+
$parser->readBytes(0);
108105
}
109106
/**
110107
* @expectedException \BitWasp\Buffertools\Exceptions\ParserOutOfRange
111108
* @expectedExceptionMessage Could not parse string of required length (empty)
112109
*/
113110
public function testReadBytesEndOfString()
114111
{
115-
$parser = new Parser('4041414142414141');
112+
$parser = new Parser(Buffer::hex('4041414142414141'));
116113
$bytes1 = $parser->readBytes(4);
117114
$bytes2 = $parser->readBytes(4);
118115
$this->assertSame($bytes1->getHex(), '40414141');
@@ -125,7 +122,7 @@ public function testReadBytesEndOfString()
125122
*/
126123
public function testReadBytesBeyondLength()
127124
{
128-
$bytes = '41424344';
125+
$bytes = Buffer::hex('41424344');
129126
$parser = new Parser($bytes);
130127
$parser->readBytes(5);
131128
}

tests/TemplateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testTemplate()
2929
public function testTemplateEmptyParse()
3030
{
3131
$template = new Template();
32-
$parser = new Parser('010203040a0b0c0d');
32+
$parser = new Parser(Buffer::hex('010203040a0b0c0d'));
3333
$template->parse($parser);
3434
}
3535

0 commit comments

Comments
 (0)