|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Silviooosilva\CacheerPhp\Helpers\CacheerHelper; |
|
6
|
|
|
use Silviooosilva\CacheerPhp\Exceptions\CacheFileException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class CacheFileHelper |
|
10
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
|
11
|
|
|
* @package Silviooosilva\CacheerPhp |
|
12
|
|
|
*/ |
|
13
|
|
|
class CacheFileHelper |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Converts a string expiration format to seconds. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $expiration |
|
20
|
|
|
* @return float|int |
|
21
|
|
|
* @throws CacheFileException |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function convertExpirationToSeconds(string $expiration): float|int |
|
24
|
|
|
{ |
|
25
|
|
|
$units = [ |
|
26
|
|
|
'second' => 1, |
|
27
|
|
|
'minute' => 60, |
|
28
|
|
|
'hour' => 3600, |
|
29
|
|
|
'day' => 86400, |
|
30
|
|
|
'week' => 604800, |
|
31
|
|
|
'month' => 2592000, |
|
32
|
|
|
'year' => 31536000, |
|
33
|
|
|
]; |
|
34
|
|
|
foreach ($units as $unit => $value) { |
|
35
|
|
|
if (str_contains($expiration, $unit)) { |
|
36
|
|
|
return (int)$expiration * $value; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
throw CacheFileException::create("Invalid expiration format"); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Merges cache data with existing data. |
|
44
|
|
|
* |
|
45
|
|
|
* @param $cacheData |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function mergeCacheData($cacheData): array |
|
49
|
|
|
{ |
|
50
|
|
|
return CacheerHelper::mergeCacheData($cacheData); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Validates a cache item. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $item |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function validateCacheItem(array $item): void |
|
60
|
|
|
{ |
|
61
|
|
|
CacheerHelper::validateCacheItem( |
|
62
|
|
|
$item, |
|
63
|
|
|
fn($msg) => CacheFileException::create($msg) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Calculates the TTL (Time To Live) for cache items. |
|
69
|
|
|
* |
|
70
|
|
|
* @param null $ttl |
|
|
|
|
|
|
71
|
|
|
* @param int|null $defaultTTL |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
* @throws CacheFileException |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function ttl($ttl = null, ?int $defaultTTL = null): mixed |
|
76
|
|
|
{ |
|
77
|
|
|
if ($ttl) { |
|
|
|
|
|
|
78
|
|
|
$ttl = is_string($ttl) ? self::convertExpirationToSeconds($ttl) : $ttl; |
|
79
|
|
|
} else { |
|
80
|
|
|
$ttl = $defaultTTL; |
|
81
|
|
|
} |
|
82
|
|
|
return $ttl; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Generates an array identifier for cache data. |
|
87
|
|
|
* |
|
88
|
|
|
* @param mixed $currentCacheData |
|
89
|
|
|
* @param mixed $cacheData |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function arrayIdentifier(mixed $currentCacheData, mixed $cacheData): array |
|
93
|
|
|
{ |
|
94
|
|
|
return CacheerHelper::arrayIdentifier($currentCacheData, $cacheData); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|