TypifiedValue   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 123
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 14 3
A encode() 0 14 3
C guess() 0 27 11
1
<?php
2
3
namespace ButterAMQP\Value;
4
5
use ButterAMQP\Buffer;
6
use ButterAMQP\Exception\AMQP\NotImplementedException;
7
use ButterAMQP\Value;
8
9
class TypifiedValue extends AbstractValue
10
{
11
    /**
12
     * @var array
13
     */
14
    private static $types = [
15
        't' => Value\BooleanValue::class,
16
        'b' => Value\OctetValue::class,
17
        'B' => Value\UnsignedOctetValue::class,
18
        'U' => Value\ShortValue::class,
19
        'u' => Value\UnsignedShortValue::class,
20
        'I' => Value\LongValue::class,
21
        'i' => Value\UnsignedLongValue::class,
22
        'L' => Value\LongLongValue::class,
23
        'l' => Value\UnsignedLongLongValue::class,
24
        'f' => Value\FloatValue::class,
25
        'd' => Value\DoubleValue::class,
26
        's' => Value\ShortStringValue::class,
27
        'S' => Value\LongStringValue::class,
28
        'A' => Value\ArrayValue::class,
29
        'T' => Value\TimestampValue::class,
30
        'F' => Value\TableValue::class,
31
    ];
32
33
    /**
34
     * @var array
35
     */
36
    private static $hints = [
37
        Value\BooleanValue::class => 't',
38
        Value\OctetValue::class => 'b',
39
        Value\UnsignedOctetValue::class => 'B',
40
        Value\ShortValue::class => 'U',
41
        Value\UnsignedShortValue::class => 'u',
42
        Value\LongValue::class => 'I',
43
        Value\UnsignedLongValue::class => 'i',
44
        Value\LongLongValue::class => 'L',
45
        Value\UnsignedLongLongValue::class => 'l',
46
        Value\FloatValue::class => 'f',
47
        Value\DoubleValue::class => 'd',
48
        Value\ShortStringValue::class => 's',
49
        Value\LongStringValue::class => 'S',
50
        Value\ArrayValue::class => 'A',
51
        Value\TimestampValue::class => 'T',
52
        Value\TableValue::class => 'F',
53
    ];
54
55
    /**
56
     * @param Buffer $data
57
     *
58
     * @return mixed
59
     */
60 44
    public static function decode(Buffer $data)
61
    {
62 44
        $hint = $data->read(1);
63
64 44
        if (isset(self::$types[$hint])) {
65 42
            return call_user_func(self::$types[$hint].'::decode', $data);
66
        }
67
68 2
        if ($hint === 'V') {
69 1
            return null;
70
        }
71
72 1
        throw new \InvalidArgumentException(sprintf('Invalid type hint "%s"', $hint));
73
    }
74
75
    /**
76
     * @param mixed $value
77
     *
78
     * @return string
79
     *
80
     * @throws NotImplementedException
81
     */
82 44
    public static function encode($value)
83
    {
84 44
        $hint = self::guess($value);
85
86 43
        if ($value instanceof AbstractValue) {
87 16
            $value = $value->getValue();
88 16
        }
89
90 43
        if (isset(self::$types[$hint])) {
91 42
            return $hint.call_user_func(self::$types[$hint].'::encode', $value);
92
        }
93
94 1
        return 'V';
95
    }
96
97
    /**
98
     * Guess value type hint.
99
     *
100
     * @param mixed $value
101
     *
102
     * @return string
103
     */
104 44
    private static function guess($value)
105
    {
106 44
        if ($value === null) {
107 1
            return 'V';
108 43
        } elseif (is_string($value)) {
109 25
            return 'S';
110 42
        } elseif (is_array($value)) {
111 20
            return isset($value[0]) ? 'A' : 'F';
112 42
        } elseif (is_bool($value)) {
113 19
            return 't';
114 23
        } elseif (is_int($value)) {
115 7
            return 'I';
116 18
        } elseif (is_float($value)) {
117 1
            return 'f';
118 17
        } elseif (is_object($value)) {
119 17
            $type = get_class($value);
120
121 17
            if (isset(self::$hints[$type])) {
122 16
                return self::$hints[$type];
123
            }
124 1
        }
125
126 1
        throw new \InvalidArgumentException(sprintf(
127 1
            'Invalid value type "%s"',
128 1
            is_object($value) ? get_class($value) : gettype($value)
129 1
        ));
130
    }
131
}
132