forked from codeigniter4/settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayHandler.php
More file actions
132 lines (115 loc) · 3.56 KB
/
ArrayHandler.php
File metadata and controls
132 lines (115 loc) · 3.56 KB
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
namespace CodeIgniter\Settings\Handlers;
/**
* Array Settings Handler
*
* Uses local storage to handle non-persistent
* Settings requests. Useful mostly for testing
* or extension by true persistent handlers.
*/
class ArrayHandler extends BaseHandler
{
/**
* Storage for general settings.
* Format: ['class' => ['property' => ['value', 'type']]]
*
* @var array<string,array<string,array>>
*/
private array $general = [];
/**
* Storage for context settings.
* Format: ['context' => ['class' => ['property' => ['value', 'type']]]]
*
* @var array<string,array|null>
*/
private array $contexts = [];
public function has(string $class, string $property, ?string $context = null): bool
{
return $this->hasStored($class, $property, $context);
}
public function get(string $class, string $property, ?string $context = null)
{
return $this->getStored($class, $property, $context);
}
public function set(string $class, string $property, $value = null, ?string $context = null)
{
$this->setStored($class, $property, $value, $context);
}
public function forget(string $class, string $property, ?string $context = null)
{
$this->forgetStored($class, $property, $context);
}
public function flush()
{
$this->general = [];
$this->contexts = [];
}
/**
* Checks whether this value is in storage.
*/
protected function hasStored(string $class, string $property, ?string $context): bool
{
if ($context === null) {
return isset($this->general[$class]) && array_key_exists($property, $this->general[$class]);
}
return isset($this->contexts[$context][$class]) && array_key_exists($property, $this->contexts[$context][$class]);
}
/**
* Retrieves a value from storage.
*
* @return mixed
*/
protected function getStored(string $class, string $property, ?string $context)
{
if (! $this->has($class, $property, $context)) {
return null;
}
return $context === null
? $this->parseValue(...$this->general[$class][$property])
: $this->parseValue(...$this->contexts[$context][$class][$property]);
}
/**
* Adds values to storage.
*
* @param mixed $value
*/
protected function setStored(string $class, string $property, $value, ?string $context): void
{
$type = gettype($value);
$value = $this->prepareValue($value);
if ($context === null) {
$this->general[$class][$property] = [
$value,
$type,
];
} else {
$this->contexts[$context][$class][$property] = [
$value,
$type,
];
}
}
/**
* Deletes an item from storage.
*/
protected function forgetStored(string $class, string $property, ?string $context): void
{
if ($context === null) {
unset($this->general[$class][$property]);
} else {
unset($this->contexts[$context][$class][$property]);
}
}
/**
* Retrieves all stored properties for a specific class and context.
*
* @return array<string,array> Format: ['property' => ['value', 'type']]
*/
protected function getAllStored(string $class, ?string $context): array
{
if ($context === null) {
return $this->general[$class] ?? [];
}
return $this->contexts[$context][$class] ?? [];
}
}