ByteString::readBits()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Buffertools\Types;
6
7
use BitWasp\Buffertools\Buffer;
8
use BitWasp\Buffertools\BufferInterface;
9
use BitWasp\Buffertools\ByteOrder;
10
use BitWasp\Buffertools\Parser;
11
12
class ByteString extends AbstractType
13
{
14
    /**
15
     * @var int|string
16
     */
17
    private $length;
18
19
    /**
20
     * @param int           $length
21
     * @param int           $byteOrder
22
     */
23 14
    public function __construct(int $length, int $byteOrder = ByteOrder::BE)
24
    {
25 14
        $this->length = $length;
26 14
        parent::__construct($byteOrder);
27 14
    }
28
29
    /**
30
     * @param BufferInterface $string
31
     * @return string
32
     */
33 12
    public function writeBits(BufferInterface $string): string
34
    {
35 12
        $bits = str_pad(
36 12
            gmp_strval(gmp_init($string->getHex(), 16), 2),
37 12
            $this->length * 8,
38 12
            '0',
39 12
            STR_PAD_LEFT
40
        );
41
42 12
        return $bits;
43
    }
44
45
    /**
46
     * @param Buffer $string
47
     * @return string
48
     * @throws \Exception
49
     */
50 12
    public function write($string): string
51
    {
52 12
        if (!($string instanceof Buffer)) {
53
            throw new \InvalidArgumentException('FixedLengthString::write() must be passed a Buffer');
54
        }
55
56 12
        $bits = $this->isBigEndian()
57 6
            ? $this->writeBits($string)
58 12
            : $this->flipBits($this->writeBits($string));
59
60 12
        $hex = str_pad(
61 12
            gmp_strval(gmp_init($bits, 2), 16),
62 12
            $this->length * 2,
63 12
            '0',
64 12
            STR_PAD_LEFT
65
        );
66
67 12
        return pack("H*", $hex);
68
    }
69
70
    /**
71
     * @param BufferInterface $buffer
72
     * @return string
73
     */
74 14
    public function readBits(BufferInterface $buffer): string
75
    {
76 14
        return str_pad(
77 14
            gmp_strval(gmp_init($buffer->getHex(), 16), 2),
78 14
            $this->length * 8,
79 14
            '0',
80 14
            STR_PAD_LEFT
81
        );
82
    }
83
84
    /**
85
     * @param Parser $parser
86
     * @return BufferInterface
87
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
88
     */
89 14
    public function read(Parser $parser): BufferInterface
90
    {
91 14
        $bits = $this->readBits($parser->readBytes($this->length));
92 14
        if (!$this->isBigEndian()) {
93 8
            $bits = $this->flipBits($bits);
94
        }
95
96 14
        return Buffer::hex(
97 14
            str_pad(
98 14
                gmp_strval(gmp_init($bits, 2), 16),
99 14
                $this->length * 2,
100 14
                '0',
101 14
                STR_PAD_LEFT
102
            ),
103 14
            $this->length
104
        );
105
    }
106
}
107