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-02-05 17:05:56 -05:00
|
|
|
* PHP version 7.4+
|
2021-02-05 13:26:01 -05:00
|
|
|
*
|
|
|
|
* @package Banker
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2021-02-05 17:05:56 -05:00
|
|
|
* @copyright 2016 - 2021 Timothy J. Warren
|
2021-02-05 13:26:01 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2021-02-05 17:05:56 -05:00
|
|
|
* @version 3.2.0
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|