Byte::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PBurggraf\BinaryUtilities\DataType;
6
7
use PBurggraf\BinaryUtilities\Exception\EndOfFileReachedException;
8
9
/**
10
 * @author Philip Burggraf <[email protected]>
11
 */
12
class Byte extends AbstractDataType
13
{
14
    /**
15
     * @throws EndOfFileReachedException
16
     *
17
     * @return array
18
     */
19
    public function read(): array
20
    {
21
        $this->assertNotEndOfFile();
22
23
        return [$this->getByte($this->offset++)];
24
    }
25
26
    /**
27
     * @param int $length
28
     *
29
     * @throws EndOfFileReachedException
30
     *
31
     * @return array
32
     */
33
    public function readArray(int $length): array
34
    {
35
        $buffer = [];
36
37
        for ($iterator = 0; $iterator < $length; ++$iterator) {
38
            $this->assertNotEndOfFile();
39
40
            $buffer[] = $this->getByte($this->offset++);
41
        }
42
43
        return $buffer;
44
    }
45
46
    /**
47
     * @param int $data
48
     *
49
     * @throws EndOfFileReachedException
50
     */
51
    public function write(int $data): void
52
    {
53
        $this->assertNotEndOfFile();
54
        $this->setByte($this->offset++, $data);
55
    }
56
57
    /**
58
     * @param array $data
59
     *
60
     * @throws EndOfFileReachedException
61
     */
62
    public function writeArray(array $data): void
63
    {
64
        $dataLength = count($data);
65
        $startBytePosition = $this->offset;
66
67
        for ($i = $startBytePosition; $i <= $startBytePosition - 1 + $dataLength; ++$i) {
68
            $this->assertNotEndOfFile();
69
            $this->setByte($this->offset++, $data[$i - $startBytePosition]);
70
        }
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function newContent(): string
77
    {
78
        return $this->content;
79
    }
80
81
    /**
82
     * @return int
83
     */
84
    public function newOffset(): int
85
    {
86
        return $this->offset;
87
    }
88
}
89