]> git.mxchange.org Git - core.git/blob - framework/main/classes/utils/numbers/class_NumberUtils.php
Continued:
[core.git] / framework / main / classes / utils / numbers / class_NumberUtils.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Utils\Numbers;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
8
9 /**
10  * A number utility class
11  *
12  * @author              Roland Haeder <webmaster@ship-simu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.ship-simu.org
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 final class NumberUtils extends BaseFrameworkSystem {
32         /**
33          * Private constructor, no instance needed. If PHP would have a static initializer ...
34          *
35          * @return      void
36          */
37         private function __construct () {
38                 // Call parent constructor
39                 parent::__construct(__CLASS__);
40         }
41
42         /**
43          * Filter a given number into a localized number
44          *
45          * @param       $value          The raw value from e.g. database
46          * @return      $localized      Localized value
47          */
48         public static function doFilterFormatNumber ($value) {
49                 // Generate it from config and localize dependencies
50                 switch (FrameworkBootstrap::getLanguageInstance()->getLanguageCode()) {
51                         case 'de': // German format is a bit different to default
52                                 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), ',', '.');
53                                 break;
54
55                         default: // US, etc.
56                                 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), '.', ',');
57                                 break;
58                 }
59
60                 // Return it
61                 return $localized;
62         }
63
64         /**
65          * Filter a given GMT timestamp (non Uni* stamp!) to make it look more
66          * beatiful for web-based front-ends. If null is given a message id
67          * null_timestamp will be resolved and returned.
68          *
69          * @param       $timestamp      Timestamp to prepare (filter) for display
70          * @return      $readable       A readable timestamp
71          */
72         public static function doFilterFormatTimestamp ($timestamp) {
73                 // Default value to return
74                 $readable = '???';
75
76                 // Is the timestamp null?
77                 if (is_null($timestamp)) {
78                         // Get a message string
79                         $readable = FrameworkBootstrap::getLanguageInstance()->getMessage('null_timestamp');
80                 } else {
81                         switch (FrameworkBootstrap::getLanguageInstance()->getLanguageCode()) {
82                                 case 'de': // German format is a bit different to default
83                                         // Split the GMT stamp up
84                                         $dateTime  = explode(' ', $timestamp  );
85                                         $dateArray = explode('-', $dateTime[0]);
86                                         $timeArray = explode(':', $dateTime[1]);
87
88                                         // Construct the timestamp
89                                         $readable = sprintf(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('german_date_time'),
90                                                 $dateArray[0],
91                                                 $dateArray[1],
92                                                 $dateArray[2],
93                                                 $timeArray[0],
94                                                 $timeArray[1],
95                                                 $timeArray[2]
96                                         );
97                                         break;
98
99                                 default: // Default is pass-through
100                                         $readable = $timestamp;
101                                         break;
102                         }
103                 }
104
105                 // Return the stamp
106                 return $readable;
107         }
108
109         /**
110          * Checks whether the given number is really a number (only chars 0-9).
111          *
112          * @param       $num    A string consisting only chars between 0 and 9
113          * @param       $castValue      Whether to cast the value to double. Do only use this to secure numbers from Requestable classes.
114          * @param       $assertMismatch         Whether to assert mismatches
115          * @return      $formatted      The (hopefully) secured numbered value
116          */
117         public static function bigintval (string $num, bool $castValue = true, bool $assertMismatch = false) {
118                 // Filter all numbers out
119                 $formatted = preg_replace('/[^0123456789]/', '', $num);
120
121                 // Shall we cast?
122                 if ($castValue === true) {
123                         // Cast to biggest numeric type, int is not enough for this cast
124                         $formatted = (double) $formatted;
125                 }
126
127                 // Assert only if requested
128                 if ($assertMismatch === true) {
129                         // Has the whole value changed?
130                         assert(('' . $formatted . '' != '' . $num . '') && (!is_null($num)));
131                 }
132
133                 // Return result
134                 return $formatted;
135         }
136
137 }