55 lines
1.3 KiB
PHP
55 lines
1.3 KiB
PHP
<?php declare(strict_types=1);
|
|
/**
|
|
* Banker
|
|
*
|
|
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
|
|
*
|
|
* PHP version 7.4+
|
|
*
|
|
* @package Banker
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
* @copyright 2016 - 2021 Timothy J. Warren
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
* @version 3.2.0
|
|
* @link https://git.timshomepage.net/timw4mail/banker
|
|
*/
|
|
namespace Aviat\Banker;
|
|
|
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
|
|
|
trait KeyValidateTrait {
|
|
/**
|
|
* @param $keys
|
|
* @param bool $hash
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
protected function validateKeys($keys, bool $hash = FALSE): void
|
|
{
|
|
// Check type of keys
|
|
if ( ! is_iterable($keys))
|
|
{
|
|
throw new InvalidArgumentException('Keys must be an array or a traversable object');
|
|
}
|
|
|
|
$keys = ($hash) ? array_keys((array)$keys) : (array)$keys;
|
|
|
|
// Check each key
|
|
array_walk($keys, fn($key) => $this->validateKey($key));
|
|
}
|
|
|
|
/**
|
|
* @param string $key
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
protected function validateKey($key): void
|
|
{
|
|
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');
|
|
}
|
|
}
|
|
} |