From: Roland Häder Date: Sat, 11 Dec 2021 23:18:41 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7ffdc10bef06624ca2c6cf58e1109f880bbbf13e;p=core.git Continued: - StringUtils::convertToClassName() is now "in-method cached". That means that the parameter $str is used as key and the value is being generated when the key does not exist in self::$cache array Signed-off-by: Roland Häder --- diff --git a/framework/main/classes/utils/strings/class_StringUtils.php b/framework/main/classes/utils/strings/class_StringUtils.php index 947a657a..257d325d 100644 --- a/framework/main/classes/utils/strings/class_StringUtils.php +++ b/framework/main/classes/utils/strings/class_StringUtils.php @@ -44,6 +44,11 @@ final class StringUtils extends BaseFrameworkSystem { */ private static $decimals = ''; // German + /** + * In-method cache array + */ + private static $cache = []; + /** * Array with bitmasks and such for pack/unpack methods to support both * 32-bit and 64-bit systems @@ -190,30 +195,36 @@ final class StringUtils extends BaseFrameworkSystem { * * @param $str The string, what ever it is needs to be converted * @return $className Generated class name + * @throws InvalidArgumentException If a paramter is invalid */ public static final function convertToClassName (string $str) { // Is the parameter valid? - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: str=%s - CALLED!', $str)); + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: str=%s - CALLED!', $str)); if (empty($str)) { // No empty strings, please throw new InvalidArgumentException('Parameter "str" is empty', self::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } - - // Init class name - $className = ''; - - // Convert all dashes in underscores - $str = self::convertDashesToUnderscores($str); + } elseif (!isset(self::$cache[$str])) { + // Init class name + $className = ''; + + // Convert all dashes in underscores + $str = self::convertDashesToUnderscores($str); + + // Now use that underscores to get classname parts for hungarian style + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: str=%s - AFTER!', $str)); + foreach (explode('_', $str) as $strPart) { + // Make the class name part lower case and first upper case + $className .= ucfirst(strtolower($strPart)); + } - // Now use that underscores to get classname parts for hungarian style - foreach (explode('_', $str) as $strPart) { - // Make the class name part lower case and first upper case - $className .= ucfirst(strtolower($strPart)); + // Set cache + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: self[%s]=%s - SET!', $str, $className)); + self::$cache[$str] = $className; } // Return class name - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: className=%s - EXIT!', $className)); - return $className; + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: self[%s]=%s - EXIT!', $str, $className)); + return self::$cache[$str]; } /**