|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
|
|
8
|
|
|
class CacheerHelper |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param array $item |
|
12
|
|
|
* @param callable|null $exceptionFactory |
|
13
|
|
|
* @return void |
|
14
|
|
|
*/ |
|
15
|
|
|
public static function validateCacheItem(array $item, ?callable $exceptionFactory = null) |
|
16
|
|
|
{ |
|
17
|
|
|
if (!isset($item['cacheKey']) || !isset($item['cacheData'])) { |
|
18
|
|
|
if ($exceptionFactory) { |
|
19
|
|
|
throw $exceptionFactory("Each item must contain 'cacheKey' and 'cacheData'"); |
|
20
|
|
|
} |
|
21
|
|
|
throw new InvalidArgumentException("Each item must contain 'cacheKey' and 'cacheData'"); |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param mixed $cacheData |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function mergeCacheData($cacheData) |
|
30
|
|
|
{ |
|
31
|
|
|
if (is_array($cacheData) && is_array(reset($cacheData))) { |
|
32
|
|
|
$merged = []; |
|
33
|
|
|
foreach ($cacheData as $data) { |
|
34
|
|
|
$merged[] = $data; |
|
35
|
|
|
} |
|
36
|
|
|
return $merged; |
|
37
|
|
|
} |
|
38
|
|
|
return (array)$cacheData; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param mixed $currentCacheData |
|
43
|
|
|
* @param mixed $cacheData |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function arrayIdentifier(mixed $currentCacheData, mixed $cacheData) |
|
47
|
|
|
{ |
|
48
|
|
|
if (is_array($currentCacheData) && is_array($cacheData)) { |
|
49
|
|
|
return array_merge($currentCacheData, $cacheData); |
|
50
|
|
|
} |
|
51
|
|
|
return array_merge((array)$currentCacheData, (array)$cacheData); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Prepara os dados para armazenamento, aplicando compressão e/ou encriptação. |
|
56
|
|
|
* |
|
57
|
|
|
* @param mixed $data |
|
58
|
|
|
* @param bool $compression |
|
59
|
|
|
* @param string|null $encryptionKey |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function prepareForStorage(mixed $data, bool $compression = false, ?string $encryptionKey = null) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$compression && is_null($encryptionKey)) { |
|
65
|
|
|
return $data; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$payload = serialize($data); |
|
69
|
|
|
|
|
70
|
|
|
if ($compression) { |
|
71
|
|
|
$payload = gzcompress($payload); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!is_null($encryptionKey)) { |
|
75
|
|
|
$iv = substr(hash('sha256', $encryptionKey), 0, 16); |
|
76
|
|
|
$encrypted = openssl_encrypt($payload, 'AES-256-CBC', $encryptionKey, 0, $iv); |
|
77
|
|
|
if ($encrypted === false) { |
|
78
|
|
|
throw new RuntimeException('Failed to encrypt cache data'); |
|
79
|
|
|
} |
|
80
|
|
|
$payload = $encrypted; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $payload; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Recupera os dados do armazenamento, aplicando decriptação e/ou descompressão. |
|
88
|
|
|
* |
|
89
|
|
|
* @param mixed $data |
|
90
|
|
|
* @param bool $compression |
|
91
|
|
|
* @param string|null $encryptionKey |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function recoverFromStorage(mixed $data, bool $compression = false, ?string $encryptionKey = null) |
|
95
|
|
|
{ |
|
96
|
|
|
if (!$compression && is_null($encryptionKey)) { |
|
97
|
|
|
return $data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (!is_null($encryptionKey)) { |
|
101
|
|
|
$iv = substr(hash('sha256', $encryptionKey), 0, 16); |
|
102
|
|
|
$decrypted = openssl_decrypt($data, 'AES-256-CBC', $encryptionKey, 0, $iv); |
|
103
|
|
|
if ($decrypted === false) { |
|
104
|
|
|
throw new RuntimeException('Failed to decrypt cache data'); |
|
105
|
|
|
} |
|
106
|
|
|
$data = $decrypted; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($compression) { |
|
110
|
|
|
$data = gzuncompress($data); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return unserialize($data); |
|
114
|
|
|
} |
|
115
|
|
|
} |