Passed
Push — main ( 10601e...ca0571 )
by Thierry
04:19
created

Databag   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

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