| Total Complexity | 10 |
| Total Lines | 91 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 |
||
| 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 |
||
| 100 | } |
||
| 101 | } |
||
| 102 |