DataBag   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 91
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 3 1
A get() 0 3 1
A touched() 0 3 1
A set() 0 4 1
A __construct() 0 7 2
A clear() 0 4 1
A getAll() 0 3 1
1
<?php
2
3
namespace Jaxon\Plugin\Response\DataBag;
4
5
use JsonSerializable;
6
7
use function array_map;
8
use function is_array;
9
10
class DataBag implements JsonSerializable
11
{
12
    /**
13
     * @var DataBagPlugin
14
     */
15
    protected $xPlugin;
16
17
    /**
18
     * @var array
19
     */
20
    protected $aData = [];
21
22
    /**
23
     * @var bool
24
     */
25
    protected $bTouched = false;
26
27
    /**
28
     * The constructor
29
     *
30
     * @param array $aData
31
     */
32
    public function __construct(DataBagPlugin $xPlugin, array $aData)
33
    {
34
        $this->xPlugin = $xPlugin;
35
        // Ensure all contents are arrays.
36
        $this->aData = array_map(function($aValue) {
37
            return is_array($aValue) ? $aValue : [];
38
        }, $aData);
39
    }
40
41
    /**
42
     * @return bool
43
     */
44
    public function touched(): bool
45
    {
46
        return $this->bTouched;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function getAll(): array
53
    {
54
        return $this->aData;
55
    }
56
57
    /**
58
     * @param string $sBag
59
     *
60
     * @return void
61
     */
62
    public function clear(string $sBag)
63
    {
64
        $this->aData[$sBag] = [];
65
        $this->xPlugin->addCommand('databag.clear', ['bag' => $sBag]);
66
    }
67
68
    /**
69
     * @param string $sBag
70
     * @param string $sKey
71
     * @param mixed $xValue
72
     *
73
     * @return void
74
     */
75
    public function set(string $sBag, string $sKey, $xValue)
76
    {
77
        $this->bTouched = true;
78
        $this->aData[$sBag][$sKey] = $xValue;
79
    }
80
81
    /**
82
     * @param string $sBag
83
     * @param string $sKey
84
     * @param mixed $xValue
85
     *
86
     * @return mixed
87
     */
88
    public function get(string $sBag, string $sKey, $xValue = null)
89
    {
90
        return $this->aData[$sBag][$sKey] ?? $xValue;
91
    }
92
93
    /**
94
     * Convert this call to array, when converting the response into json.
95
     *
96
     * @return array
97
     */
98
    public function jsonSerialize(): array
99
    {
100
        return $this->aData;
101
    }
102
}
103