Databag   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 10

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 2 1
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 JsonSerializable;
6
7
use function key_exists;
8
9
class Databag implements JsonSerializable
10
{
11
    /**
12
     * @var bool
13
     */
14
    protected $bTouched = false;
15
16
    /**
17
     * The constructor
18
     *
19
     * @param array $aData
20
     */
21
    public function __construct(protected array $aData)
22
    {}
23
24
    /**
25
     * @return bool
26
     */
27
    public function touched(): bool
28
    {
29
        return $this->bTouched;
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    public function getAll(): array
36
    {
37
        return $this->aData;
38
    }
39
40
    /**
41
     * @param string $sBag
42
     *
43
     * @return void
44
     */
45
    public function clear(string $sBag): void
46
    {
47
        $this->bTouched = true;
48
        $this->aData[$sBag] = [];
49
    }
50
51
    /**
52
     * @param string $sBag
53
     * @param string $sKey
54
     * @param mixed $xValue
55
     *
56
     * @return void
57
     */
58
    public function set(string $sBag, string $sKey, $xValue): void
59
    {
60
        $this->bTouched = true;
61
        $this->aData[$sBag][$sKey] = $xValue;
62
    }
63
64
    /**
65
     * @param string $sBag
66
     * @param string $sKey
67
     * @param mixed $xValue
68
     *
69
     * @return void
70
     */
71
    public function new(string $sBag, string $sKey, $xValue): void
72
    {
73
        // Set the value only if it doesn't already exist.
74
        if(!isset($this->aData[$sBag]) || !key_exists($sKey, $this->aData[$sBag]))
75
        {
76
            $this->set($sBag, $sKey, $xValue);
77
        }
78
    }
79
80
    /**
81
     * @param string $sBag
82
     * @param string $sKey
83
     * @param mixed $xValue
84
     *
85
     * @return mixed
86
     */
87
    public function get(string $sBag, string $sKey, $xValue = null): mixed
88
    {
89
        return $this->aData[$sBag][$sKey] ?? $xValue;
90
    }
91
92
    /**
93
     * Convert this call to array, when converting the response into json.
94
     *
95
     * @return array
96
     */
97
    public function jsonSerialize(): array
98
    {
99
        return $this->aData;
100
    }
101
}
102