3 namespace Org\Mxchange\CoreFramework\Utils\Numbers;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 * A number utility class
12 * @author Roland Haeder <webmaster@ship-simu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.ship-simu.org
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.
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.
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/>.
31 final class NumberUtils extends BaseFrameworkSystem {
33 * Private constructor, no instance needed. If PHP would have a static initializer ...
37 private function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Filter a given number into a localized number
45 * @param $value The raw value from e.g. database
46 * @return $localized Localized value
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'), ',', '.');
56 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), '.', ',');
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.
69 * @param $timestamp Timestamp to prepare (filter) for display
70 * @return $readable A readable timestamp
72 public static function doFilterFormatTimestamp ($timestamp) {
73 // Default value to return
76 // Is the timestamp null?
77 if (is_null($timestamp)) {
78 // Get a message string
79 $readable = FrameworkBootstrap::getLanguageInstance()->getMessage('null_timestamp');
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]);
88 // Construct the timestamp
89 $readable = sprintf(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('german_date_time'),
99 default: // Default is pass-through
100 $readable = $timestamp;
110 * Checks whether the given number is really a number (only chars 0-9).
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
117 public static function bigintval (string $num, bool $castValue = true, bool $assertMismatch = false) {
118 // Filter all numbers out
119 $formatted = preg_replace('/[^0123456789]/', '', $num);
122 if ($castValue === true) {
123 // Cast to biggest numeric type, int is not enough for this cast
124 $formatted = (double) $formatted;
127 // Assert only if requested
128 if ($assertMismatch === true) {
129 // Has the whole value changed?
130 assert(('' . $formatted . '' != '' . $num . '') && (!is_null($num)));