Completed
Push — master ( d06bab...02e31a )
by Eugene
03:46
created

PackUtils::getHeaderSize()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 4
nop 1
crap 5
1
<?php
2
3
namespace Tarantool\Client\Packer;
4
5
use Tarantool\Client\Exception\Exception;
6
7
abstract class PackUtils
8
{
9
    private static $offsets = [
10
        // MP_UINT
11
        0xcd => 2,
12
        0xce => 4,
13
        0xcf => 8,
14
15
        // MP_INT
16
        0xd1 => 2,
17
        0xd2 => 4,
18
        0xd3 => 8,
19
    ];
20
21 110
    public static function packLength($length)
22
    {
23 110
        return pack('CN', 0xce, $length);
24
    }
25
26 102
    public static function unpackLength($data)
27
    {
28 102
        if (false === $data = @unpack('C_/Nlength', $data)) {
29 1
            throw new Exception('Unable to unpack length value.');
30
        }
31
32 101
        return $data['length'];
33
    }
34
35 6
    public static function getHeaderSize($buffer)
36
    {
37 6
        $offset = 0;
38 6
        $len = strlen($buffer);
39
40 6
        while ($offset < $len) {
41 6
            $c = ord($buffer[$offset]);
42
43 6
            if (self::isMap($c) && $offset) {
44 3
                break;
45
            }
46
47 6
            $offset += isset(self::$offsets[$c]) ? self::$offsets[$c] + 1 : 1;
48
        }
49
50 6
        return $offset;
51
    }
52
53 6
    private static function isMap($c)
54
    {
55 6
        return 0x80 === ($c & 0xf0) || 0xde === $c || 0xdf === $c;
56
    }
57
}
58