-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.php
More file actions
197 lines (169 loc) · 5.21 KB
/
Cache.php
File metadata and controls
197 lines (169 loc) · 5.21 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Gt\FileCache;
use DateTimeImmutable;
use DateTimeInterface;
use Gt\TypeSafeGetter\CallbackTypeSafeGetter;
use TypeError;
class Cache implements CallbackTypeSafeGetter {
private FileAccess $fileAccess;
public function __construct(
string $path = "cache",
private readonly int $secondsValid = 60 * 60, // 1 hour of validity by default
?FileAccess $fileAccess = null,
) {
if(is_null($fileAccess)) {
$fileAccess = new FileAccess($path);
}
$this->fileAccess = $fileAccess;
}
public function get(
string $name,
callable $callback,
):mixed {
try {
$this->fileAccess->checkValidity($name, $this->secondsValid);
return $this->fileAccess->getData($name);
}
catch(FileNotFoundException|CacheInvalidException) {
try {
$value = $callback();
$this->fileAccess->setData($name, $value);
return $value;
}
catch(CacheValueGenerationException) {
return null;
}
}
}
public function getString(string $name, callable $callback):string {
return (string)$this->get($name, $callback);
}
public function getInt(string $name, callable $callback):int {
return (int)$this->get($name, $callback);
}
public function getFloat(string $name, callable $callback):float {
return (float)$this->get($name, $callback);
}
public function getBool(string $name, callable $callback):bool {
return (bool)$this->get($name, $callback);
}
public function getDateTime(string $name, callable $callback):DateTimeInterface {
$value = $this->get($name, $callback);
if($value instanceof DateTimeInterface) {
return $value;
}
elseif(is_int($value)) {
$dateTime = new DateTimeImmutable();
return $dateTime->setTimestamp($value);
}
return new DateTimeImmutable($value);
}
/** @return array<mixed> */
public function getArray(string $name, callable $callback):array {
$value = $this->get($name, $callback);
if(!is_array($value)) {
throw new TypeError("Data '$name' is not an array");
}
return $value;
}
/**
* @template TObject of object
* @param class-string<TObject>|"int"|"integer"|"float"|"double"|"string"|"bool"|"boolean" $className
* @phpstan-return ($className is class-string<TObject> ? array<TObject> : ($className is "int"|"integer" ? array<int> : ($className is "float"|"double" ? array<float> : ($className is "string" ? array<string> : array<bool>))))
*/
public function getTypedArray(string $name, string $className, callable $callback):array {
$array = $this->get($name, $callback);
if(!is_array($array)) {
throw new TypeError("Data '$name' is not an array");
}
/** @var array<int|string, mixed> $array */
foreach($array as $key => $value) {
$array[$key] = $this->validateAndConvertValue($value, $className, $key);
}
return $array;
}
/**
* @template TObject of object
* @param mixed $value
* @param class-string<TObject>|"int"|"integer"|"float"|"double"|"string"|"bool"|"boolean" $className
* @param string|int $key
* @phpstan-return ($className is class-string<TObject> ? TObject : ($className is "int"|"integer" ? int : ($className is "float"|"double" ? float : ($className is "string" ? string : bool))))
*/
private function validateAndConvertValue(mixed $value, string $className, string|int $key): mixed {
return match(strtolower($className)) {
"int", "integer" => $this->validateAndConvertInt($value, $key),
"float", "double" => $this->validateAndConvertFloat($value, $key),
"string" => $this->convertToString($value),
"bool", "boolean" => $this->convertToBool($value),
default => $this->validateInstance($value, $className, $key),
};
}
/**
* @param mixed $value
* @param string|int $key
* @return int
*/
private function validateAndConvertInt(mixed $value, string|int $key): int {
if(is_int($value)) {
return $value;
}
if(is_numeric($value)) {
return (int)$value;
}
throw new TypeError("Array value at key '$key' is not an integer");
}
/**
* @param mixed $value
* @param string|int $key
* @return float
*/
private function validateAndConvertFloat(mixed $value, string|int $key): float {
if(is_float($value)) {
return $value;
}
if(is_numeric($value)) {
return (float)$value;
}
throw new TypeError("Array value at key '$key' is not a float");
}
/**
* @param mixed $value
* @return string
*/
private function convertToString(mixed $value): string {
return (string)$value;
}
/**
* @param mixed $value
* @return bool
*/
private function convertToBool(mixed $value): bool {
return (bool)$value;
}
/**
* @template TObject of object
* @param mixed $value
* @param class-string<TObject> $className
* @param string|int $key
* @return TObject
*/
private function validateInstance(mixed $value, string $className, string|int $key): object {
if($value instanceof $className) {
return $value;
}
throw new TypeError("Array value at key '$key' is not an instance of $className");
}
/**
* @template T of object
* @param class-string<T> $className
* @return T
*/
public function getInstance(string $name, string $className, callable $callback):object {
$value = $this->get($name, $callback);
if(!$value instanceof $className) {
throw new TypeError("Value is not an instance of $className");
}
/** @var T $value */
return $value;
}
}