Integer::writeArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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