-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathString.php
More file actions
44 lines (36 loc) · 1.17 KB
/
Copy pathString.php
File metadata and controls
44 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace PhpBinaryReader\Type;
use PhpBinaryReader\BinaryReader;
use PhpBinaryReader\Exception\InvalidDataException;
class String implements TypeInterface
{
/**
* @param \PhpBinaryReader\BinaryReader $br
* @param int $length
* @return string
* @throws \OutOfBoundsException
* @throws InvalidDataException
*/
public function read(BinaryReader &$br, $length)
{
if (!is_int($length)) {
throw new InvalidDataException('The length parameter must be an integer');
}
if (($length + $br->getPosition()) > $br->getEofPosition()) {
throw new \OutOfBoundsException('Cannot read string, it exceeds the boundary of the file');
}
$str = substr($br->getInputString(), $br->getPosition(), $length);
$br->setPosition($br->getPosition() + $length);
return $str;
}
/**
* @param \PhpBinaryReader\BinaryReader $br
* @param int $length
* @return string
*/
public function readAligned(BinaryReader &$br, $length)
{
$br->align();
return $this->read($br, $length);
}
}