]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/utils/strings/class_StringUtils.php
Continued:
[core.git] / framework / main / classes / utils / strings / class_StringUtils.php
index 8ca43ddf62c6e3f2b4a5218f17976eff4d327b91..c95dc11dc3bffd4c1cae817a4a8262543f01a59b 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,22 +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) {
-               // Init class name
-               $className = '';
-
-               // Convert all dashes in underscores
-               $str = self::convertDashesToUnderscores($str);
+               // Is the parameter valid?
+               //* 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);
+               } 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
-               return $className;
+               //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: self[%s]=%s - EXIT!', $str, $className));
+               return self::$cache[$str];
        }
 
        /**
@@ -409,7 +428,7 @@ final class StringUtils extends BaseFrameworkSystem {
                        //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: big=%d,chunk(%d)=%s', $big, strlen($chunk), md5($chunk)));
 
                        $packed .= $chunk;
-               } // END - for
+               }
 
                // Return it
                //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: packed=%s - EXIT!', $packed));