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

Integer24Reader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 65
ccs 0
cts 36
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A readUnsigned() 0 21 3
B readSigned() 0 25 5
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