Format   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 30
ccs 17
cts 17
cp 1
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pack() 0 12 4
A unpack() 0 14 4
1
<?php
2
3
namespace kalanis\kw_cache\Format;
4
5
6
use kalanis\kw_cache\Interfaces\IFormat;
7
8
9
/**
10
 * Class Format
11
 * @package kalanis\kw_cache\Format
12
 * Basic work with content to storage - let primitives stay, encode rest
13
 */
14
class Format implements IFormat
15
{
16 1
    public function unpack($content)
17
    {
18 1
        if (is_numeric($content)) {
19 1
            return $content;
20
        }
21 1
        if (is_bool($content)) {
22 1
            return $content;
23
        }
24 1
        $encodeResult = json_decode(strval($content), true);
25 1
        if (is_null($encodeResult)) {
26
            // problems with decoding - return original string
27 1
            return $content;
28
        }
29 1
        return $encodeResult;
30
    }
31
32 1
    public function pack($data)
33
    {
34 1
        if (is_bool($data)) {
35 1
            return $data;
36
        }
37 1
        if (is_numeric($data)) {
38 1
            return $data;
39
        }
40 1
        if (is_string($data)) {
41 1
            return $data;
42
        }
43 1
        return strval(json_encode($data));
44
    }
45
}
46