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 {
|
|
|
|
/**
|
|
|
|
* @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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|