JsonParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 67
ccs 12
cts 14
cp 0.8571
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __destruct() 0 4 1
A encode() 0 4 1
A decode() 0 13 3
1
<?php
2
3
namespace Dazzle\Util\Parser\Json;
4
5
use Dazzle\Util\Enum\EnumInterface;
6
use Dazzle\Util\Enum\EnumTrait;
7
use Dazzle\Util\Parser\ParserInterface;
8
9
class JsonParser implements ParserInterface, EnumInterface
10
{
11
    use EnumTrait;
12
13
    /**
14
     * @var int
15
     */
16
    const DECODE_DEFAULT = 2;
17
18
    /**
19
     * @var int
20
     */
21
    const DECODE_OBJECT = 1;
22
23
    /**
24
     * @var int
25
     */
26
    const DECODE_ARRAY  = 2;
27
28
    /**
29
     * @var int
30
     */
31
    private $flags;
32
33
    /**
34
     * @param int $flags
35
     */
36 3
    public function __construct($flags = self::DECODE_DEFAULT)
37
    {
38 3
        $this->flags = $flags;
39 3
    }
40
41
    /**
42
     *
43
     */
44 3
    public function __destruct()
45
    {
46 3
        unset($this->flags);
47 3
    }
48
49
    /**
50
     * @override
51
     * @inheritDoc
52
     */
53 6
    public function encode($mixed)
54
    {
55 6
        return json_encode($mixed);
56
    }
57
58
    /**
59
     * @override
60
     * @inheritDoc
61
     */
62 1
    public function decode($str)
63
    {
64 1
        if (($this->flags & self::DECODE_ARRAY) === self::DECODE_ARRAY)
65
        {
66
            return json_decode($str, true);
67
        }
68 1
        else if (($this->flags & self::DECODE_OBJECT) === self::DECODE_OBJECT)
69
        {
70 1
            return json_decode($str);
71
        }
72
73
        return null;
74
    }
75
}
76