* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ final class NumberUtils extends BaseFrameworkSystem { /** * Private constructor, no instance needed. If PHP would have a static initializer ... * * @return void */ private function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Filter a given number into a localized number * * @param $value The raw value from e.g. database * @return $localized Localized value */ public static function doFilterFormatNumber ($value) { // Generate it from config and localize dependencies switch (FrameworkBootstrap::getLanguageInstance()->getLanguageCode()) { case 'de': // German format is a bit different to default $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), ',', '.'); break; default: // US, etc. $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), '.', ','); break; } // Return it return $localized; } /** * Filter a given GMT timestamp (non Uni* stamp!) to make it look more * beatiful for web-based front-ends. If null is given a message id * null_timestamp will be resolved and returned. * * @param $timestamp Timestamp to prepare (filter) for display * @return $readable A readable timestamp */ public static function doFilterFormatTimestamp ($timestamp) { // Default value to return $readable = '???'; // Is the timestamp null? if (is_null($timestamp)) { // Get a message string $readable = FrameworkBootstrap::getLanguageInstance()->getMessage('null_timestamp'); } else { switch (FrameworkBootstrap::getLanguageInstance()->getLanguageCode()) { case 'de': // German format is a bit different to default // Split the GMT stamp up $dateTime = explode(' ', $timestamp ); $dateArray = explode('-', $dateTime[0]); $timeArray = explode(':', $dateTime[1]); // Construct the timestamp $readable = sprintf(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('german_date_time'), $dateArray[0], $dateArray[1], $dateArray[2], $timeArray[0], $timeArray[1], $timeArray[2] ); break; default: // Default is pass-through $readable = $timestamp; break; } } // Return the stamp return $readable; } /** * Checks whether the given number is really a number (only chars 0-9). * * @param $num A string consisting only chars between 0 and 9 * @param $castValue Whether to cast the value to double. Do only use this to secure numbers from Requestable classes. * @param $assertMismatch Whether to assert mismatches * @return $ret The (hopefully) secured numbered value */ public static function bigintval (string $num, bool $castValue = true, bool $assertMismatch = false) { // Filter all numbers out $ret = preg_replace('/[^0123456789]/', '', $num); // Shall we cast? if ($castValue === true) { // Cast to biggest numeric type $ret = (double) $ret; } // Assert only if requested if ($assertMismatch === true) { // Has the whole value changed? assert(('' . $ret . '' != '' . $num . '') && (!is_null($num))); } // Return result return $ret; } }