Skip to content

Commit 39502af

Browse files
author
Thomas Kerin
committed
Remove unnecessary functions from Parser
1 parent 89923ed commit 39502af

2 files changed

Lines changed: 7 additions & 64 deletions

File tree

src/Buffertools/Parser.php

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct(BufferInterface $input = null)
3333
if ($input instanceof BufferInterface) {
3434
$this->string = $input->getBinary();
3535
$this->size = $input->getSize();
36+
assert(strlen($this->string) === $this->size);
3637
}
3738
}
3839

@@ -85,64 +86,6 @@ public function readBytes(int $numBytes, bool $flipBytes = false): BufferInterfa
8586
return new Buffer($string, $length);
8687
}
8788

88-
/**
89-
* Write $data as $bytes bytes. Can be flipped if needed.
90-
*
91-
* @param integer $numBytes - number of bytes to write
92-
* @param SerializableInterface|BufferInterface|string $data - buffer, serializable or hex
93-
* @param bool $flipBytes
94-
* @return Parser
95-
*/
96-
public function writeBytes(int $numBytes, $data, bool $flipBytes = false): Parser
97-
{
98-
// Treat $data to ensure it's a buffer, with the correct size
99-
if ($data instanceof SerializableInterface) {
100-
$data = $data->getBuffer();
101-
}
102-
103-
if (is_string($data)) {
104-
// Convert to a buffer
105-
$data = Buffer::hex($data, $numBytes);
106-
} else if (!($data instanceof BufferInterface)) {
107-
throw new \RuntimeException('Invalid data passed to Parser::writeBytes');
108-
}
109-
110-
$this->writeBuffer($numBytes, $data, $flipBytes);
111-
112-
return $this;
113-
}
114-
115-
/**
116-
* Write $data as $bytes bytes. Can be flipped if needed.
117-
*
118-
* @param integer $numBytes
119-
* @param string $data
120-
* @param bool $flipBytes
121-
* @return Parser
122-
*/
123-
public function writeRawBinary(int $numBytes, string $data, bool $flipBytes = false): Parser
124-
{
125-
return $this->writeBuffer($numBytes, new Buffer($data, $numBytes), $flipBytes);
126-
}
127-
128-
/**
129-
* @param BufferInterface $buffer
130-
* @param bool $flipBytes
131-
* @param int $numBytes
132-
* @return Parser
133-
*/
134-
public function writeBuffer(int $numBytes, BufferInterface $buffer, bool $flipBytes = false): Parser
135-
{
136-
// only create a new buffer if the size does not match
137-
if ($buffer->getSize() != $numBytes) {
138-
$buffer = new Buffer($buffer->getBinary(), $numBytes);
139-
}
140-
141-
$this->appendBuffer($buffer, $flipBytes);
142-
143-
return $this;
144-
}
145-
14689
/**
14790
* @param BufferInterface $buffer
14891
* @param bool $flipBytes
@@ -184,7 +127,7 @@ public function writeArray(array $serializable): Parser
184127
}
185128

186129
if ($object instanceof BufferInterface) {
187-
$parser->writeBytes($object->getSize(), $object);
130+
$parser->appendBinary($object->getBinary());
188131
} else {
189132
throw new \RuntimeException('Input to writeArray must be Buffer[], or SerializableInterface[]');
190133
}
@@ -203,6 +146,6 @@ public function writeArray(array $serializable): Parser
203146
*/
204147
public function getBuffer(): BufferInterface
205148
{
206-
return new Buffer($this->string, null);
149+
return new Buffer($this->string, $this->size);
207150
}
208151
}

tests/ParserTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,30 @@ public function testWriteBytes()
4242
$bytes = Buffer::hex('41424344');
4343

4444
$parser = new Parser();
45-
$parser->writeBytes(4, $bytes);
45+
$parser->appendBuffer($bytes);
4646
$this->assertTrue($parser->getBuffer()->equals($bytes));
4747
}
4848

4949
public function testWriteBytesFlip()
5050
{
5151
$bytes = Buffer::hex('41424344');
5252
$parser = new Parser();
53-
$parser->writeBytes(4, $bytes, true);
53+
$parser->appendBuffer($bytes, true);
5454

5555
$this->assertEquals('44434241', $parser->getBuffer()->getHex());
5656
}
5757

5858
public function testWriteBytesPadded()
5959
{
6060
$parser = new Parser();
61-
$parser->writeBytes(4, Buffer::hex('34'));
61+
$parser->appendBuffer(Buffer::hex('34'));
6262
$this->assertEquals("00000034", $parser->getBuffer()->getHex());
6363
}
6464

6565
public function testWriteBytesFlipPadded()
6666
{
6767
$parser = new Parser();
68-
$parser->writeBytes(4, Buffer::hex('34'), true);
68+
$parser->appendBuffer(Buffer::hex('34', 4), true);
6969
$this->assertEquals("34000000", $parser->getBuffer()->getHex());
7070
}
7171

0 commit comments

Comments
 (0)