]> git.mxchange.org Git - core.git/blob - framework/main/classes/utils/number/class_NumberUtils.php
Continued:
[core.git] / framework / main / classes / utils / number / class_NumberUtils.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Utils\Number;
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 - 2020 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         /**
34          * Private constructor, no instance needed. If PHP would have a static initializer ...
35          *
36          * @return      void
37          */
38         private function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Filter a given number into a localized number
45          *
46          * @param       $value          The raw value from e.g. database
47          * @return      $localized      Localized value
48          */
49         public static function doFilterFormatNumber ($value) {
50                 // Generate it from config and localize dependencies
51                 switch (FrameworkBootstrap::getLanguageInstance()->getLanguageCode()) {
52                         case 'de': // German format is a bit different to default
53                                 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), ',', '.');
54                                 break;
55
56                         default: // US, etc.
57                                 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), '.', ',');
58                                 break;
59                 }
60
61                 // Return it
62                 return $localized;
63         }
64
65         /**
66          * Filter a given GMT timestamp (non Uni* stamp!) to make it look more
67          * beatiful for web-based front-ends. If null is given a message id
68          * null_timestamp will be resolved and returned.
69          *
70          * @param       $timestamp      Timestamp to prepare (filter) for display
71          * @return      $readable       A readable timestamp
72          */
73         public static function doFilterFormatTimestamp ($timestamp) {
74                 // Default value to return
75                 $readable = '???';
76
77                 // Is the timestamp null?
78                 if (is_null($timestamp)) {
79                         // Get a message string
80                         $readable = FrameworkBootstrap::getLanguageInstance()->getMessage('null_timestamp');
81                 } else {
82                         switch (FrameworkBootstrap::getLanguageInstance()->getLanguageCode()) {
83                                 case 'de': // German format is a bit different to default
84                                         // Split the GMT stamp up
85                                         $dateTime  = explode(' ', $timestamp  );
86                                         $dateArray = explode('-', $dateTime[0]);
87                                         $timeArray = explode(':', $dateTime[1]);
88
89                                         // Construct the timestamp
90                                         $readable = sprintf(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('german_date_time'),
91                                                 $dateArray[0],
92                                                 $dateArray[1],
93                                                 $dateArray[2],
94                                                 $timeArray[0],
95                                                 $timeArray[1],
96                                                 $timeArray[2]
97                                         );
98                                         break;
99
100                                 default: // Default is pass-through
101                                         $readable = $timestamp;
102                                         break;
103                         }
104                 }
105
106                 // Return the stamp
107                 return $readable;
108         }
109
110 }