|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Silviooosilva\CacheerPhp\Exceptions\CacheRedisException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class CacheRedisHelper |
|
9
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
|
10
|
|
|
* @package Silviooosilva\CacheerPhp |
|
11
|
|
|
*/ |
|
12
|
|
|
class CacheRedisHelper |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param mixed $data |
|
17
|
|
|
* @param bool $serialize |
|
18
|
|
|
* @return string |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function serialize(mixed $data, bool $serialize = true) |
|
21
|
|
|
{ |
|
22
|
|
|
if($serialize) { |
|
23
|
|
|
return serialize($data); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return unserialize($data); |
|
27
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $item |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function validateCacheItem(array $item) |
|
35
|
|
|
{ |
|
36
|
|
|
if (!isset($item['cacheKey']) || !isset($item['cacheData'])) { |
|
37
|
|
|
throw CacheRedisException::create("Each item must contain 'cacheKey' and 'cacheData'"); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $options |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function mergeCacheData($cacheData) |
|
46
|
|
|
{ |
|
47
|
|
|
if (is_array($cacheData) && is_array(reset($cacheData))) { |
|
48
|
|
|
$merged = []; |
|
49
|
|
|
foreach ($cacheData as $data) { |
|
50
|
|
|
$merged[] = $data; |
|
51
|
|
|
} |
|
52
|
|
|
return $merged; |
|
53
|
|
|
} |
|
54
|
|
|
return (array)$cacheData; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param mixed $currentCacheData |
|
59
|
|
|
* @param mixed $cacheData |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function arrayIdentifier(mixed $currentCacheData, mixed $cacheData) |
|
63
|
|
|
{ |
|
64
|
|
|
/** |
|
65
|
|
|
* Se ambos forem arrays, mescle-os de forma recursiva para preservar subarrays |
|
66
|
|
|
*/ |
|
67
|
|
|
if (is_array($currentCacheData) && is_array($cacheData)) { |
|
68
|
|
|
return self::mergeRecursive($currentCacheData, $cacheData); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Se $currentCacheData não for um array, inicialize-o como um array vazio |
|
73
|
|
|
*/ |
|
74
|
|
|
if (!is_array($currentCacheData)) { |
|
75
|
|
|
$currentCacheData = []; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Se $cacheData não for um array, converta-o em um array |
|
80
|
|
|
*/ |
|
81
|
|
|
if (!is_array($cacheData)) { |
|
82
|
|
|
$cacheData = [$cacheData]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return array_merge($currentCacheData, $cacheData); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Mescla arrays de forma recursiva. |
|
90
|
|
|
* @param array $array1 |
|
91
|
|
|
* @param array $array2 |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
private static function mergeRecursive(array $array1, array $array2) |
|
95
|
|
|
{ |
|
96
|
|
|
foreach ($array2 as $key => $value) { |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Se a chave existe em ambos os arrays e ambos os valores são arrays, mescle recursivamente |
|
100
|
|
|
*/ |
|
101
|
|
|
if (isset($array1[$key]) && is_array($array1[$key]) && is_array($value)) { |
|
102
|
|
|
$array1[$key] = self::mergeRecursive($array1[$key], $value); |
|
103
|
|
|
} else { |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Caso contrário, sobrescreva o valor em $array1 com o valor de $array2 |
|
107
|
|
|
*/ |
|
108
|
|
|
$array1[$key] = $value; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $array1; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|