use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+// Import SPL stuff
+use \InvalidArgumentException;
+
/**
* A number utility class
*
/**
* Filter a given number into a localized number
*
- * @param $value The raw value from e.g. database
+ * @param $value The raw float value from e.g. database
* @return $localized Localized value
+ * @throws InvalidArgumentException If a parameter has an invalid value
*/
- public static function doFilterFormatNumber ($value) {
+ public static function doFilterFormatNumber (float $value) {
+ // Check value
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NUMBER-UTILS: value=%s - CALLED!', $value));
+ if ($value < 0) {
+ // Not valid value
+ throw new InvalidArgumentException(sprintf('value=%s cannot be below zero', $value));
+ }
+
// Generate it from config and localize dependencies
switch (FrameworkBootstrap::getLanguageInstance()->getLanguageCode()) {
case 'de': // German format is a bit different to default
}
// Return it
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NUMBER-UTILS: localized=%s - CALLED!', $localized));
return $localized;
}
*
* @param $timestamp Timestamp to prepare (filter) for display
* @return $readable A readable timestamp
+ * @throws InvalidArgumentException If a parameter has an invalid value
*/
- public static function doFilterFormatTimestamp ($timestamp) {
+ public static function doFilterFormatTimestamp (string $timestamp = NULL) {
+ // Check parameter
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NUMBER-UTILS: timestamp[%s]=%s - CALLED!', gettype($timestamp), $timestamp));
+ if (empty($timestamp)) {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "timestamp" is empty');
+ }
+
// Default value to return
$readable = '???';
}
// Return the stamp
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NUMBER-UTILS: readable=%s - EXIT!', $readable));
return $readable;
}
* @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 $formatted The (hopefully) secured numbered value
+ * @throws InvalidArgumentException If a parameter has an invalid value
*/
public static function bigintval (string $num, bool $castValue = true, bool $assertMismatch = false) {
+ // Check parameter
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NUMBER-UTILS: num=%s,castValue=%d,assertMismatch=%d - CALLED!', $num, intval($castValue), intval($assertMismatch)));
+ if ($num === '') {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "num" is empty');
+ }
+
// Filter all numbers out
$formatted = preg_replace('/[^0123456789]/', '', $num);
// Shall we cast?
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('NUMBER-UTILS: formatted=%s', $formatted));
if ($castValue === true) {
// Cast to biggest numeric type, int is not enough for this cast
$formatted = (double) $formatted;
}
// Assert only if requested
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('NUMBER-UTILS: formatted[%s]=%s', gettype($formatted), $formatted));
if ($assertMismatch === true) {
// Has the whole value changed?
assert(('' . $formatted . '' != '' . $num . '') && (!is_null($num)));
}
// Return result
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NUMBER-UTILS: formatted[%s]=%s - EXIT!', gettype($formatted), $formatted));
return $formatted;
}