- var_dump: documentation ( source)
- strtr: documentation ( source)
- constant: documentation ( source)
<?php
class StringHelper
{
public function interpolate($str, array $context = [], $asIs = false)
{
$replace = [];
foreach ($context as $key => $val) {
if ($asIs) {
$replace[$key] = $val;
} else {
$replace['{{' . $key . '}}'] = $val;
}
}
return strtr($str, $replace);
}
}
class CacheKeysDict
{
protected static $stringHelper = null;
const BOT_ACTIVITY_MESSAGES = 'bot_activity_messages';
const PROFILE = 'profile_{{id}}';
public static function prepareKey($id, array $context = [])
{
$key = constant("CacheKeysDict::{$id}");
return self::getStringHelper()->interpolate($key, $context);
}
public static function getStringHelper()
{
if (is_null(self::$stringHelper)) {
self::$stringHelper = new StringHelper;
}
return self::$stringHelper;
}
}
var_dump([
CacheKeysDict::BOT_ACTIVITY_MESSAGES,
CacheKeysDict::prepareKey(CacheKeysDict::BOT_ACTIVITY_MESSAGES),
CacheKeysDict::prepareKey(CacheKeysDict::PROFILE, ['id' => 503]),
]);