Format::pack()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 4
rs 10
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