|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\ViewReferenceBundle\Connector\Redis; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class ViewReferenceRedisTool. |
|
7
|
|
|
*/ |
|
8
|
|
|
class ViewReferenceRedisTool |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* This method generated a string that can be persisted for redis with data. |
|
12
|
|
|
* |
|
13
|
|
|
* @param $data |
|
14
|
|
|
* |
|
15
|
|
|
* @return string |
|
16
|
|
|
*/ |
|
17
|
|
|
public function redislize($data) |
|
18
|
|
|
{ |
|
19
|
|
|
// Only serialize if it's not a string or a integer |
|
20
|
|
|
if (!is_string($data) && !is_int($data)) { |
|
21
|
|
|
return urlencode(serialize($data)); |
|
22
|
|
|
} |
|
23
|
|
|
// Encode string to escape wrong saves |
|
24
|
|
|
return urlencode($data); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* This method unredislize a string. |
|
29
|
|
|
* |
|
30
|
|
|
* @param $data |
|
31
|
|
|
* |
|
32
|
|
|
* @return mixed|string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function unredislize($data) |
|
35
|
|
|
{ |
|
36
|
|
|
$data = urldecode($data); |
|
37
|
|
|
|
|
38
|
|
|
if (self::isSerialized($data)) { |
|
39
|
|
|
$unserializedData = @unserialize($data); |
|
40
|
|
|
if ($unserializedData !== false) { |
|
41
|
|
|
return $unserializedData; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $data; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Redislize an array (key, valueToRedislize). |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $data |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function redislizeArray(array $data) |
|
56
|
|
|
{ |
|
57
|
|
|
$result = []; |
|
58
|
|
|
foreach ($data as $key => $value) { |
|
59
|
|
|
if (!is_array($value)) { |
|
60
|
|
|
$result[$key] = $this->redislize($value); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $result; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* unredislize an array (key, valueToUnredislize). |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $data |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
public function unredislizeArray(array $data) |
|
75
|
|
|
{ |
|
76
|
|
|
$result = []; |
|
77
|
|
|
foreach ($data as $key => $value) { |
|
78
|
|
|
$result[$key] = $this->unredislize($value); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* This method generate a key hash. |
|
86
|
|
|
* |
|
87
|
|
|
* @param $alias |
|
88
|
|
|
* @param $id |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function generateKey($alias, $id) |
|
93
|
|
|
{ |
|
94
|
|
|
return $alias.':'.$id; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function isSerialized($data) { |
|
98
|
|
|
// if it isn't a string, it isn't serialized |
|
99
|
|
|
if ( !is_string( $data ) ) |
|
100
|
|
|
return false; |
|
101
|
|
|
$data = trim( $data ); |
|
102
|
|
|
if ( 'N;' == $data ) |
|
103
|
|
|
return true; |
|
104
|
|
|
if ( !preg_match( '/^([adObis]):/', $data, $badions ) ) |
|
105
|
|
|
return false; |
|
106
|
|
|
switch ( $badions[1] ) { |
|
107
|
|
|
case 'a' : |
|
|
|
|
|
|
108
|
|
|
case 'O' : |
|
|
|
|
|
|
109
|
|
|
case 's' : |
|
|
|
|
|
|
110
|
|
|
if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) ) |
|
111
|
|
|
return true; |
|
112
|
|
|
break; |
|
113
|
|
|
case 'b' : |
|
|
|
|
|
|
114
|
|
|
case 'i' : |
|
|
|
|
|
|
115
|
|
|
case 'd' : |
|
|
|
|
|
|
116
|
|
|
if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) ) |
|
117
|
|
|
return true; |
|
118
|
|
|
break; |
|
119
|
|
|
} |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.