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