From: Roland Häder Date: Sat, 7 Nov 2020 14:46:02 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=513812814a5a17022d414727e5a4c6d0823fe5f6;p=core.git Continued: - moved class StringUtils to deeper package, needs updating ApplicationHelper! - moved some methods from monolithic class BaseFrameworkSystem to StringUtils Signed-off-by: Roland Häder --- diff --git a/application/tests/class_ApplicationHelper.php b/application/tests/class_ApplicationHelper.php index e4ee7971..5224a07e 100644 --- a/application/tests/class_ApplicationHelper.php +++ b/application/tests/class_ApplicationHelper.php @@ -10,6 +10,7 @@ use Org\Mxchange\CoreFramework\Loader\ClassLoader; use Org\Mxchange\CoreFramework\Manager\ManageableApplication; use Org\Mxchange\CoreFramework\Registry\Registerable; use Org\Mxchange\CoreFramework\Template\CompileableTemplate; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; /** * A class holding general data about the application and some methods for @@ -142,7 +143,7 @@ class ApplicationHelper extends BaseApplication implements ManageableApplication // Get a controller resolver $resolverClass = sprintf( 'Org\Mxchange\CoreFramework\Tests\Resolver\Controller\%s', - self::convertToClassName(sprintf( + StringUtils::convertToClassName(sprintf( '%s_%s_controller_resolver', $this->getAppShortName(), FrameworkBootstrap::getRequestTypeFromSystem() diff --git a/framework/bootstrap/class_FrameworkBootstrap.php b/framework/bootstrap/class_FrameworkBootstrap.php index e5d27674..2f73941b 100644 --- a/framework/bootstrap/class_FrameworkBootstrap.php +++ b/framework/bootstrap/class_FrameworkBootstrap.php @@ -18,6 +18,7 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Request\Requestable; use Org\Mxchange\CoreFramework\Response\Responseable; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \BadMethodCallException; @@ -237,7 +238,7 @@ final class FrameworkBootstrap { // Load basic include files to continue bootstrapping self::loadInclude(new SplFileInfo(sprintf('%smain%sinterfaces%sclass_FrameworkInterface.php', ApplicationEntryPoint::detectFrameworkPath(), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR))); self::loadInclude(new SplFileInfo(sprintf('%smain%sclasses%sclass_BaseFrameworkSystem.php', ApplicationEntryPoint::detectFrameworkPath(), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR))); - self::loadInclude(new SplFileInfo(sprintf('%smain%sclasses%sutils%sclass_StringUtils.php', ApplicationEntryPoint::detectFrameworkPath(), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR))); + self::loadInclude(new SplFileInfo(sprintf('%smain%sclasses%sutils%sstring%sclass_StringUtils.php', ApplicationEntryPoint::detectFrameworkPath(), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR))); self::loadInclude(new SplFileInfo(sprintf('%smain%sinterfaces%sregistry%sclass_Registerable.php', ApplicationEntryPoint::detectFrameworkPath(), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR))); self::loadInclude(new SplFileInfo(sprintf('%sconfig%sclass_FrameworkConfiguration.php', ApplicationEntryPoint::detectFrameworkPath(), DIRECTORY_SEPARATOR))); @@ -594,7 +595,7 @@ final class FrameworkBootstrap { $requestType = self::getRequestTypeFromSystem(); // Create a new request object - $requestInstance = ObjectFactory::createObjectByName(sprintf('Org\Mxchange\CoreFramework\Request\%sRequest', BaseFrameworkSystem::convertToClassName($request))); + $requestInstance = ObjectFactory::createObjectByName(sprintf('Org\Mxchange\CoreFramework\Request\%sRequest', StringUtils::convertToClassName($request))); // Remember request instance here self::setRequestInstance($requestInstance); @@ -607,7 +608,7 @@ final class FrameworkBootstrap { } // END - if // ... and a new response object - $responseClass = sprintf('Org\Mxchange\CoreFramework\Response\%sResponse', BaseFrameworkSystem::convertToClassName($request)); + $responseClass = sprintf('Org\Mxchange\CoreFramework\Response\%sResponse', StringUtils::convertToClassName($request)); $responseInstance = ObjectFactory::createObjectByName($responseClass); // Remember response instance here diff --git a/framework/config-global.php b/framework/config-global.php index 1a93ec9c..24eecc9e 100644 --- a/framework/config-global.php +++ b/framework/config-global.php @@ -460,5 +460,8 @@ $cfg->setConfigEntry('registry_iterator_class', 'Org\Mxchange\CoreFramework\Iter // CFG: QUIET-DNS-RESOLVER $cfg->setConfigEntry('quiet_dns_resolver', FALSE); -// Remove config from this name-space. Don't worry, no configuration is cleared. -unset($cfg); +// CFG: THOUSANDS-SEPARATOR +$cfg->setConfigEntry('thousands_separator', '.'); + +// CFG: DECIMALS-SEPARATOR +$cfg->setConfigEntry('decimals_separator', ','); diff --git a/framework/config/class_FrameworkConfiguration.php b/framework/config/class_FrameworkConfiguration.php index e8a5b308..29fdabea 100644 --- a/framework/config/class_FrameworkConfiguration.php +++ b/framework/config/class_FrameworkConfiguration.php @@ -9,7 +9,7 @@ use Org\Mxchange\CoreFramework\Generic\NullPointerException; use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; use Org\Mxchange\CoreFramework\Registry\Registerable; -use Org\Mxchange\CoreFramework\String\Utils\StringUtils; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \InvalidArgumentException; diff --git a/framework/main/classes/class_BaseFrameworkSystem.php b/framework/main/classes/class_BaseFrameworkSystem.php index 085abf3b..34076636 100644 --- a/framework/main/classes/class_BaseFrameworkSystem.php +++ b/framework/main/classes/class_BaseFrameworkSystem.php @@ -41,9 +41,9 @@ use Org\Mxchange\CoreFramework\State\Stateable; use Org\Mxchange\CoreFramework\Stream\Input\InputStream; use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer; use Org\Mxchange\CoreFramework\Stream\Output\OutputStream; -use Org\Mxchange\CoreFramework\String\Utils\StringUtils; use Org\Mxchange\CoreFramework\Template\CompileableTemplate; use Org\Mxchange\CoreFramework\User\ManageableAccount; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; use Org\Mxchange\CoreFramework\Visitor\Visitor; // Import SPL stuff @@ -232,49 +232,11 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac */ private $callbackInstance = NULL; - /** - * Thousands separator - */ - private $thousands = '.'; // German - - /** - * Decimal separator - */ - private $decimals = ','; // German - /** * Generic array */ private $genericArray = array(); - /** - * Array with bitmasks and such for pack/unpack methods to support both - * 32-bit and 64-bit systems - */ - private $packingData = array( - 32 => array( - 'step' => 3, - 'left' => 0xffff0000, - 'right' => 0x0000ffff, - 'factor' => 16, - 'format' => 'II', - ), - 64 => array( - 'step' => 7, - 'left' => 0xffffffff00000000, - 'right' => 0x00000000ffffffff, - 'factor' => 32, - 'format' => 'NN' - ) - ); - - /** - * Simple 64-bit check, thanks to "Salman A" from stackoverflow.com: - * - * The integer size is 4 bytes on 32-bit and 8 bytes on a 64-bit system. - */ - private $archArrayElement = false; - /*********************** * Exception codes.... * ***********************/ @@ -408,9 +370,6 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac // Then set it self::$startupTime = microtime(true); } // END - if - - // Set array element - $this->archArrayElement = (PHP_INT_SIZE === 8 ? 64 : 32); } /** @@ -1305,35 +1264,6 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac return crc32($this->__toString()); } - /** - * Formats computer generated price values into human-understandable formats - * with thousand and decimal separators. - * - * @param $value The in computer format value for a price - * @param $currency The currency symbol (use HTML-valid characters!) - * @param $decNum Number of decimals after commata - * @return $price The for the current language formated price string - * @throws MissingDecimalsThousandsSeparatorException If decimals or - * thousands separator - * is missing - */ - public function formatCurrency ($value, $currency = '€', $decNum = 2) { - // Are all required attriutes set? - if ((!isset($this->decimals)) || (!isset($this->thousands))) { - // Throw an exception - throw new MissingDecimalsThousandsSeparatorException($this, self::EXCEPTION_ATTRIBUTES_ARE_MISSING); - } // END - if - - // Cast the number - $value = (float) $value; - - // Reformat the US number - $price = number_format($value, $decNum, $this->decimals, $this->thousands) . $currency; - - // Return as string... - return $price; - } - /** * Appends a trailing slash to a string * @@ -1616,29 +1546,6 @@ Loaded includes: } } - /** - * Converts e.g. a command from URL to a valid class by keeping out bad characters - * - * @param $str The string, what ever it is needs to be converted - * @return $className Generated class name - */ - public static final function convertToClassName ($str) { - // Init class name - $className = ''; - - // Convert all dashes in underscores - $str = StringUtils::convertDashesToUnderscores($str); - - // Now use that underscores to get classname parts for hungarian style - foreach (explode('_', $str) as $strPart) { - // Make the class name part lower case and first upper case - $className .= ucfirst(strtolower($strPart)); - } // END - foreach - - // Return class name - return $className; - } - /** * Marks up the code by adding e.g. line numbers * @@ -1783,7 +1690,7 @@ Loaded includes: * @return $fieldValue Field value from the user * @throws NullPointerException If the result instance is null */ - public final function getField ($fieldName) { + public final function getField (string $fieldName) { // Default field value $fieldValue = NULL; @@ -1826,7 +1733,7 @@ Loaded includes: * @return $isSet Whether the given field name is set * @throws NullPointerException If the result instance is null */ - public function isFieldSet ($fieldName) { + public function isFieldSet (string $fieldName) { // Get result instance $resultInstance = $this->getResultInstance(); @@ -2843,7 +2750,7 @@ Loaded includes: * @param $boolean Boolean value * @return $translated Translated boolean value */ - public static final function translateBooleanToYesNo ($boolean) { + public static final function translateBooleanToYesNo (bool $boolean) { // Make sure it is really boolean assert(is_bool($boolean)); @@ -2854,74 +2761,6 @@ Loaded includes: return $translated; } - /** - * Encodes raw data (almost any type) by "serializing" it and then pack it - * into a "binary format". - * - * @param $rawData Raw data (almost any type) - * @return $encoded Encoded data - */ - protected function encodeData ($rawData) { - // Make sure no objects or resources pass through - assert(!is_object($rawData)); - assert(!is_resource($rawData)); - - // First "serialize" it (json_encode() is faster than serialize()) - $encoded = $this->packString(json_encode($rawData)); - - // And return it - return $encoded; - } - - /** - * Pack a string into a "binary format". Please execuse me that this is - * widely undocumented. :-( - * - * @param $str Unpacked string - * @return $packed Packed string - * @todo Improve documentation - */ - protected function packString ($str) { - // Debug message - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('str=' . $str . ' - CALLED!'); - - // First compress the string (gzcompress is okay) - $str = gzcompress($str); - - // Init variable - $packed = ''; - - // And start the "encoding" loop - for ($idx = 0; $idx < strlen($str); $idx += $this->packingData[$this->archArrayElement]['step']) { - $big = 0; - for ($i = 0; $i < $this->packingData[$this->archArrayElement]['step']; $i++) { - $factor = ($this->packingData[$this->archArrayElement]['step'] - 1 - $i); - - if (($idx + $i) <= strlen($str)) { - $ord = ord(substr($str, ($idx + $i), 1)); - - $add = $ord * pow(256, $factor); - - $big += $add; - - //print 'idx=' . $idx . ',i=' . $i . ',ord=' . $ord . ',factor=' . $factor . ',add=' . $add . ',big=' . $big . PHP_EOL; - } // END - if - } // END - for - - $l = ($big & $this->packingData[$this->archArrayElement]['left']) >>$this->packingData[$this->archArrayElement]['factor']; - $r = $big & $this->packingData[$this->archArrayElement]['right']; - - $chunk = str_pad(pack($this->packingData[$this->archArrayElement]['format'], $l, $r), 8, '0', STR_PAD_LEFT); - //* NOISY-DEBUG */ print 'big=' . $big . ',chunk('.strlen($chunk) . ')='.md5($chunk).PHP_EOL; - - $packed .= $chunk; - } // END - for - - // Return it - //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('packed=' . $packed . ' - EXIT!'); - return $packed; - } - /** * Creates a full-qualified file name (FQFN) for given file name by adding * a configured temporary file path to it. diff --git a/framework/main/classes/commands/html/class_HtmlLoginAreaCommand.php b/framework/main/classes/commands/html/class_HtmlLoginAreaCommand.php index 0adfc32b..e7859fa9 100644 --- a/framework/main/classes/commands/html/class_HtmlLoginAreaCommand.php +++ b/framework/main/classes/commands/html/class_HtmlLoginAreaCommand.php @@ -13,7 +13,7 @@ use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Request\Requestable; use Org\Mxchange\CoreFramework\Resolver\Command\CommandResolver; use Org\Mxchange\CoreFramework\Response\Responseable; -use Org\Mxchange\CoreFramework\String\Utils\StringUtils; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; /** * A command for the login area (member/gamer area) diff --git a/framework/main/classes/criteria/class_BaseCriteria.php b/framework/main/classes/criteria/class_BaseCriteria.php index df5a8cdf..1073cbc4 100644 --- a/framework/main/classes/criteria/class_BaseCriteria.php +++ b/framework/main/classes/criteria/class_BaseCriteria.php @@ -6,7 +6,7 @@ namespace Org\Mxchange\CoreFramework\Criteria; use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Criteria\Search\SearchCriteria; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; -use Org\Mxchange\CoreFramework\String\Utils\StringUtils; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; /** * A general crtieria class diff --git a/framework/main/classes/feature/class_FrameworkFeature.php b/framework/main/classes/feature/class_FrameworkFeature.php index 6310f98e..f5d87d95 100644 --- a/framework/main/classes/feature/class_FrameworkFeature.php +++ b/framework/main/classes/feature/class_FrameworkFeature.php @@ -7,6 +7,7 @@ use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Loader\NoClassException; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; /** * The general feature management class. No instance is needed as this class @@ -156,7 +157,7 @@ class FrameworkFeature extends BaseFrameworkSystem { // Array for call-back $callable = array( self::$enabledFeatures[$featureName]['instance'], - sprintf('featureMethod%s', self::convertToClassName($featureMethod)) + sprintf('featureMethod%s', StringUtils::convertToClassName($featureMethod)) ); // So is the feature's method callable? diff --git a/framework/main/classes/file_directories/binary/stack/class_StackFile.php b/framework/main/classes/file_directories/binary/stack/class_StackFile.php index 7a41398a..f1ff7e47 100644 --- a/framework/main/classes/file_directories/binary/stack/class_StackFile.php +++ b/framework/main/classes/file_directories/binary/stack/class_StackFile.php @@ -6,6 +6,7 @@ namespace Org\Mxchange\CoreFramework\Filesystem\Stack; use Org\Mxchange\CoreFramework\Filesystem\Block; use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile; use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException; +use Org\Mxchange\CoreFramework\String\Util\StringUtils; // Import SPL stuff use \SplFileInfo; @@ -77,7 +78,7 @@ class StackFile extends BaseBinaryFile implements Block { assert(!is_resource($value)); // Encode/convert the value into a "binary format" - $encoded = $this->encodeData($value); + $encoded = StringUtils::encodeData($value); // Get a strong hash for the "encoded" data $hash = self::hash($encoded); diff --git a/framework/main/classes/helper/class_BaseHelper.php b/framework/main/classes/helper/class_BaseHelper.php index 6ab87f05..e9cbdf38 100644 --- a/framework/main/classes/helper/class_BaseHelper.php +++ b/framework/main/classes/helper/class_BaseHelper.php @@ -8,6 +8,7 @@ use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; use Org\Mxchange\CoreFramework\Generic\NullPointerException; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; /** * A generic helper class with generic methods @@ -198,7 +199,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'='.$fieldValue); // Now filter it through the value through the filter method - $filteredValue = call_user_func_array(array($this, 'doFilter' . self::convertToClassName($filterMethod)), array($fieldValue)); + $filteredValue = call_user_func_array(array($this, 'doFilter' . StringUtils::convertToClassName($filterMethod)), array($fieldValue)); // Assign it with a template variable $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue); diff --git a/framework/main/classes/helper/html/links/class_HtmlLinkHelper.php b/framework/main/classes/helper/html/links/class_HtmlLinkHelper.php index caba4ff1..13552a20 100644 --- a/framework/main/classes/helper/html/links/class_HtmlLinkHelper.php +++ b/framework/main/classes/helper/html/links/class_HtmlLinkHelper.php @@ -8,7 +8,7 @@ use Org\Mxchange\CoreFramework\Configuration\FrameworkConfiguration; use Org\Mxchange\CoreFramework\Generic\NullPointerException; use Org\Mxchange\CoreFramework\Helper\Template\HelpableTemplate; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; -use Org\Mxchange\CoreFramework\String\Utils\StringUtils; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; use Org\Mxchange\CoreFramework\Template\CompileableTemplate; /** diff --git a/framework/main/classes/request/html/class_HtmlRequest.php b/framework/main/classes/request/html/class_HtmlRequest.php index 391fc8fa..f8b0fdba 100644 --- a/framework/main/classes/request/html/class_HtmlRequest.php +++ b/framework/main/classes/request/html/class_HtmlRequest.php @@ -4,7 +4,7 @@ namespace Org\Mxchange\CoreFramework\Request; // Import framework stuff use Org\Mxchange\CoreFramework\Request\Requestable; -use Org\Mxchange\CoreFramework\String\Utils\StringUtils; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; /** * A concrete and secured HTML request class to make HTML requests more abstract diff --git a/framework/main/classes/resolver/action/class_BaseActionResolver.php b/framework/main/classes/resolver/action/class_BaseActionResolver.php index cff62f7e..98341652 100644 --- a/framework/main/classes/resolver/action/class_BaseActionResolver.php +++ b/framework/main/classes/resolver/action/class_BaseActionResolver.php @@ -5,6 +5,7 @@ namespace Org\Mxchange\CoreFramework\Resolver\Action; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Resolver\BaseResolver; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \InvalidArgumentException; @@ -90,7 +91,7 @@ abstract class BaseActionResolver extends BaseResolver { '%s\%s%sAction', $namespace, $this->getCapitalizedClassPrefix(), - self::convertToClassName($actionName) + StringUtils::convertToClassName($actionName) ); // Now, let us create the full name of the action class @@ -123,7 +124,7 @@ abstract class BaseActionResolver extends BaseResolver { '%s\%s%sAction', $this->getNamespace(), $this->getCapitalizedClassPrefix(), - self::convertToClassName($actionName) + StringUtils::convertToClassName($actionName) ); // ... and set it diff --git a/framework/main/classes/resolver/class_BaseResolver.php b/framework/main/classes/resolver/class_BaseResolver.php index 46f1a0d5..7b262c37 100644 --- a/framework/main/classes/resolver/class_BaseResolver.php +++ b/framework/main/classes/resolver/class_BaseResolver.php @@ -6,6 +6,7 @@ namespace Org\Mxchange\CoreFramework\Resolver; use Org\Mxchange\CoreFramework\Controller\Controller; use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; /** * A generic resolver class @@ -120,7 +121,7 @@ abstract class BaseResolver extends BaseFrameworkSystem { $className = $this->getClassPrefix(); // And capitalize it - $className = self::convertToClassName($className); + $className = StringUtils::convertToClassName($className); // Return it return $className; diff --git a/framework/main/classes/resolver/command/class_BaseCommandResolver.php b/framework/main/classes/resolver/command/class_BaseCommandResolver.php index 98c3a81e..ed9fbdf6 100644 --- a/framework/main/classes/resolver/command/class_BaseCommandResolver.php +++ b/framework/main/classes/resolver/command/class_BaseCommandResolver.php @@ -9,6 +9,7 @@ use Org\Mxchange\CoreFramework\Command\InvalidCommandException; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Request\Requestable; use Org\Mxchange\CoreFramework\Resolver\BaseResolver; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \InvalidArgumentException; @@ -89,7 +90,7 @@ abstract class BaseCommandResolver extends BaseResolver { '%s\%s%sCommand', $this->getNamespace(), $this->getCapitalizedClassPrefix(), - self::convertToClassName($commandName) + StringUtils::convertToClassName($commandName) ); // Create command class name @@ -218,7 +219,7 @@ abstract class BaseCommandResolver extends BaseResolver { '%s\%s%sCommand', $namespace, $this->getCapitalizedClassPrefix(), - self::convertToClassName($commandName) + StringUtils::convertToClassName($commandName) ); // Now, let us create the full name of the command class diff --git a/framework/main/classes/resolver/controller/class_BaseControllerResolver.php b/framework/main/classes/resolver/controller/class_BaseControllerResolver.php index 1da067a8..12281e34 100644 --- a/framework/main/classes/resolver/controller/class_BaseControllerResolver.php +++ b/framework/main/classes/resolver/controller/class_BaseControllerResolver.php @@ -11,6 +11,7 @@ use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Resolver\BaseResolver; use Org\Mxchange\CoreFramework\Resolver\Controller\ControllerResolver; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \InvalidArgumentException; @@ -108,7 +109,7 @@ abstract class BaseControllerResolver extends BaseResolver { '%s\%s%sController', $this->getNamespace(), $this->getCapitalizedClassPrefix(), - self::convertToClassName($controllerName) + StringUtils::convertToClassName($controllerName) ); // ... and set it @@ -175,7 +176,7 @@ abstract class BaseControllerResolver extends BaseResolver { $className = sprintf( '%s\%sController', $namespace, - $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName) + $this->getCapitalizedClassPrefix() . StringUtils::convertToClassName($controllerName) ); $newsControllerName = sprintf( '%s\%sDefaultNewsController', diff --git a/framework/main/classes/template/class_BaseTemplateEngine.php b/framework/main/classes/template/class_BaseTemplateEngine.php index 87642847..772fa885 100644 --- a/framework/main/classes/template/class_BaseTemplateEngine.php +++ b/framework/main/classes/template/class_BaseTemplateEngine.php @@ -13,7 +13,7 @@ use Org\Mxchange\CoreFramework\Manager\ManageableApplication; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Response\Responseable; -use Org\Mxchange\CoreFramework\String\Utils\StringUtils; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \InvalidArgumentException; @@ -1456,7 +1456,7 @@ abstract class BaseTemplateEngine extends BaseFrameworkSystem { // Is this view helper loaded? if (!isset($this->helpers[$helperName])) { // Create a class name - $className = self::convertToClassName($helperName) . 'ViewHelper'; + $className = StringUtils::convertToClassName($helperName) . 'ViewHelper'; // Generate new instance $this->helpers[$helperName] = ObjectFactory::createObjectByName($className); diff --git a/framework/main/classes/template/image/class_ImageTemplateEngine.php b/framework/main/classes/template/image/class_ImageTemplateEngine.php index 1ef96f6d..85574487 100644 --- a/framework/main/classes/template/image/class_ImageTemplateEngine.php +++ b/framework/main/classes/template/image/class_ImageTemplateEngine.php @@ -11,6 +11,7 @@ use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Response\Responseable; use Org\Mxchange\CoreFramework\Template\CompileableTemplate; use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \SplFileInfo; @@ -182,10 +183,10 @@ class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTempl //* DEBUG: */ echo "START: >".$element."<
\n"; if (in_array($element, $this->mainNodes)) { // Okay, main node found! - $methodName = 'setImage' . self::convertToClassName($element); + $methodName = 'setImage' . StringUtils::convertToClassName($element); } elseif (in_array($element, $this->subNodes)) { // Sub node found - $methodName = 'setImageProperty' . self::convertToClassName($element); + $methodName = 'setImageProperty' . StringUtils::convertToClassName($element); } elseif ($element != 'image') { // Invalid node name found throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN); @@ -219,7 +220,7 @@ class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTempl } // Construct method name - $methodName = 'finish' . self::convertToClassName($nodeName); + $methodName = 'finish' . StringUtils::convertToClassName($nodeName); // Call the corresponding method call_user_func_array(array($this->getImageInstance(), $methodName), array()); @@ -271,7 +272,7 @@ class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTempl $imageType = $this->compileRawCode($imageType); // Now make a class name of it - $className = self::convertToClassName($imageType.'_image'); + $className = StringUtils::convertToClassName($imageType.'_image'); // And try to initiate it $this->setImageInstance(ObjectFactory::createObjectByName($className, array($this))); diff --git a/framework/main/classes/template/mail/class_MailTemplateEngine.php b/framework/main/classes/template/mail/class_MailTemplateEngine.php index e68a66e6..a442cf1e 100644 --- a/framework/main/classes/template/mail/class_MailTemplateEngine.php +++ b/framework/main/classes/template/mail/class_MailTemplateEngine.php @@ -11,6 +11,7 @@ use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Response\Responseable; use Org\Mxchange\CoreFramework\Template\CompileableTemplate; use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \UnexpectedValueException; @@ -175,10 +176,10 @@ class MailTemplateEngine extends BaseTemplateEngine implements CompileableTempla //* DEBUG: */ echo "START: >".$element."<
\n"; if (in_array($element, $this->getMainNodes())) { // Okay, main node found! - $methodName = 'setEmail' . self::convertToClassName($element); + $methodName = 'setEmail' . StringUtils::convertToClassName($element); } elseif (in_array($element, $this->getSubNodes())) { // Sub node found - $methodName = 'setEmailProperty' . self::convertToClassName($element); + $methodName = 'setEmailProperty' . StringUtils::convertToClassName($element); } elseif ($element != 'text-mail') { // Invalid node name found throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN); @@ -212,7 +213,7 @@ class MailTemplateEngine extends BaseTemplateEngine implements CompileableTempla } // Construct method name - $methodName = 'finish' . self::convertToClassName($nodeName); + $methodName = 'finish' . StringUtils::convertToClassName($nodeName); // Call the corresponding method call_user_func_array(array($this, $methodName), array()); diff --git a/framework/main/classes/template/menu/class_MenuTemplateEngine.php b/framework/main/classes/template/menu/class_MenuTemplateEngine.php index 0bc2e03e..39177f1d 100644 --- a/framework/main/classes/template/menu/class_MenuTemplateEngine.php +++ b/framework/main/classes/template/menu/class_MenuTemplateEngine.php @@ -11,6 +11,7 @@ use Org\Mxchange\CoreFramework\Parser\Xml\XmlParser; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Template\CompileableTemplate; use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine; +use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \SplFileInfo; @@ -270,13 +271,13 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla //* DEBUG: */ echo "START: >".$element."<
\n"; if (in_array($element, $this->getMainNodes())) { // Okay, main node found! - $methodName = 'start' . self::convertToClassName($element); + $methodName = 'start' . StringUtils::convertToClassName($element); // Set it $this->setCurrMainNode($element); } elseif (in_array($element, $this->getSubNodes())) { // Sub node found - $methodName = 'start' . self::convertToClassName($element); + $methodName = 'start' . StringUtils::convertToClassName($element); } elseif ($element != 'menu') { // Invalid node name found throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN); @@ -307,7 +308,7 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla } // END - if // Construct method name - $methodName = 'finish' . self::convertToClassName($nodeName); + $methodName = 'finish' . StringUtils::convertToClassName($nodeName); // Call the corresponding method //* DEBUG: */ echo "call: ".$methodName."
\n"; diff --git a/framework/main/classes/utils/class_StringUtils.php b/framework/main/classes/utils/class_StringUtils.php deleted file mode 100644 index de0e7c8a..00000000 --- a/framework/main/classes/utils/class_StringUtils.php +++ /dev/null @@ -1,74 +0,0 @@ - - * @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 StringUtils extends BaseFrameworkSystem { - /** - * Private constructor, no instance needed - * - * @return void - */ - private function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - } - - /** - * Converts dashes to underscores, e.g. useable for configuration entries - * - * @param $str The string with maybe dashes inside - * @return $str The converted string with no dashed, but underscores - * @throws NullPointerException If $str is null - * @throws InvalidArgumentException If $str is empty - */ - public static function convertDashesToUnderscores ($str) { - // Is it null? - if (is_null($str)) { - // Throw NPE - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } elseif (!is_string($str)) { - // Entry is empty - throw new InvalidArgumentException(sprintf('str[]=%s is not a string', gettype($str)), FrameworkConfiguration::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } elseif ((is_string($str)) && (empty($str))) { - // Entry is empty - throw new InvalidArgumentException('str is empty', FrameworkConfiguration::EXCEPTION_CONFIG_KEY_IS_EMPTY); - } - - // Convert them all - $str = str_replace('-', '_', $str); - - // Return converted string - return $str; - } - -} diff --git a/framework/main/classes/utils/string/class_StringUtils.php b/framework/main/classes/utils/string/class_StringUtils.php new file mode 100644 index 00000000..8d4a2280 --- /dev/null +++ b/framework/main/classes/utils/string/class_StringUtils.php @@ -0,0 +1,243 @@ + + * @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 StringUtils extends BaseFrameworkSystem { + /** + * Thousands separator + */ + private static $thousands = ''; // German + + /** + * Decimal separator + */ + private static $decimals = ''; // German + + /** + * Array with bitmasks and such for pack/unpack methods to support both + * 32-bit and 64-bit systems + */ + private static $packingData = array( + 32 => array( + 'step' => 3, + 'left' => 0xffff0000, + 'right' => 0x0000ffff, + 'factor' => 16, + 'format' => 'II', + ), + 64 => array( + 'step' => 7, + 'left' => 0xffffffff00000000, + 'right' => 0x00000000ffffffff, + 'factor' => 32, + 'format' => 'NN' + ) + ); + + /** + * Simple 64-bit check, thanks to "Salman A" from stackoverflow.com: + * + * The integer size is 4 bytes on 32-bit and 8 bytes on a 64-bit system. + */ + private static $archArrayElement = 0; + + /** + * Private constructor, no instance needed. If PHP would have a static initializer ... + * + * @return void + */ + private function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Is one not set? + if (empty(self::$archArrayElement)) { + // Set array element + self::$archArrayElement = (PHP_INT_SIZE === 8 ? 64 : 32); + + // Init from configuration + self::$thousands = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('thousands_separator'); + self::$decimals = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals_separator'); + } + } + + /** + * Converts dashes to underscores, e.g. useable for configuration entries + * + * @param $str The string with maybe dashes inside + * @return $str The converted string with no dashed, but underscores + * @throws NullPointerException If $str is null + * @throws InvalidArgumentException If $str is empty + */ + public static function convertDashesToUnderscores ($str) { + // Is it null? + if (is_null($str)) { + // Throw NPE + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } elseif (!is_string($str)) { + // Entry is empty + throw new InvalidArgumentException(sprintf('str[]=%s is not a string', gettype($str)), FrameworkConfiguration::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } elseif ((is_string($str)) && (empty($str))) { + // Entry is empty + throw new InvalidArgumentException('str is empty', FrameworkConfiguration::EXCEPTION_CONFIG_KEY_IS_EMPTY); + } + + // Convert them all + $str = str_replace('-', '_', $str); + + // Return converted string + return $str; + } + + /** + * Encodes raw data (almost any type) by "serializing" it and then pack it + * into a "binary format". + * + * @param $rawData Raw data (almost any type) + * @return $encoded Encoded data + * @throws InvalidArgumentException If $rawData has a non-serializable data type + */ + public static function encodeData ($rawData) { + // Make sure no objects or resources pass through + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: rawData[]=%s - CALLED!', gettype($rawData))); + if (is_object($rawData) || is_resource($rawData)) { + // Not all variable types should be serialized here + throw new InvalidArgumentException(sprintf('rawData[]=%s cannot be serialized.', gettype($rawData))); + } + + // Init instance + $dummyInstance = new StringUtils(); + + // First "serialize" it (json_encode() is faster than serialize()) + $encoded = self::packString(json_encode($rawData)); + + // And return it + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: encoded()=%d - EXIT!', strlen($encoded))); + return $encoded; + } + + /** + * Converts e.g. a command from URL to a valid class by keeping out bad characters + * + * @param $str The string, what ever it is needs to be converted + * @return $className Generated class name + */ + public static final function convertToClassName (string $str) { + // Init class name + $className = ''; + + // Convert all dashes in underscores + $str = self::convertDashesToUnderscores($str); + + // Now use that underscores to get classname parts for hungarian style + foreach (explode('_', $str) as $strPart) { + // Make the class name part lower case and first upper case + $className .= ucfirst(strtolower($strPart)); + } + + // Return class name + return $className; + } + + /** + * Formats computer generated price values into human-understandable formats + * with thousand and decimal separators. + * + * @param $value The in computer format value for a price + * @param $currency The currency symbol (use HTML-valid characters!) + * @param $decNum Number of decimals after commata + * @return $price The for the current language formated price string + * @throws MissingDecimalsThousandsSeparatorException If decimals or thousands separator is missing + */ + public static function formatCurrency (float $value, string $currency = '€', int $decNum = 2) { + // Init instance + $dummyInstance = new StringUtils(); + + // Reformat the US number + $price = number_format($value, $decNum, self::$decimals, self::$thousands) . $currency; + + // Return as string... + return $price; + } + + /** + * Pack a string into a "binary format". Please execuse me that this is + * widely undocumented. :-( + * + * @param $str Unpacked string + * @return $packed Packed string + * @todo Improve documentation + */ + private static function packString (string $str) { + // First compress the string (gzcompress is okay) + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: str=%s - CALLED!', $str)); + $str = gzcompress($str); + + // Init variable + $packed = ''; + + // And start the "encoding" loop + for ($idx = 0; $idx < strlen($str); $idx += self::$packingData[self::$archArrayElement]['step']) { + $big = 0; + for ($i = 0; $i < self::$packingData[self::$archArrayElement]['step']; $i++) { + $factor = (self::$packingData[self::$archArrayElement]['step'] - 1 - $i); + + if (($idx + $i) <= strlen($str)) { + $ord = ord(substr($str, ($idx + $i), 1)); + + $add = $ord * pow(256, $factor); + + $big += $add; + + //print 'idx=' . $idx . ',i=' . $i . ',ord=' . $ord . ',factor=' . $factor . ',add=' . $add . ',big=' . $big . PHP_EOL; + } + } + + // Left/right parts (low/high?) + $l = ($big & self::$packingData[self::$archArrayElement]['left']) >>self::$packingData[self::$archArrayElement]['factor']; + $r = $big & self::$packingData[self::$archArrayElement]['right']; + + // Create chunk + $chunk = str_pad(pack(self::$packingData[self::$archArrayElement]['format'], $l, $r), 8, '0', STR_PAD_LEFT); + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: big=%d,chunk(%d)=%s', $big, strlen($chunk), md5($chunk))); + + $packed .= $chunk; + } // END - for + + // Return it + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('STRING-UTILS: packed=%s - EXIT!', $packed)); + return $packed; + } + +} diff --git a/framework/main/exceptions/main/class_InvalidClassNameException.php b/framework/main/exceptions/main/class_InvalidClassNameException.php index 76d010ff..7d6dceec 100644 --- a/framework/main/exceptions/main/class_InvalidClassNameException.php +++ b/framework/main/exceptions/main/class_InvalidClassNameException.php @@ -1,2 +1,2 @@ - * @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.shipsimu.org - * @deprecated Don't use this anymore - * - * 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 . - */ -class MissingDecimalsThousandsSeparatorException extends FrameworkException { - /** - * The constructor - * - * @param $message Message from the exception - * @param $code Code number for the exception - * @return void - */ - public function __construct ($instance, $code) { - // Add a message around the missing class - $message = sprintf('[%s:%d] Decimal and thousand seperators are missing.', - $instance->__toString(), - $this->getLine() - ); - - // Call parent constructor - parent::__construct($message, $code); - } - -} +// @DEPRECATED diff --git a/framework/main/interfaces/class_FrameworkInterface.php b/framework/main/interfaces/class_FrameworkInterface.php index f14dbc03..5b744e15 100644 --- a/framework/main/interfaces/class_FrameworkInterface.php +++ b/framework/main/interfaces/class_FrameworkInterface.php @@ -34,7 +34,7 @@ interface FrameworkInterface { * @return $fieldValue Field value from the user * @throws NullPointerException If the result instance is null */ - function getField ($fieldName); + function getField (string $fieldName); /** * Checks if given field is set @@ -43,7 +43,7 @@ interface FrameworkInterface { * @return $isSet Whether the given field name is set * @throws NullPointerException If the result instance is null */ - function isFieldSet ($fieldName); + function isFieldSet (string $fieldName); /** * Setter for call-back instance