From: Roland Häder Date: Tue, 24 Nov 2020 03:00:41 +0000 (+0100) Subject: WIP: X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=6933805f6517e22a56f0c6cb3a6c8d8f26e72c73 WIP: - moved monolithic doFilter*() methods (dynamatic invocation) to new NumberUtils class Signed-off-by: Roland Häder --- diff --git a/framework/main/classes/class_BaseFrameworkSystem.php b/framework/main/classes/class_BaseFrameworkSystem.php index 91af004f..0c7e737e 100644 --- a/framework/main/classes/class_BaseFrameworkSystem.php +++ b/framework/main/classes/class_BaseFrameworkSystem.php @@ -224,7 +224,7 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac if (self::$startupTime == 0) { // Then set it self::$startupTime = microtime(true); - } // END - if + } } /** @@ -259,7 +259,7 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac * @args $args Arguments passed to the method * @return void */ - public final function __call ($methodName, $args) { + public final function __call (string $methodName, array $args = NULL) { // Set self-instance self::$instance = $this; @@ -278,11 +278,9 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac * @return void * @throws InvalidArgumentException If self::$instance is not a framework's own object */ - public static final function __callStatic ($methodName, $args) { - // Trace message - //* PRINT-DEBUG: */ printf('[%s:%d]: methodName=%s,args[]=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $methodName, gettype($args)); - + public static final function __callStatic (string $methodName, array $args = NULL) { // Init argument string and class name + //* PRINT-DEBUG: */ printf('[%s:%d]: methodName=%s,args[]=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $methodName, gettype($args)); $argsString = ''; $className = 'unknown'; @@ -336,19 +334,16 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac // Comma for next one $argsString .= ', '; - } // END - foreach + } // Last comma found? if (substr($argsString, -2, 1) == ',') { // Remove last comma $argsString = substr($argsString, 0, -2); - } // END - if + } // Close braces $argsString .= ')'; - } else { - // Invalid arguments! - $argsString = sprintf('!INVALID:%s!', gettype($args)); } // Output stub message @@ -379,7 +374,7 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac * @param $value Value to store * @return void */ - public final function __set ($name, $value) { + public final function __set (string $name, $value) { $this->debugBackTrace(sprintf('Tried to set a missing field. name=%s, value[%s]=%s', $name, gettype($value), @@ -393,7 +388,7 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac * @param $name Name of the field/attribute * @return void */ - public final function __get ($name) { + public final function __get (string $name) { $this->debugBackTrace(sprintf('Tried to get a missing field. name=%s', $name )); @@ -405,7 +400,7 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac * @param $name Name of the field/attribute * @return void */ - public final function __unset ($name) { + public final function __unset (string $name) { $this->debugBackTrace(sprintf('Tried to unset a missing field. name=%s', $name )); @@ -447,9 +442,9 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac * @param $realClass Class name (string) * @return void */ - public final function setRealClass ($realClass) { + public final function setRealClass (string $realClass) { // Set real class - $this->realClass = (string) $realClass; + $this->realClass = $realClass; } /** @@ -766,7 +761,7 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac // Is there a trailing slash? if (substr($str, -1, 1) != '/') { $str .= '/'; - } // END - if + } // Return string with trailing slash return $str; @@ -791,7 +786,7 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac $content = sprintf('
Message: %s
' . PHP_EOL, $message); - } // END - if + } // Generate the output $content .= sprintf('
%s
', @@ -852,7 +847,7 @@ Loaded includes: if (!empty($message)) { // Then add it as well $stubMessage .= ' Message: ' . $message; - } // END - if + } // Debug instance is there? if (!is_null($this->getDebugInstance())) { @@ -877,7 +872,7 @@ Loaded includes: if (!empty($message)) { // Output message printf('Message: %s
' . PHP_EOL, $message); - } // END - if + } print('
');
 		debug_print_backtrace();
@@ -886,7 +881,7 @@ Loaded includes:
 		// Exit program?
 		if ($doExit === true) {
 			exit();
-		} // END - if
+		}
 	}
 
 	/**
@@ -989,13 +984,13 @@ Loaded includes:
 			if ($doPrint === false) {
 				// Die here if not printed
 				exit();
-			} // END - if
+			}
 		} else {
 			// Are debug times enabled?
 			if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('debug_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_output_timings') == 'Y') {
 				// Prepent it
 				$message = $this->getPrintableExecutionTime() . $message;
-			} // END - if
+			}
 
 			// Put directly out
 			if ($doPrint === true) {
@@ -1030,7 +1025,7 @@ Loaded includes:
 				$errorArray['message'],
 				$errorArray['type']
 			);
-		} // END - if
+		}
 
 		// Add line number to the code
 		foreach (explode(chr(10), $phpCode) as $lineNo => $code) {
@@ -1039,79 +1034,12 @@ Loaded includes:
 				($lineNo + 1),
 				htmlentities($code, ENT_QUOTES)
 			);
-		} // END - foreach
+		}
 
 		// Return the code
 		return $markedCode;
 	}
 
-	/**
-	 * 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 function doFilterFormatTimestamp ($timestamp) {
-		// Default value to return
-		$readable = '???';
-
-		// Is the timestamp null?
-		if (is_null($timestamp)) {
-			// Get a message string
-			$readable = $this->getLanguageInstance()->getMessage('null_timestamp');
-		} else {
-			switch ($this->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;
-			} // END - switch
-		}
-
-		// Return the stamp
-		return $readable;
-	}
-
-	/**
-	 * Filter a given number into a localized number
-	 *
-	 * @param	$value		The raw value from e.g. database
-	 * @return	$localized	Localized value
-	 */
-	public function doFilterFormatNumber ($value) {
-		// Generate it from config and localize dependencies
-		switch ($this->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;
-		} // END - switch
-
-		// Return it
-		return $localized;
-	}
-
 	/**
 	 * "Getter" for databse entry
 	 *
@@ -1124,7 +1052,7 @@ Loaded includes:
 		if (!$this->getResultInstance() instanceof SearchableResult) {
 			// Throw an exception here
 			throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-		} // END - if
+		}
 
 		// Rewind it
 		$this->getResultInstance()->rewind();
@@ -1133,7 +1061,7 @@ Loaded includes:
 		if ($this->getResultInstance()->valid() === false) {
 			// @TODO Move the constant to e.g. BaseDatabaseResult when there is a non-cached database result available
 			throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), CachedDatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
-		} // END - if
+		}
 
 		// Get next entry
 		$this->getResultInstance()->next();
@@ -1163,7 +1091,7 @@ Loaded includes:
 		if (is_null($resultInstance)) {
 			// Then the user instance is no longer valid (expired cookies?)
 			throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-		} // END - if
+		}
 
 		// Get current array
 		$fieldArray = $resultInstance->current();
@@ -1203,7 +1131,7 @@ Loaded includes:
 		if (is_null($resultInstance)) {
 			// Then the user instance is no longer valid (expired cookies?)
 			throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-		} // END - if
+		}
 
 		// Get current array
 		$fieldArray = $resultInstance->current();
@@ -1238,7 +1166,7 @@ Loaded includes:
 
 			// Yes, then send the whole result to the database layer
 			$wrapperInstance->doUpdateByResult($this->getResultInstance());
-		} // END - if
+		}
 	}
 
 	/**
@@ -1407,7 +1335,7 @@ Loaded includes:
 		if (is_null(self::$hashLength)) {
 			// No, then hash a string and save its length.
 			self::$hashLength = strlen(self::hash('abc123'));
-		} // END - if
+		}
 
 		// Return it
 		return self::$hashLength;
@@ -1429,13 +1357,13 @@ Loaded includes:
 		if ($castValue === true) {
 			// Cast to biggest numeric type
 			$ret = (double) $ret;
-		} // END - if
+		}
 
 		// Assert only if requested
 		if ($assertMismatch === true) {
 			// Has the whole value changed?
 			assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
-		} // END - if
+		}
 
 		// Return result
 		return $ret;
@@ -1456,7 +1384,7 @@ Loaded includes:
 		if ($assertMismatch === true) {
 			// Has the whole value changed?
 			assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
-		} // END - if
+		}
 
 		// Return result
 		return $ret;
@@ -1532,7 +1460,7 @@ Loaded includes:
 			// No, then abort here
 			trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
 			exit;
-		} // END - if
+		}
 
 		// Debug message
 		//* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',value=' . print_r($this->genericArray[$keyGroup][$subGroup], true));
@@ -1657,7 +1585,7 @@ Loaded includes:
 			// Already initialized
 			trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' already initialized.');
 			exit;
-		} // END - if
+		}
 
 		// Initialize it
 		$this->genericArray[$keyGroup][$subGroup] = array();
@@ -1681,7 +1609,7 @@ Loaded includes:
 			// Already initialized
 			trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' already initialized.');
 			exit;
-		} // END - if
+		}
 
 		// Initialize it
 		$this->genericArray[$keyGroup][$subGroup][$key] = array();
@@ -1706,7 +1634,7 @@ Loaded includes:
 			// Already initialized
 			trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' already initialized.');
 			exit;
-		} // END - if
+		}
 
 		// Initialize it
 		$this->genericArray[$keyGroup][$subGroup][$key][$element] = array();
@@ -1729,7 +1657,7 @@ Loaded includes:
 		if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
 			// Initialize array
 			$this->initGenericArrayKey($keyGroup, $subGroup, $key);
-		} // END - if
+		}
 
 		// Then push it
 		$count = array_push($this->genericArray[$keyGroup][$subGroup][$key], $value);
@@ -1758,7 +1686,7 @@ Loaded includes:
 		if (!$this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
 			// Initialize array
 			$this->initGenericArrayElement($keyGroup, $subGroup, $key, $element);
-		} // END - if
+		}
 
 		// Then push it
 		$count = array_push($this->genericArray[$keyGroup][$subGroup][$key][$element], $value);
@@ -1786,7 +1714,7 @@ Loaded includes:
 			// Not found
 			trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
 			exit;
-		} // END - if
+		}
 
 		// Then "pop" it
 		$value = array_pop($this->genericArray[$keyGroup][$subGroup][$key]);
@@ -1814,7 +1742,7 @@ Loaded includes:
 			// Not found
 			trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
 			exit;
-		} // END - if
+		}
 
 		// Then "shift" it
 		$value = array_shift($this->genericArray[$keyGroup][$subGroup][$key]);
@@ -1840,7 +1768,7 @@ Loaded includes:
 			// Abort here
 			trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' not found.');
 			exit;
-		} // END - if
+		}
 
 		// Then count it
 		$count = count($this->genericArray[$keyGroup]);
@@ -1868,7 +1796,7 @@ Loaded includes:
 			// Abort here
 			trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
 			exit;
-		} // END - if
+		}
 
 		// Then count it
 		$count = count($this->genericArray[$keyGroup][$subGroup]);
@@ -1928,7 +1856,7 @@ Loaded includes:
 			// Then abort here
 			trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' does not exist.');
 			exit;
-		} // END - if
+		}
 
 		// Return it
 		return $this->genericArray[$keyGroup];
@@ -1968,7 +1896,7 @@ Loaded includes:
 			// Then abort here
 			trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' does not exist.');
 			exit;
-		} // END - if
+		}
 
 		// Return it
 		return $this->genericArray[$keyGroup][$subGroup][$key];
@@ -2010,7 +1938,7 @@ Loaded includes:
 			// Then abort here
 			trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' does not exist.');
 			exit;
-		} // END - if
+		}
 
 		// Return it
 		return $this->genericArray[$keyGroup][$subGroup][$key][$element];
@@ -2103,7 +2031,7 @@ Loaded includes:
 		if (!is_writable($basePath)) {
 			// Path is write-protected
 			throw new PathWriteProtectedException($infoInstance, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN);
-		} // END - if
+		}
 
 		// Add it
 		$tempInstance = new SplFileInfo($basePath . DIRECTORY_SEPARATOR . $infoInstance->getBasename());
@@ -2112,7 +2040,7 @@ Loaded includes:
 		if (!FrameworkBootstrap::isReachableFilePath($tempInstance)) {
 			// Not reachable
 			throw new FileIoException($tempInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
-		} // END - if
+		}
 
 		// Return it
 		return $tempInstance;
@@ -2134,7 +2062,7 @@ Loaded includes:
 		if ($stateInstance instanceof Stateable) {
 			// Then use that state name
 			$stateName = $stateInstance->getStateName();
-		} // END - if
+		}
 
 		// Return result
 		return $stateName;
diff --git a/framework/main/classes/helper/class_BaseHelper.php b/framework/main/classes/helper/class_BaseHelper.php
index e9cbdf38..494e3cbd 100644
--- a/framework/main/classes/helper/class_BaseHelper.php
+++ b/framework/main/classes/helper/class_BaseHelper.php
@@ -193,7 +193,7 @@ abstract class BaseHelper extends BaseFrameworkSystem {
 	 * @return	void
 	 * @todo	Rewrite this method using a helper class for filtering data
 	 */
-	public function assignFieldWithFilter ($fieldName, $filterMethod) {
+	public function assignFieldWithFilter (string $fieldName, string $filterMethod) {
 		// Get the value
 		$fieldValue = $this->getValueField($fieldName);
 		//* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'='.$fieldValue);
@@ -213,7 +213,7 @@ abstract class BaseHelper extends BaseFrameworkSystem {
 	 * @return	void
 	 * @throws	NullPointerException	If recovery of requested value instance failed
 	 */
-	public function prefetchValueInstance ($registryKey, $extraKey = NULL) {
+	public function prefetchValueInstance (string $registryKey, string $extraKey = NULL) {
 		//* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('O:'.$registryKey.'/'.$extraKey);
 		try {
 			// Get the required instance
diff --git a/framework/main/classes/helper/html/blocks/class_HtmlBlockHelper.php b/framework/main/classes/helper/html/blocks/class_HtmlBlockHelper.php
index d0d6177f..7a6a697c 100644
--- a/framework/main/classes/helper/html/blocks/class_HtmlBlockHelper.php
+++ b/framework/main/classes/helper/html/blocks/class_HtmlBlockHelper.php
@@ -52,7 +52,7 @@ class HtmlBlockHelper extends BaseHtmlHelper implements HelpableTemplate {
 	 * @param	$blockName			Name of the block we shall generate
 	 * @return	$helperInstance		A prepared instance of this helper
 	 */
-	public static final function createHtmlBlockHelper (CompileableTemplate $templateInstance, $blockName) {
+	public static final function createHtmlBlockHelper (CompileableTemplate $templateInstance, string $blockName) {
 		// Get new instance
 		$helperInstance = new HtmlBlockHelper();
 
@@ -102,7 +102,7 @@ class HtmlBlockHelper extends BaseHtmlHelper implements HelpableTemplate {
 	 * @param	$messageId			Message id to load an assign
 	 * @return	void
 	 */
-	public function assignMessageField ($templateVariable, $messageId) {
+	public function assignMessageField (string $templateVariable, string $messageId) {
 		// Get message
 		$message = $this->getLanguageInstance()->getMessage($messageId);
 
@@ -117,7 +117,7 @@ class HtmlBlockHelper extends BaseHtmlHelper implements HelpableTemplate {
 	 * @param	$actionValue	Action value to assign
 	 * @return	void
 	 */
-	public function assignLinkFieldWithAction ($linkField, $actionValue) {
+	public function assignLinkFieldWithAction (string $linkField, $actionValue) {
 		$this->getTemplateInstance()->assignVariable($linkField . '_action', $actionValue);
 	}
 
@@ -127,7 +127,7 @@ class HtmlBlockHelper extends BaseHtmlHelper implements HelpableTemplate {
 	 * @param	$userStatus		Raw user status from database layer
 	 * @return	$translated		Translated user status
 	 */
-	protected function doFilterUserStatusTranslator ($userStatus) {
+	protected function doFilterUserStatusTranslator (string $userStatus) {
 		// Generate message id
 		$messageId = 'user_status_' . strtolower($userStatus);
 
diff --git a/framework/main/classes/utils/number/class_NumberUtils.php b/framework/main/classes/utils/number/class_NumberUtils.php
new file mode 100644
index 00000000..2318858e
--- /dev/null
+++ b/framework/main/classes/utils/number/class_NumberUtils.php
@@ -0,0 +1,112 @@
+
+ * @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
+	 * @todo	Won't work as getLanguageInstance() is still in monolithic BaseFrameworkSystem
+	 */
+	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
+	 * @todo	Won't work as getLanguageInstance() is still in monolithic BaseFrameworkSystem
+	 */
+	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;
+	}
+
+}