X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=inc%2Fclasses%2Fmain%2Fhelper%2Fclass_BaseHelper.php;h=c06f86fd6007ac4378984f4870c7e1be98f67cf4;hb=5203f9bd014ad46fbc7ee54e7223dcd46e14e3b4;hp=e8d757028ff96662ef753c88160293d02171c716;hpb=607a11e2c22949ea0647568c17d62a605595e83b;p=core.git diff --git a/inc/classes/main/helper/class_BaseHelper.php b/inc/classes/main/helper/class_BaseHelper.php index e8d75702..c06f86fd 100644 --- a/inc/classes/main/helper/class_BaseHelper.php +++ b/inc/classes/main/helper/class_BaseHelper.php @@ -2,11 +2,11 @@ /** * A generic helper class with generic methods * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @link http://www.shipsimu.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 @@ -87,7 +87,7 @@ class BaseHelper extends BaseFrameworkSystem { * @return void */ protected final function addContent ($newContent) { - $this->content .= (string) trim($newContent) . chr(10); + $this->content .= (string) trim($newContent) . PHP_EOL; } /** @@ -149,6 +149,16 @@ class BaseHelper extends BaseFrameworkSystem { return $this->content; } + /** + * Public setter for extra instance + * + * @param $extraInstance An extra instance of FrameworkInterface to set + * @return void + */ + public final function setExtraInstance (FrameworkInterface $extraInstance) { + $this->extraInstance = $extraInstance; + } + /** * Assigns a field from the value instance with a template variable * @@ -175,10 +185,10 @@ class BaseHelper extends BaseFrameworkSystem { public function assignFieldWithFilter ($fieldName, $filterMethod) { // Get the value $fieldValue = $this->getValueField($fieldName); - //* DEBUG: */ $this->debugOutput($fieldName.'='.$fieldValue); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'='.$fieldValue); // Now filter it through the value through the filter method - $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue)); + $filteredValue = call_user_func_array(array($this, 'doFilter' . self::convertToClassName($filterMethod)), array($fieldValue)); // Assign it with a template variable $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue); @@ -193,14 +203,14 @@ class BaseHelper extends BaseFrameworkSystem { * @throws NullPointerException If recovery of requested value instance failed */ public function prefetchValueInstance ($registryKey, $extraKey = NULL) { - //* DEBUG: */ $this->debugOutput('O:'.$registryKey.'/'.$extraKey); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('O:'.$registryKey.'/'.$extraKey); try { // Get the required instance $this->valueInstance = Registry::getRegistry()->getInstance($registryKey); } catch (NullPointerException $e) { // Not set in registry // @TODO Try to log it here - //* DEBUG: */ $this->debugOutput($registryKey.'=NULL!'); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($registryKey.'=NULL!'); } // Shall we get an extra instance? @@ -212,18 +222,13 @@ class BaseHelper extends BaseFrameworkSystem { // Try to create it $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', array($this->valueInstance)); } - //* DEBUG: */ $this->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!'); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!'); } // END - if // Is the value instance valid? if (is_null($this->valueInstance)) { - try { - // Get the requested instance - $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance)); - } catch (FrameworkException $e) { - // Okay, nothing found so throw a null pointer exception here - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } + // Get the requested instance + $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance)); } // END - if } @@ -251,9 +256,9 @@ class BaseHelper extends BaseFrameworkSystem { // Add the group to the stack $this->groups[$this->totalCounter] = $groupId; - $this->groups[$groupId]['opened'] = true; + $this->groups[$groupId]['opened'] = TRUE; $this->groups[$groupId]['content'] = sprintf( - "%s\n", + '%s' . PHP_EOL, $groupId, strlen($content), $tag, @@ -281,7 +286,7 @@ class BaseHelper extends BaseFrameworkSystem { } // END - if // Check if any group was opened before - if ($this->ifGroupOpenedPreviously() === false) { + if ($this->ifGroupOpenedPreviously() === FALSE) { // Then throw an exception throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED); } // END - if @@ -307,7 +312,7 @@ class BaseHelper extends BaseFrameworkSystem { $this->groups[$groupId]['tag'], $content ); - $this->groups[$groupId]['opened'] = false; + $this->groups[$groupId]['opened'] = FALSE; // Mark previous group as closed $this->setPreviousGroupId(''); @@ -338,7 +343,7 @@ class BaseHelper extends BaseFrameworkSystem { // Add the group to the stack $this->subGroups[$this->totalCounter] = $subGroupId; - $this->subGroups[$subGroupId]['opened'] = true; + $this->subGroups[$subGroupId]['opened'] = TRUE; $this->subGroups[$subGroupId]['content'] = sprintf("%s\n", $subGroupId, strlen($content), $tag, $content); $this->subGroups[$subGroupId]['tag'] = $tag; @@ -356,7 +361,7 @@ class BaseHelper extends BaseFrameworkSystem { */ public function closePreviousSubGroupByContent ($content = '') { // Check if any sub group was opened before - if ($this->ifSubGroupOpenedPreviously() === false) { + if ($this->ifSubGroupOpenedPreviously() === FALSE) { // Then throw an exception throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED); } // END - if @@ -367,12 +372,13 @@ class BaseHelper extends BaseFrameworkSystem { // Is the content empty? if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) { // Get it from opener - $content = sprintf("", $subGroupId, $this->subGroups[$subGroupId]['tag']); + $content = sprintf('', $subGroupId, $this->subGroups[$subGroupId]['tag']); } // END - if // Add content to it and mark it as closed - $this->subGroups[$subGroupId]['content'] .= sprintf("%s\n", $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content); - $this->subGroups[$subGroupId]['opened'] = false; + $this->subGroups[$subGroupId]['content'] .= sprintf('%s' . PHP_EOL, $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content); + $this->subGroups[$subGroupId]['opened'] = FALSE + ; // Mark previous sub group as closed $this->setPreviousSubGroupId(''); @@ -391,7 +397,7 @@ class BaseHelper extends BaseFrameworkSystem { // Is header content there? if (isset($this->groups['header'])) { // Then add it - $content .= $this->groups['header']['content'] . chr(10); + $content .= $this->groups['header']['content'] . PHP_EOL; } // END - if // Initiate content @@ -400,26 +406,26 @@ class BaseHelper extends BaseFrameworkSystem { // Now "walk" through all groups and sub-groups for ($idx = 1; $idx <= $this->totalCounter; $idx++) { // Is this a sub/group and is it closed? - if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) { + 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"; $content .= $groupContent; - } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) { + } 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"; $content .= trim($subGroupContent); } else { // Something went wrong - $this->debugInstance(__METHOD__."(): Something unexpected happened here."); + $this->debugInstance(__METHOD__ . '(): Something unexpected happened here.'); } } // END - for // Is footer content there? if (isset($this->groups['footer'])) { // Then add it - $content .= $this->groups['footer']['content'] . chr(10); + $content .= $this->groups['footer']['content'] . PHP_EOL; } // END - if // Return it @@ -435,7 +441,7 @@ class BaseHelper extends BaseFrameworkSystem { */ protected function ifGroupIsOpened ($groupId) { // Is the group open? - $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true)); + $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === TRUE)); // Return status return $isOpened; @@ -449,20 +455,27 @@ class BaseHelper extends BaseFrameworkSystem { * @throws NullPointerException Thrown if $valueInstance is null */ public function getValueField ($fieldName) { + // Init value + $fieldValue = NULL; + // The $valueInstance attribute should not be null! if (is_null($this->getValueInstance())) { // Throws an exception here throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } // END - if - // Get the field value - $fieldValue = $this->getValueInstance()->getField($fieldName); - //* DEBUG: */ $this->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).')'); - - // Is it null? - if ((is_null($fieldValue)) && (!is_null($this->extraInstance))) { + // Is the field set? + if ($this->getValueInstance()->isFieldSet($fieldName)) { + // Get the field value + $fieldValue = $this->getValueInstance()->getField($fieldName); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->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__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Extra instance!'); + } else { + // Field is not set + $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] fieldName=' . $fieldName . ' is not set! - @TODO'); } // END - if // Return it