Tuple::binary()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 11
nc 4
nop 2
1
<?php
2
namespace Cassandra\Type;
3
4
class Tuple extends Base{
5
6
    /**
7
     * @param array $value
8
     * @param array $definition
9
     * @throws Exception
10
     */
11
    public function __construct($value, array $definition){
12
        $this->_definition = $definition;
13
        
14
        if ($value === null)
15
            return;
16
        
17
        if (!is_array($value))
18
            throw new Exception('Incoming value must be type of array.');
19
20
        $this->_value = $value;
21
    }
22
23
    public static function binary($value, array $definition){
24
        $binary = '';
25
        foreach ($definition as $key => $type) {
26
            if ($value[$key] === null) {
27
                $binary .= "\xff\xff\xff\xff";
28
            }
29
            else {
30
                $valueBinary = $value[$key] instanceof Base
31
                    ? $value[$key]->getBinary()
32
                    : Base::getBinaryByType($type, $value[$key]);
33
                
34
                $binary .= pack('N', strlen($valueBinary)) . $valueBinary;
35
            }
36
        }
37
        
38
        return $binary;
39
    }
40
    
41
    /**
42
     * 
43
     * @param string $binary
44
     * @param array $definition
45
     * @return array
46
     */
47
    public static function parse($binary, array $definition){
48
        return (new \Cassandra\Response\StreamReader($binary))->readTuple($definition);
49
    }
50
}
51