From: Roland Häder Date: Fri, 17 Feb 2023 06:49:38 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=262f905be8d6f9dc9fd02acc9505cb45ef2a53f6 Continued: - HelperGroupAlreadyCreatedException is redundant to BadMethodCallException - added a lot more debug lines --- diff --git a/framework/main/classes/helper/class_BaseHelper.php b/framework/main/classes/helper/class_BaseHelper.php index 451cab9c..413655db 100644 --- a/framework/main/classes/helper/class_BaseHelper.php +++ b/framework/main/classes/helper/class_BaseHelper.php @@ -10,6 +10,10 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils; +// Import SPL stuff +use \BasMethodCallException; +use \InvalidArgumentException; + /** * A generic helper class with generic methods * @@ -130,8 +134,16 @@ abstract class BaseHelper extends BaseFrameworkSystem { * * @param $newContent New content to add * @return void + * @throws InvalidArgumentException If a parameter has an invalid value */ protected final function addContentToPreviousGroup (string $newContent) { + // Check on parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: newContent=%s - CALLED!', $newContent)); + if (empty($newContent)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "newContent" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // Check for sub/group if ($this->ifSubGroupOpenedPreviously()) { // Get sub group id @@ -149,6 +161,9 @@ abstract class BaseHelper extends BaseFrameworkSystem { // Add it directly $this->addContent($newContent); } + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-HELPER: EXIT!'); } /** @@ -175,13 +190,24 @@ abstract class BaseHelper extends BaseFrameworkSystem { * * @param $fieldName Name of the field to assign * @return void + * @throws InvalidArgumentException If a parameter has an invalid value */ public function assignField (string $fieldName) { + // Check on parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: fieldName=%s - CALLED!', $fieldName)); + if (empty($fieldName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "fieldName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // Get the value from value instance $fieldValue = $this->getValueField($fieldName); // Assign it with a template variable $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-HELPER: EXIT!'); } /** @@ -191,18 +217,33 @@ abstract class BaseHelper extends BaseFrameworkSystem { * @param $fieldName Name of the field to assign * @param $filterMethod Method name to call of the value instance * @return void + * @throws InvalidArgumentException If a parameter has an invalid value * @todo Rewrite this method using a helper class for filtering data */ public function assignFieldWithFilter (string $fieldName, string $filterMethod) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: fieldName=%s,filterMethod=%s - CALLED!', $fieldName, $filterMethod)); + if (empty($fieldName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "fieldName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (empty($filterMethod)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "filterMethod" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // Get the value $fieldValue = $this->getValueField($fieldName); - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'='.$fieldValue); // Now filter it through the value through the filter method + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: %s[%s]=%s', $fieldName, gettype($fieldValue), $fieldValue)); $filteredValue = call_user_func_array(array($this, 'doFilter' . StringUtils::convertToClassName($filterMethod)), array($fieldValue)); // Assign it with a template variable + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: filteredValue[%s]=%s', gettype($filteredValue), $filteredValue)); $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-HELPER: EXIT!'); } /** @@ -211,11 +252,11 @@ abstract class BaseHelper extends BaseFrameworkSystem { * @param $registryKey Registry key which holds an object with values * @param $extraKey Extra value instance key used if registryKey is null * @return void - * @throws NullPointerException If recovery of requested value instance failed + * @throws InvalidArgumentException If a parameter has an invalid value */ public function prefetchValueInstance (string $registryKey, string $extraKey = NULL) { // Validate parameter - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('O:'.$registryKey.'/'.$extraKey); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: registryKey=%s,extraKey[%s]=%s - CALLED!', $registryKey, gettype($extraKey), $extraKey)); if (empty($registryKey)) { // Throw IAE throw new InvalidArgumentException('Parameter "registryKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); @@ -226,27 +267,32 @@ abstract class BaseHelper extends BaseFrameworkSystem { $this->valueInstance = GenericRegistry::getRegistry()->getInstance($registryKey); } catch (NullPointerException $e) { // Not set in registry - // @TODO Try to log it here - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($registryKey.'=NULL!'); + //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: registryKey=%s returned no instance (NPE thrown)', $registryKey)); } // Shall we get an extra instance? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: extraKey[%s]=%s', gettype($extraKey), $extraKey)); if (!is_null($extraKey)) { try { // Get the extra instance. $this->extraInstance = GenericRegistry::getRegistry()->getInstance($extraKey); } catch (NullPointerException $e) { // Try to create it - $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', array($this->valueInstance)); + //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: extraKey=%s returned no instance (NPE thrown), this->valueInstance[]=%s', $extraKey, gettype($this->valueInstance))); + $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', [$this->valueInstance]); } - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!'); + //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: extraKey=%s,this->extraInstance[%s]=%s', $extraKey, gettype($this->extraInstance), $this->extraInstance)); } // Is the value instance valid? + //* NOSIY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: this->valueInstance[]=%s - BEFORE!', gettype($this->valueInstance))); if (is_null($this->valueInstance)) { // Get the requested instance $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance)); } + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: this->valueInstance[]=%s - AFTER/EXIT!', gettype($this->valueInstance))); } /** @@ -258,23 +304,31 @@ abstract class BaseHelper extends BaseFrameworkSystem { * @param $content Initial content to add to the group * @param $tag HTML tag used to open this group * @return void - * @throws HelperGroupAlreadyCreatedException If the group was already created before + * @throws InvalidArgumentException If a parameter has an invalid value + * @throws BadMethodCallException If the group was already created before */ protected function openGroupByIdContent (string $groupId, string $content, string $tag) { // Is the group already there? - //* DEBUG: */ echo "OPEN:groupId={$groupId},content=
".htmlentities($content)."
\n"; + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: groupdId=%s,content=%s,tag=%s - CALLED!', $groupdId, $content, $tag)); if (empty($groupId)) { // Throw IAE throw new InvalidArgumentException('Parameter "groupId" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (isset($this->groups[$groupId])) { // Then throw an exception here - throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND); + throw new BadMethodCallException(sprintf('[%s:%d]: groupId=%s is already opened.', $this->__toString(), $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND); + } elseif (empty($content)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "content" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (empty($tag)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "tag" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Count one up $this->totalCounter++; // Add the group to the stack + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: this->totalCounter=%d', $this->totalCounter)); $this->groups[$this->totalCounter] = $groupId; $this->groups[$groupId]['opened'] = true; $this->groups[$groupId]['content'] = sprintf( @@ -287,7 +341,11 @@ abstract class BaseHelper extends BaseFrameworkSystem { $this->groups[$groupId]['tag'] = $tag; // Mark this group as previously opened + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: Setting this->previousGroupId=%s ...', $groupId)); $this->setPreviousGroupId($groupId); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-HELPER: EXIT!'); } /** @@ -300,6 +358,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { */ public function closePreviousGroupByContent (string $content = '') { // Check if any sub group was opened before + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: content(%d)=%s - CALLED!', strlen($content), $content)); if ($this->ifSubGroupOpenedPreviously()) { // Close it automatically $this->closePreviousSubGroupByContent(); @@ -336,7 +395,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { // Mark previous group as closed $this->setPreviousGroupId(''); - //* DEBUG: */ echo "CLOSE:groupId={$groupId}
\n"; + //* NOISY-DEBUG: */ echo "CLOSE:groupId={$groupId}
\n"; } /** @@ -351,7 +410,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before */ protected function openSubGroupByIdContent (string $subGroupId, string $content, string $tag) { - //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."
\n"; + //* NOISY-DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."
\n"; // Is the group already there? if (empty($subGroupId)) { // Throw IAE @@ -405,7 +464,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { // Mark previous sub group as closed $this->setPreviousSubGroupId(''); - //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}
\n"; + //* NOISY-DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}
\n"; } /** @@ -432,12 +491,12 @@ abstract class BaseHelper extends BaseFrameworkSystem { if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) { // Then add it's content $groupContent = trim($this->groups[$this->groups[$idx]]['content']); - //* DEBUG: */ echo "group={$this->groups[$idx]},content=
".htmlentities($groupContent)."

\n"; + //* NOISY-DEBUG: */ echo "group={$this->groups[$idx]},content=
".htmlentities($groupContent)."

\n"; $content .= $groupContent; } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) { // Then add it's content $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content']; - //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=
".htmlentities($subGroupContent)."

\n"; + //* NOISY-DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=
".htmlentities($subGroupContent)."

\n"; $content .= trim($subGroupContent); } else { // Something went wrong @@ -452,7 +511,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { } // Return it - //* DEBUG: */ echo "content=
".htmlentities($content)."
(".strlen($content).")
\n"; + //* NOISY-DEBUG: */ echo "content=
".htmlentities($content)."
(".strlen($content).")
\n"; return $content; } @@ -461,12 +520,21 @@ abstract class BaseHelper extends BaseFrameworkSystem { * * @param $groupId Id of group to check * @return $isOpened Whether the specified group is open + * @throws InvalidArgumentException If a parameter has an invalid value */ protected function ifGroupIsOpened (string $groupId) { + // Check on parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: groupId=%s - CALLED!', $groupId)); + if (empty($groupId)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupId" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // Is the group open? $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true)); // Return status + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: isOpened=%d - EXIT!', intval($isOpened))); return $isOpened; } @@ -475,9 +543,17 @@ abstract class BaseHelper extends BaseFrameworkSystem { * * @param $fieldName Name of the field we shall fetch * @return $fieldValue Value from field + * @throws InvalidArgumentException If a parameter has an invalid value * @throws NullPointerException Thrown if $valueInstance is null */ public function getValueField (string $fieldName) { + // Check parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: fieldName=%s - CALLED!', $fieldName)); + if (empty($fieldName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "fieldName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // Init value $fieldValue = NULL; @@ -491,17 +567,18 @@ abstract class BaseHelper extends BaseFrameworkSystem { if ($this->getValueInstance()->isFieldSet($fieldName)) { // Get the field value $fieldValue = $this->getValueInstance()->getField($fieldName); - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Value instance!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Value instance!'); } elseif ((!is_null($this->extraInstance)) && ($this->extraInstance->isFieldSet($fieldName))) { // So try the extra instance $fieldValue = $this->extraInstance->getField($fieldName); - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Extra instance!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Extra instance!'); } else { // Field is not set $this->debugOutput('BASE-HELPER: fieldName=' . $fieldName . ' is not set! - @TODO'); } // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: fieldValue[%s]=%s - EXIT!', gettype($fieldValue), $fieldValue)); return $fieldValue; } diff --git a/framework/main/exceptions/helper/class_HelperGroupAlreadyCreatedException.php b/framework/main/exceptions/helper/class_HelperGroupAlreadyCreatedException.php index 56463368..7d6dceec 100644 --- a/framework/main/exceptions/helper/class_HelperGroupAlreadyCreatedException.php +++ b/framework/main/exceptions/helper/class_HelperGroupAlreadyCreatedException.php @@ -1,51 +1,2 @@ - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 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 HelperGroupAlreadyCreatedException extends FrameworkException { - /** - * The constructor - * - * @param $messageArray Array containing exception data - * @param $code Code number for the exception - * @return void - */ - public function __construct (array $messageArray, int $code) { - // Add a message around the missing class - $message = sprintf('[%s:%d] Group %s already opened before.', - $messageArray[0]->__toString(), - $this->getLine(), - $messageArray[1] - ); - - // Call parent constructor - parent::__construct($message, $code); - } - -} +// @DEPRECATED