]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/utils/numbers/class_NumberUtils.php
Continued:
[core.git] / framework / main / classes / utils / numbers / class_NumberUtils.php
index 8e349015515928b5d82452f4fed99f38d0806bb3..264b17eecfdeb9a06d9ea7889172dcbc096ac285 100644 (file)
@@ -6,6 +6,9 @@ namespace Org\Mxchange\CoreFramework\Utils\Numbers;
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+
 /**
  * A number utility class
  *
@@ -42,10 +45,18 @@ final class NumberUtils extends BaseFrameworkSystem {
        /**
         * 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
@@ -58,6 +69,7 @@ final class NumberUtils extends BaseFrameworkSystem {
                }
 
                // Return it
+               //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NUMBER-UTILS: localized=%s - CALLED!', $localized));
                return $localized;
        }
 
@@ -68,8 +80,16 @@ final class NumberUtils extends BaseFrameworkSystem {
         *
         * @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 = '???';
 
@@ -103,6 +123,7 @@ final class NumberUtils extends BaseFrameworkSystem {
                }
 
                // Return the stamp
+               //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('NUMBER-UTILS: readable=%s - EXIT!', $readable));
                return $readable;
        }
 
@@ -113,24 +134,35 @@ final class NumberUtils extends BaseFrameworkSystem {
         * @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;
        }