3 namespace Org\Mxchange\CoreFramework\Utils\Number;
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 - 2020 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 {
34 * Private constructor, no instance needed. If PHP would have a static initializer ...
38 private function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Filter a given number into a localized number
46 * @param $value The raw value from e.g. database
47 * @return $localized Localized value
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'), ',', '.');
57 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), '.', ',');
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.
70 * @param $timestamp Timestamp to prepare (filter) for display
71 * @return $readable A readable timestamp
73 public static function doFilterFormatTimestamp ($timestamp) {
74 // Default value to return
77 // Is the timestamp null?
78 if (is_null($timestamp)) {
79 // Get a message string
80 $readable = FrameworkBootstrap::getLanguageInstance()->getMessage('null_timestamp');
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]);
89 // Construct the timestamp
90 $readable = sprintf(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('german_date_time'),
100 default: // Default is pass-through
101 $readable = $timestamp;
111 * Checks whether the given number is really a number (only chars 0-9).
113 * @param $num A string consisting only chars between 0 and 9
114 * @param $castValue Whether to cast the value to double. Do only use this to secure numbers from Requestable classes.
115 * @param $assertMismatch Whether to assert mismatches
116 * @return $ret The (hopefully) secured numbered value
118 public static function bigintval (string $num, bool $castValue = true, bool $assertMismatch = false) {
119 // Filter all numbers out
120 $ret = preg_replace('/[^0123456789]/', '', $num);
123 if ($castValue === true) {
124 // Cast to biggest numeric type
125 $ret = (double) $ret;
128 // Assert only if requested
129 if ($assertMismatch === true) {
130 // Has the whole value changed?
131 assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));