Completed
Push — master ( 4a5c62...dba287 )
by Daniel
03:51
created

Integer24Reader::readUnsigned()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
ccs 0
cts 17
cp 0
rs 9.3142
cc 3
eloc 16
nc 3
nop 0
crap 12
1
<?php
2
/**
3
 * This file is part of the stream package
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Stream\Reader;
9
10
use GravityMedia\Stream\Enum\ByteOrder;
11
use GravityMedia\Stream\Exception;
12
13
/**
14
 * 24-bit integer (short) reader
15
 *
16
 * @package GravityMedia\Stream\Reader
17
 */
18
class Integer24Reader extends AbstractIntegerReader
19
{
20
    /**
21
     * Use byte order aware trait
22
     */
23
    use ByteOrderAwareTrait;
24
25
    /**
26
     * Read unsigned 24-bit integer (short) data from the stream
27
     *
28
     * @return int
29
     */
30
    protected function readUnsigned()
31
    {
32
        $data = $this->getStream()->read(3);
33
34
        switch ($this->getByteOrder()) {
35
            case ByteOrder::BIG_ENDIAN:
36
                $format = 'N*';
37
                $data = $data . "\x00";
38
                break;
39
            case ByteOrder::LITTLE_ENDIAN:
40
                $format = 'V*';
41
                $data = "\x00" . $data;
42
                break;
43
            default:
44
                $format = 'L*';
45
                $data = $data . "\x00";
46
        }
47
48
        list(, $value) = unpack($format, $data);
49
        return $value;
50
    }
51
52
    /**
53
     * Read signed 24-bit integer (short) data from the stream
54
     *
55
     * @return int
56
     */
57
    protected function readSigned()
58
    {
59
        $data = $this->getStream()->read(3);
60
61
        if (ByteOrder::MACHINE_ENDIAN !== $this->getByteOrder()
62
            && $this->getMachineByteOrder() !== $this->getByteOrder()
63
        ) {
64
            $data = strrev($data);
65
        }
66
67
        switch ($this->getByteOrder()) {
68
            case ByteOrder::BIG_ENDIAN:
69
                $data = $data . "\x00";
70
                break;
71
            case ByteOrder::LITTLE_ENDIAN:
72
                $data = "\x00" . $data;
73
                break;
74
            default:
75
                $data = $data . "\x00";
76
        }
77
78
79
        list(, $value) = unpack('l*', $data);
80
        return $value;
81
    }
82
}
83