KeyValidation::validateKey()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 1
1
<?php
2
3
namespace Jlaswell\SimpleCache;
4
5
use Traversable;
6
use Jlaswell\SimpleCache\InvalidKeyException;
7
8
trait KeyValidation
9
{
10
    public function validateKey($key) : string
11
    {
12
        if (!is_string($key) || strpbrk($key, '{}()/\@:') || strlen($key) < 1) {
13
            throw new InvalidKeyException();
14
        }
15
16
        return $key;
17
    }
18
19
    private function transformKeys($keys)
20
    {
21
        if ($keys instanceof Traversable) {
22
            return $this->transformTraversableKeys($keys);
23
        } elseif (is_array($keys)) {
24
            foreach ($keys as $key) {
25
                $this->validateKey($key);
26
            }
27
            return $keys;
28
        }
29
30
        // @todo May need to adjust this exception type
31
        throw new InvalidKeyException(
32
            "Cannot call getMultiple with a non array or non Traversable type"
33
        );
34
    }
35
36
    private function transformTraversableKeys($keys)
37
    {
38
        $tempKeys = [];
39
        foreach ($keys as $key => $value) {
40
            $this->validateKey($key);
41
            $tempKeys[] = $key;
42
        }
43
44
        return $tempKeys;
45
    }
46
}
47