Continued:
authorRoland Häder <roland@mxchange.org>
Sat, 11 Dec 2021 23:18:41 +0000 (00:18 +0100)
committerRoland Häder <roland@mxchange.org>
Sat, 11 Dec 2021 23:18:41 +0000 (00:18 +0100)
- 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 <roland@mxchange.org>
framework/main/classes/utils/strings/class_StringUtils.php

index 947a657a84a37c2093f4fe0f90e3b50231042fba..257d325d5e23b44c947c479b327fcbb30cef3162 100644 (file)
@@ -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];
        }
 
        /**