1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\Cache\Common; |
4
|
|
|
|
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTime; |
7
|
|
|
use Exception; |
8
|
|
|
use Traversable; |
9
|
|
|
use Vectorface\Cache\Exception\CacheException; |
10
|
|
|
use Vectorface\Cache\Exception\InvalidArgumentException as CacheArgumentException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Utility methods common to many PSR-16 cache implementations |
14
|
|
|
*/ |
15
|
|
|
trait PSR16Util |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The DateTime implementation to use |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private static $dateTimeClass = DateTime::class; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Enforce a fairly standard key format |
26
|
|
|
* |
27
|
|
|
* @param mixed $key |
28
|
|
|
* @return int|float|string|bool Returns the key, if valid |
29
|
|
|
* @throws CacheArgumentException Thrown if the key is not a legal value |
30
|
|
|
*/ |
31
|
101 |
|
protected function key($key) |
32
|
|
|
{ |
33
|
101 |
|
if (is_numeric($key) || is_string($key)) { |
34
|
97 |
|
return $key; |
35
|
|
|
} |
36
|
|
|
|
37
|
7 |
|
throw new CacheArgumentException("key is not a legal value"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Enforce fairly standard key formats on an iterable of values |
42
|
|
|
* @param iterable $values |
43
|
|
|
* @return iterable The values array |
44
|
|
|
* @throws CacheArgumentException Thrown if any of the keys is not a legal value |
45
|
|
|
*/ |
46
|
17 |
|
protected function values($values) { |
47
|
17 |
|
if (!is_array($values) && !($values instanceof Traversable)) { |
48
|
5 |
|
throw new CacheArgumentException("values must be provided as an array or a Traversable"); |
49
|
|
|
} |
50
|
|
|
|
51
|
12 |
|
$array = []; |
52
|
12 |
|
foreach ($values as $key => $value) { |
53
|
12 |
|
$array[$this->key($key)] = $value; |
54
|
|
|
} |
55
|
12 |
|
return $array; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Enforce a fairly standard key format on an array or Traversable of keys |
60
|
|
|
* |
61
|
|
|
* @param iterable $keys |
62
|
|
|
* @return mixed[] Returns the keys, if valid |
63
|
|
|
* @throws CacheArgumentException Thrown if any of the keys is not a legal value |
64
|
|
|
*/ |
65
|
21 |
|
protected function keys($keys) |
66
|
|
|
{ |
67
|
21 |
|
if (!is_array($keys) && !($keys instanceof Traversable)) { |
68
|
6 |
|
throw new CacheArgumentException("keys must be provided as an array or a Traversable"); |
69
|
|
|
} |
70
|
|
|
|
71
|
15 |
|
$array = []; |
72
|
15 |
|
foreach ($keys as $key) { |
73
|
15 |
|
$array[] = $this->key($key); |
74
|
|
|
} |
75
|
14 |
|
return $array; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Enforce a valid step value for increment/decrement methods |
80
|
|
|
* |
81
|
|
|
* @param int $step |
82
|
|
|
* @return int |
83
|
|
|
* @throws CacheArgumentException Thrown if the step is not a legal value |
84
|
|
|
*/ |
85
|
5 |
|
protected function step($step) |
86
|
|
|
{ |
87
|
5 |
|
if (!is_integer($step)) { |
|
|
|
|
88
|
|
|
throw new CacheArgumentException("step must be an integer"); |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
return $step; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add defaults to an array of values from a cache |
96
|
|
|
* |
97
|
|
|
* Note: This does NOT check the keys array |
98
|
|
|
* |
99
|
|
|
* @param array|iterable $keys An array of expected keys |
100
|
|
|
* @param array $values An array of values pulled from the cache |
101
|
|
|
* @param mixed $default The default value to be populated for missing entries |
102
|
|
|
* @return array The values array, with defaults added |
103
|
|
|
*/ |
104
|
2 |
|
protected static function defaults($keys, $values, $default) |
105
|
|
|
{ |
106
|
2 |
|
foreach ($keys as $key) { |
107
|
2 |
|
if (!isset($values[$key])) { |
108
|
2 |
|
$values[$key] = $default; |
109
|
|
|
} |
110
|
|
|
} |
111
|
2 |
|
return $values; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Convert a PSR-16 compatible TTL argument to a standard integer TTL as used by most caches |
116
|
|
|
* |
117
|
|
|
* @param mixed $ttl Takes a valid TTL argument and converts to an integer TTL |
118
|
|
|
* @throws CacheArgumentException|CacheException Throws if the argument is not a valid TTL |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
98 |
|
public static function ttl($ttl) |
122
|
|
|
{ |
123
|
98 |
|
if ($ttl instanceof DateInterval) { |
124
|
2 |
|
return static::intervalToTTL($ttl); |
125
|
96 |
|
} elseif (is_numeric($ttl) || $ttl === null) { |
126
|
93 |
|
return $ttl; |
127
|
|
|
} |
128
|
|
|
|
129
|
5 |
|
throw new CacheArgumentException("TTL must be specified as a number, a DateInterval, or null"); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Convert a DateInterval to a time diff in seconds |
134
|
|
|
* |
135
|
|
|
* @param DateInterval $interval |
136
|
|
|
* @return int The number of seconds from now until $interval |
137
|
|
|
* @throws CacheException |
138
|
|
|
*/ |
139
|
2 |
|
public static function intervalToTTL(DateInterval $interval) |
140
|
|
|
{ |
141
|
2 |
|
$dateClass = self::$dateTimeClass; |
142
|
|
|
|
143
|
|
|
try { |
144
|
2 |
|
$now = new $dateClass(); |
145
|
1 |
|
$exp = (new $dateClass())->add($interval); |
146
|
1 |
|
} catch (Exception $e) { |
147
|
1 |
|
throw new CacheException("Could not get current DateTime"); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
return $exp->getTimestamp() - $now->getTimestamp(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|