Continued:
[core.git] / framework / main / classes / utils / class_StringUtils.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\String\Utils;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration;
7 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9
10 // Import SPL stuff
11 use \InvalidArgumentException;
12
13 /**
14  * A string utility class
15  *
16  * @author              Roland Haeder <webmaster@ship-simu.org>
17  * @version             0.0.0
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2019 Core Developer Team
19  * @license             GNU GPL 3.0 or any newer version
20  * @link                http://www.ship-simu.org
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 final class StringUtils extends BaseFrameworkSystem {
36         /**
37          * Private constructor, no instance needed
38          *
39          * @return      void
40          */
41         private function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Converts dashes to underscores, e.g. useable for configuration entries
48          *
49          * @param       $str    The string with maybe dashes inside
50          * @return      $str    The converted string with no dashed, but underscores
51          * @throws      NullPointerException    If $str is null
52          * @throws      InvalidArgumentException        If $str is empty
53          */
54         public static function convertDashesToUnderscores ($str) {
55                 // Is it null?
56                 if (is_null($str)) {
57                         // Throw NPE
58                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
59                 } elseif (!is_string($str)) {
60                         // Entry is empty
61                         throw new InvalidArgumentException(sprintf('str[]=%s is not a string', gettype($str)), FrameworkConfiguration::EXCEPTION_CONFIG_KEY_IS_EMPTY);
62                 } elseif ((is_string($str)) && (empty($str))) {
63                         // Entry is empty
64                         throw new InvalidArgumentException('str is empty', FrameworkConfiguration::EXCEPTION_CONFIG_KEY_IS_EMPTY);
65                 }
66
67                 // Convert them all
68                 $str = str_replace('-', '_', $str);
69
70                 // Return converted string
71                 return $str;
72         }
73
74 }