KeyValidatorTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateKeys() 0 3 1
A validateKey() 0 8 4
1
<?php
2
3
namespace SubjectivePHP\Psr\SimpleCache;
4
5
/**
6
 * Trait for validating PSR-16 simple cache keys.
7
 */
8
trait KeyValidatorTrait
9
{
10
    /**
11
     * Verifies the given cache key is a legal value.
12
     *
13
     * @param mixed $key The cache key to validate.
14
     *
15
     * @return void
16
     *
17
     * @throws InvalidArgumentException Thrown if the $key string is not a legal value.
18
     */
19
    protected function validateKey($key)
20
    {
21
        if (!is_string($key) || $key === '') {
22
            throw new InvalidArgumentException('$key must be a valid non empty string');
23
        }
24
25
        if (preg_match('#[{}()/\\\@:]#', $key) > 0) {
26
            throw new InvalidArgumentException("Key '{$key}' contains unsupported characters");
27
        }
28
    }
29
30
    /**
31
     * Verifies all of the given cache keys are a legal values.
32
     *
33
     * @param array $keys The collection cache key to validate.
34
     *
35
     * @return void
36
     *
37
     * @throws InvalidArgumentException Thrown if the $key string is not a legal value.
38
     */
39
    protected function validateKeys(array $keys)
40
    {
41
        array_walk($keys, [$this, 'validateKey']);
42
    }
43
}
44