TableValue::encode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ButterAMQP\Value;
4
5
use ButterAMQP\Buffer;
6
7
class TableValue extends AbstractValue
8
{
9
    /**
10
     * @param array $value
11
     *
12
     * @return string
13
     */
14 21
    public static function encode($value)
15
    {
16 21
        $data = '';
17
18 21
        foreach ($value as $key => $element) {
19 21
            $data .= ShortStringValue::encode($key).TypifiedValue::encode($element);
20 21
        }
21
22 21
        return LongValue::encode(strlen($data)).$data;
23
    }
24
25
    /**
26
     * @param Buffer $data
27
     *
28
     * @return array
29
     */
30 21 View Code Duplication
    public static function decode(Buffer $data)
31
    {
32 21
        $length = LongValue::decode($data);
33 21
        $buffer = new Buffer($data->read($length));
34 21
        $elements = [];
35
36 21
        while (!$buffer->eof()) {
37 21
            $elements[ShortStringValue::decode($buffer)] = TypifiedValue::decode($buffer);
38 21
        }
39
40 21
        return $elements;
41
    }
42
}
43