Passed
Push — master ( 159f4a...679316 )
by
unknown
02:11
created

PSR16Util::key()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Vectorface\Cache\Common;
4
5
use DateInterval;
6
use DateTime;
7
use Traversable;
8
use Vectorface\Cache\Exception\CacheException;
9
use Vectorface\Cache\Exception\InvalidArgumentException as CacheArgumentException;
10
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 97
    protected function key($key)
32
    {
33 97
        if (is_numeric($key) || is_string($key)) {
34 93
            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
     * Add defaults to an array of values from a cache
80
     *
81
     * Note: This does NOT check the keys array
82
     *
83
     * @param array|iterable $keys An array of expected keys
84
     * @param array $values An array of values pulled from the cache
85
     * @param mixed $default The default value to be populated for missing entries
86
     * @return array The values array, with defaults added
87
     */
88 2
    protected static function defaults($keys, $values, $default)
89
    {
90 2
        foreach ($keys as $key) {
91 2
            if (!isset($values[$key])) {
92 2
                $values[$key] = $default;
93
            }
94
        }
95 2
        return $values;
96
    }
97
98
    /**
99
     * Convert a PSR-16 compatible TTL argument to a standard integer TTL as used by most caches
100
     *
101
     * @param mixed $ttl Takes a valid TTL argument and converts to an integer TTL
102
     * @throws CacheArgumentException|CacheException Throws if the argument is not a valid TTL
103
     * @return int
104
     */
105 96
    public static function ttl($ttl)
106
    {
107 96
        if ($ttl instanceof DateInterval) {
108 2
            return static::intervalToTTL($ttl);
109 94
        } elseif (is_numeric($ttl) || $ttl === null) {
110 91
            return $ttl;
111
        }
112
113 5
        throw new CacheArgumentException("TTL must be specified as a number, a DateInterval, or null");
114
    }
115
116
    /**
117
     * Convert a DateInterval to a time diff in seconds
118
     *
119
     * @param DateInterval $interval
120
     * @return int The number of seconds from now until $interval
121
     * @throws CacheException
122
     */
123 2
    public static function intervalToTTL(DateInterval $interval)
124
    {
125 2
        $dateClass = self::$dateTimeClass;
126
127
        try {
128 2
            $now = new $dateClass();
129 1
            $exp = (new $dateClass())->add($interval);
130 1
        } catch (\Exception $e) {
131 1
            throw new CacheException("Could not get current DateTime");
132
        }
133
134 1
        return $exp->getTimestamp() - $now->getTimestamp();
135
    }
136
}
137