Total Complexity | 8 |
Total Lines | 91 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 |
||
101 | } |
||
102 | } |
||
103 |