CollectionMap   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 10
Bugs 5 Features 2
Metric Value
wmc 8
c 10
b 5
f 2
lcom 0
cbo 3
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A binary() 0 17 4
A parse() 0 3 1
1
<?php
2
namespace Cassandra\Type;
3
4
class CollectionMap 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
        if ($value === null)
14
            return;
15
    
16
        if (!is_array($value))
17
            throw new Exception('Incoming value must be type of array.');
18
        
19
        $this->_value = $value;
20
    }
21
    
22
    /**
23
     * 
24
     * @param array $value
25
     * @param array $definition [$keyType, $valueType]
26
     * @return string
27
     */
28
    public static function binary($value, array $definition){
29
        list($keyType, $valueType) = $definition;
30
        $binary = pack('N', count($value));
31
        foreach($value as $key => $val) {
32
            $keyPacked = $key instanceof Base
33
                ? $key->getBinary()
34
                : Base::getBinaryByType($keyType, $key);
35
            
36
            $valuePacked = $val instanceof Base
37
                ? $val->getBinary()
38
                : Base::getBinaryByType($valueType, $val);
39
            
40
            $binary .= pack('N', strlen($keyPacked)) . $keyPacked;
41
            $binary .= pack('N', strlen($valuePacked)) . $valuePacked;
42
        }
43
        return $binary;
44
    }
45
    
46
    public static function parse($binary, array $definition){
47
        return (new \Cassandra\Response\StreamReader($binary))->readMap($definition);
48
    }
49
}
50