Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ValidatesKey
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 validateKey
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Cache;
16
17/**
18 * Shared cache key validation, used by both the PSR-16 (Cache) and PSR-6 (Psr6\CacheItemPool) surfaces
19 *
20 * @category   Pop
21 * @package    Pop\Cache
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    5.0.0
26 */
27trait ValidatesKey
28{
29
30    /**
31     * Validate a cache key (throws on reserved characters or an empty string)
32     *
33     * @param  string $key
34     * @throws InvalidArgumentException
35     * @return void
36     */
37    protected function validateKey(string $key): void
38    {
39        if (($key === '') || (preg_match('#[{}()/\\\\@:]#', $key) === 1)) {
40            throw new InvalidArgumentException(
41                'Error: The cache key contains one or more reserved characters, or is empty.'
42            );
43        }
44    }
45
46}