-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Data.php
132 lines (112 loc) · 3.04 KB
/
Data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com>
* @copyright Marwan Al-Soltany 2021
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace MAKS\Velox\Frontend;
use MAKS\Velox\Backend\Event;
use MAKS\Velox\Backend\Config;
use MAKS\Velox\Helper\Misc;
/**
* A class that serves as an abstracted data bag/store that is accessible via dot-notation.
*
* Example:
* ```
* // get all data
* $allData = Data::getAll();
*
* // check for variable availability
* $someVarExists = Data::has('someVar');
*
* // get a specific variable or fall back to a default value
* $someVar = Data::get('someVar', 'fallbackValue');
*
* // set a specific variable value
* Data::set('someNewVar.someKey', 'someValue');
* ```
*
* @package Velox\Frontend
* @since 1.0.0
* @api
*/
class Data
{
/**
* This event will be dispatched when the data is loaded.
* This event will be passed a reference to the data array.
*
* @var string
*/
public const ON_LOAD = 'data.on.load';
/**
* The currently loaded data.
*/
protected static array $bag = [];
/**
* Loads the data from system configuration.
*
* @return void
*/
protected static function load(): void
{
if (empty(static::$bag)) {
static::$bag = (array)Config::get('data', static::$bag);
// make `Config::$config['data']` points to `Data::$bag` (the same array in memory)
$config = &Config::getReference();
$config['data'] = &static::$bag;
Event::dispatch(self::ON_LOAD, [&static::$bag]);
}
}
/**
* Checks whether a value of a key exists in `self::$bag` via dot-notation.
*
* @param string $key The dotted key representation.
*
* @return bool
*/
public static function has(string $key): bool
{
static::load();
$value = Misc::getArrayValueByKey(self::$bag, $key, null);
return isset($value);
}
/**
* Gets a value of a key from `self::$bag` via dot-notation.
*
* @param string $key The dotted key representation.
* @param mixed $default [optional] The default fallback value.
*
* @return mixed The requested value or null.
*/
public static function get(string $key, $default = null)
{
static::load();
return Misc::getArrayValueByKey(self::$bag, $key, $default);
}
/**
* Sets a value of a key in `self::$bag` via dot-notation.
*
* @param string $key The dotted key representation.
* @param mixed $value The value to set.
*
* @return void
*/
public static function set(string $key, $value): void
{
static::load();
Misc::setArrayValueByKey(self::$bag, $key, $value);
}
/**
* Returns the currently loaded data.
*
* @return array
*/
public static function getAll(): ?array
{
static::load();
return static::$bag;
}
}