X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Futils%2Fclass_StringUtils.php;fp=framework%2Fmain%2Fclasses%2Futils%2Fclass_StringUtils.php;h=25e3cdbfeb85554ac3bf57dba51b8475c0a0cff8;hb=6e770c26c13f7b06bfc3f9b565b22f6607c5755e;hp=0000000000000000000000000000000000000000;hpb=384eb9724a031ed971f4f2aaf3cf691dbfc1690d;p=core.git diff --git a/framework/main/classes/utils/class_StringUtils.php b/framework/main/classes/utils/class_StringUtils.php new file mode 100644 index 00000000..25e3cdbf --- /dev/null +++ b/framework/main/classes/utils/class_StringUtils.php @@ -0,0 +1,74 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +final class StringUtils extends BaseFrameworkSystem { + /** + * Private constructor, no instance needed + * + * @return void + */ + private function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Converts dashes to underscores, e.g. useable for configuration entries + * + * @param $str The string with maybe dashes inside + * @return $str The converted string with no dashed, but underscores + * @throws NullPointerException If $str is null + * @throws InvalidArgumentException If $str is empty + */ + public static function convertDashesToUnderscores ($str) { + // Is it null? + if (is_null($str)) { + // Throw NPE + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } elseif (!is_string($str)) { + // Entry is empty + throw new InvalidArgumentException(sprintf('str[]=%s is not a string', gettype($str)), FrameworkConfiguration::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } elseif ((is_string($str)) && (empty($str))) { + // Entry is empty + throw new InvalidArgumentException('str is empty', FrameworkConfiguration::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } + + // Convert them all + $str = str_replace('-', '_', $str); + + // Return converted string + return $str; + } + +}