2021-02-05 13:26:01 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Banker
|
|
|
|
*
|
|
|
|
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
|
|
|
|
*
|
2021-11-30 11:56:15 -05:00
|
|
|
* PHP version 8+
|
2021-02-05 13:26:01 -05:00
|
|
|
*
|
|
|
|
* @package Banker
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2023-03-16 13:09:36 -04:00
|
|
|
* @copyright 2016 - 2023 Timothy J. Warren
|
2021-02-05 13:26:01 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2023-03-16 16:24:11 -04:00
|
|
|
* @version 4.1.1
|
2021-02-05 13:26:01 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/banker
|
|
|
|
*/
|
|
|
|
namespace Aviat\Banker;
|
|
|
|
|
|
|
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
|
|
|
|
|
|
|
trait KeyValidateTrait {
|
|
|
|
/**
|
2021-02-18 19:23:20 -05:00
|
|
|
* @param iterable $keys
|
2021-02-05 13:26:01 -05:00
|
|
|
* @param bool $hash
|
|
|
|
*/
|
2021-02-18 19:23:20 -05:00
|
|
|
protected function validateKeys(iterable $keys, bool $hash = FALSE): void
|
2021-02-05 13:26:01 -05:00
|
|
|
{
|
|
|
|
$keys = ($hash) ? array_keys((array)$keys) : (array)$keys;
|
|
|
|
|
|
|
|
// Check each key
|
2021-02-18 19:23:20 -05:00
|
|
|
array_walk($keys, [$this, 'validateKey']);
|
2021-02-05 13:26:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-18 19:23:20 -05:00
|
|
|
* @param mixed $key
|
2021-02-05 13:26:01 -05:00
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2021-02-18 19:23:20 -05:00
|
|
|
protected function validateKey(mixed $key): void
|
2021-02-05 13:26:01 -05:00
|
|
|
{
|
|
|
|
if ( ! is_string($key))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('Cache key must be a string.');
|
|
|
|
}
|
|
|
|
else if (preg_match("`[{}()/@:\\\]`", $key) === 1)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('Invalid characters in cache key');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|