Completed
Push — master ( ba315b...f972dd )
by Philip
02:39
created

Integer::newOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PBurggraf\BinaryUtilities\DataType;
4
5
use PBurggraf\BinaryUtilities\Exception\EndOfFileReachedException;
6
7
/**
8
 * @author Philip Burggraf <[email protected]>
9
 */
10
class Integer extends AbstractDataType
11
{
12
    /**
13
     * @throws EndOfFileReachedException
14
     *
15
     * @return array
16
     */
17
    public function read(): array
18
    {
19
        $bytes = [];
20
21
        $this->assertNotEndOfFile();
22
        $bytes[] = $this->getByte($this->offset++);
23
        $this->assertNotEndOfFile();
24
        $bytes[] = $this->getByte($this->offset++);
25
        $this->assertNotEndOfFile();
26
        $bytes[] = $this->getByte($this->offset++);
27
        $this->assertNotEndOfFile();
28
        $bytes[] = $this->getByte($this->offset++);
29
30
        $data = $this->endianMode->applyEndianess($bytes);
31
32
        return [
33
            $this->mergeBytes($data),
34
        ];
35
    }
36
37
    /**
38
     * @param int $length
39
     *
40
     * @throws EndOfFileReachedException
41
     *
42
     * @return array
43
     */
44
    public function readArray(int $length): array
45
    {
46
        $buffer = [];
47
48
        for ($iterator = 0; $iterator < $length; ++$iterator) {
49
            $bytes = [];
50
51
            $this->assertNotEndOfFile();
52
            $bytes[] = $this->getByte($this->offset++);
53
            $this->assertNotEndOfFile();
54
            $bytes[] = $this->getByte($this->offset++);
55
            $this->assertNotEndOfFile();
56
            $bytes[] = $this->getByte($this->offset++);
57
            $this->assertNotEndOfFile();
58
            $bytes[] = $this->getByte($this->offset++);
59
60
            $data = $this->endianMode->applyEndianess($bytes);
61
62
            $buffer[] = $this->mergeBytes($data);
63
        }
64
65
        return $buffer;
66
    }
67
68
    /**
69
     * @param array $data
70
     *
71
     * @return int
72
     */
73
    private function mergeBytes(array $data): int
74
    {
75
        return $data[0] << 24 | $data[1] << 16 | $data[2] << 8 | $data[3];
76
    }
77
78
    /**
79
     * @param int $data
80
     *
81
     * @throws EndOfFileReachedException
82
     */
83
    public function write(int $data): void
84
    {
85
        $bytes = $this->splitBytes($data);
86
87
        $bytes = $this->endianMode->applyEndianess($bytes);
88
89
        $this->assertNotEndOfFile();
90
        $this->setByte($this->offset++, $bytes[0]);
91
92
        $this->assertNotEndOfFile();
93
        $this->setByte($this->offset++, $bytes[1]);
94
95
        $this->assertNotEndOfFile();
96
        $this->setByte($this->offset++, $bytes[2]);
97
98
        $this->assertNotEndOfFile();
99
        $this->setByte($this->offset++, $bytes[3]);
100
    }
101
102
    /**
103
     * @param array $data
104
     *
105
     * @throws EndOfFileReachedException
106
     */
107
    public function writeArray(array $data): void
108
    {
109
        $dataLength = count($data);
110
        $startBytePosition = $this->offset;
111
112
        for ($i = $this->offset; $i <= $this->offset - 1 + $dataLength; ++$i) {
113
            $bytes = $this->splitBytes($data[$i - $startBytePosition]);
114
115
            $this->assertNotEndOfFile();
116
            $this->setByte($this->offset++, $bytes[0]);
117
118
            $this->assertNotEndOfFile();
119
            $this->setByte($this->offset++, $bytes[1]);
120
121
            $this->assertNotEndOfFile();
122
            $this->setByte($this->offset++, $bytes[2]);
123
124
            $this->assertNotEndOfFile();
125
            $this->setByte($this->offset++, $bytes[3]);
126
127
        }
128
    }
129
130
    /**
131
     * @param int $data
132
     *
133
     * @return array
134
     */
135
    public function splitBytes(int $data): array
136
    {
137
        $bytes = [];
138
139
        $bytes[] = ($data & 0xff000000) >> 24;
140
        $bytes[] = ($data & 0x00ff0000) >> 16;
141
        $bytes[] = ($data & 0x0000ff00) >> 8;
142
        $bytes[] = ($data & 0x000000ff);
143
144
        return $bytes;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function newContent(): string
151
    {
152
        return $this->content;
153
    }
154
155
    /**
156
     * @return int
157
     */
158
    public function newOffset(): int
159
    {
160
        return $this->offset;
161
    }
162
}
163