Short::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
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 Short 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
28
        $data = $this->endianMode->applyEndianess($bytes);
29
30
        return [
31
            $this->mergeBytes($data),
32
        ];
33
    }
34
35
    /**
36
     * @param int $length
37
     *
38
     * @throws EndOfFileReachedException
39
     *
40
     * @return array
41
     */
42
    public function readArray(int $length): array
43
    {
44
        $buffer = [];
45
46
        for ($iterator = 0; $iterator < $length; ++$iterator) {
47
            $bytes = [];
48
49
            $this->assertNotEndOfFile();
50
            $bytes[] = $this->getByte($this->offset++);
51
            $this->assertNotEndOfFile();
52
            $bytes[] = $this->getByte($this->offset++);
53
54
            $data = $this->endianMode->applyEndianess($bytes);
55
56
            $buffer[] = $this->mergeBytes($data);
57
        }
58
59
        return $buffer;
60
    }
61
62
    /**
63
     * @param array $data
64
     *
65
     * @return int
66
     */
67
    private function mergeBytes(array $data): int
68
    {
69
        return $data[0] << 8 | $data[1];
70
    }
71
72
    /**
73
     * @param int $data
74
     *
75
     * @throws EndOfFileReachedException
76
     */
77
    public function write(int $data): void
78
    {
79
        $bytes = $this->splitBytes($data);
80
81
        $bytes = $this->endianMode->applyEndianess($bytes);
82
83
        $this->assertNotEndOfFile();
84
        $this->setByte($this->offset++, $bytes[0]);
85
86
        $this->assertNotEndOfFile();
87
        $this->setByte($this->offset++, $bytes[1]);
88
    }
89
90
    /**
91
     * @param array $data
92
     *
93
     * @throws EndOfFileReachedException
94
     */
95
    public function writeArray(array $data): void
96
    {
97
        $dataLength = count($data);
98
        $startBytePosition = $this->offset;
99
100
        for ($i = $startBytePosition; $i <= $startBytePosition - 1 + $dataLength; ++$i) {
101
            $bytes = $this->splitBytes($data[$i - $startBytePosition]);
102
103
            $bytes = $this->endianMode->applyEndianess($bytes);
104
105
            $this->assertNotEndOfFile();
106
            $this->setByte($this->offset++, $bytes[0]);
107
108
            $this->assertNotEndOfFile();
109
            $this->setByte($this->offset++, $bytes[1]);
110
        }
111
    }
112
113
    /**
114
     * @param int $data
115
     *
116
     * @return array
117
     */
118
    public function splitBytes(int $data): array
119
    {
120
        $bytes = [];
121
122
        $bytes[] = ($data & 0xff00) >> 8;
123
        $bytes[] = ($data & 0x00ff);
124
125
        return $bytes;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function newContent(): string
132
    {
133
        return $this->content;
134
    }
135
136
    /**
137
     * @return int
138
     */
139
    public function newOffset(): int
140
    {
141
        return $this->offset;
142
    }
143
}
144