Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
KeyValidateTrait
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 validateKeys
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 validateKey
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php declare(strict_types=1);
2/**
3 * Banker
4 *
5 * A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
6 *
7 * PHP version 8+
8 *
9 * @package     Banker
10 * @author      Timothy J. Warren <tim@timshomepage.net>
11 * @copyright   2016 - 2023  Timothy J. Warren
12 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13 * @version     4.1.0
14 * @link        https://git.timshomepage.net/timw4mail/banker
15 */
16namespace Aviat\Banker;
17
18use Aviat\Banker\Exception\InvalidArgumentException;
19
20trait KeyValidateTrait {
21    /**
22     * @param iterable $keys
23     * @param bool $hash
24     */
25    protected function validateKeys(iterable $keys, bool $hash = FALSE): void
26    {
27        $keys = ($hash) ? array_keys((array)$keys) : (array)$keys;
28
29        // Check each key
30        array_walk($keys, [$this, 'validateKey']);
31    }
32
33    /**
34     * @param mixed $key
35     * @throws InvalidArgumentException
36     */
37    protected function validateKey(mixed $key): void
38    {
39        if ( ! is_string($key))
40        {
41            throw new InvalidArgumentException('Cache key must be a string.');
42        }
43        else if (preg_match("`[{}()/@:\\\]`", $key) === 1)
44        {
45            throw new InvalidArgumentException('Invalid characters in cache key');
46        }
47    }
48}