| @@ 8-35 (lines=28) @@ | ||
| 5 | use PhpBinaryReader\BinaryReader; |
|
| 6 | use PhpBinaryReader\Exception\InvalidDataException; |
|
| 7 | ||
| 8 | class Byte implements TypeInterface |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * Returns an variable number of bytes |
|
| 12 | * |
|
| 13 | * @param \PhpBinaryReader\BinaryReader $br |
|
| 14 | * @param int|null $length |
|
| 15 | * @return string |
|
| 16 | * @throws \OutOfBoundsException |
|
| 17 | * @throws InvalidDataException |
|
| 18 | */ |
|
| 19 | public function read(BinaryReader &$br, $length = null) |
|
| 20 | { |
|
| 21 | if (!is_int($length)) { |
|
| 22 | throw new InvalidDataException('The length parameter must be an integer'); |
|
| 23 | } |
|
| 24 | ||
| 25 | $br->align(); |
|
| 26 | ||
| 27 | if (!$br->canReadBytes($length)) { |
|
| 28 | throw new \OutOfBoundsException('Cannot read bytes, it exceeds the boundary of the file'); |
|
| 29 | } |
|
| 30 | ||
| 31 | $segment = $br->readFromHandle($length); |
|
| 32 | ||
| 33 | return $segment; |
|
| 34 | } |
|
| 35 | } |
|
| 36 | ||
| @@ 8-43 (lines=36) @@ | ||
| 5 | use PhpBinaryReader\BinaryReader; |
|
| 6 | use PhpBinaryReader\Exception\InvalidDataException; |
|
| 7 | ||
| 8 | class Str implements TypeInterface |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * @param \PhpBinaryReader\BinaryReader $br |
|
| 12 | * @param int $length |
|
| 13 | * @return string |
|
| 14 | * @throws \OutOfBoundsException |
|
| 15 | * @throws InvalidDataException |
|
| 16 | */ |
|
| 17 | public function read(BinaryReader &$br, $length) |
|
| 18 | { |
|
| 19 | if (!is_int($length)) { |
|
| 20 | throw new InvalidDataException('The length parameter must be an integer'); |
|
| 21 | } |
|
| 22 | ||
| 23 | if (!$br->canReadBytes($length)) { |
|
| 24 | throw new \OutOfBoundsException('Cannot read string, it exceeds the boundary of the file'); |
|
| 25 | } |
|
| 26 | ||
| 27 | $str = $br->readFromHandle($length); |
|
| 28 | ||
| 29 | return $str; |
|
| 30 | } |
|
| 31 | ||
| 32 | /** |
|
| 33 | * @param \PhpBinaryReader\BinaryReader $br |
|
| 34 | * @param int $length |
|
| 35 | * @return string |
|
| 36 | */ |
|
| 37 | public function readAligned(BinaryReader &$br, $length) |
|
| 38 | { |
|
| 39 | $br->align(); |
|
| 40 | ||
| 41 | return $this->read($br, $length); |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||