Varint::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
namespace Cassandra\Type;
3
4
class Varint extends Bigint{
5
    /**
6
     * @param int|string $value
7
     * @throws Exception
8
     */
9
    public function __construct($value = null){
10
        if ($value === null)
11
            return;
12
    
13
        if (!is_numeric($value))
14
            throw new Exception('Incoming value must be type of int.');
15
    
16
        $this->_value = (int) $value;
17
    }
18
    
19
    public static function binary($value){
20
        $higher = ($value & 0xffffffff00000000) >>32;
21
        $lower = $value & 0x00000000ffffffff;
22
        return pack('NN', $higher, $lower);
23
    }
24
    
25
    /**
26
     * @return int
27
     */
28
    public static function parse($binary){
29
        $value = 0;
30
        $length = strlen($binary);
31
        foreach (unpack('C*', $binary) as $i => $byte)
32
            $value |= $byte << (($length - $i) * 8);
33
        $shift = (\PHP_INT_SIZE - $length) * 8;
34
        return $value << $shift >> $shift;
35
    }
36
}
37