33 lines
838 B
PHP
33 lines
838 B
PHP
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace App\Services;
|
||
|
|
||
|
class CacheKeyBuilder
|
||
|
{
|
||
|
/**
|
||
|
* @param mixed ...$args
|
||
|
* @return string
|
||
|
*/
|
||
|
public function buildCacheKeyFromArgs(...$args): string
|
||
|
{
|
||
|
$cacheName = '';
|
||
|
foreach ($args as $key => $arg) {
|
||
|
if (is_array($arg)) {
|
||
|
$cacheName .= http_build_query($arg, '', '|');
|
||
|
} elseif ($arg === null) {
|
||
|
$cacheName .= '|N';
|
||
|
} elseif (is_scalar($arg)) {
|
||
|
$cacheName .= '|' . (string) $arg;
|
||
|
} elseif ($arg instanceof CacheKeyInterface) {
|
||
|
$cacheName .= $arg->toCacheKey();
|
||
|
} else {
|
||
|
throw new \InvalidArgumentException("Invalid argument $key type for key generator.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $cacheName;
|
||
|
}
|
||
|
}
|