Throwing an NPE here hides the actual exception and it is much harder to track what...
[core.git] / inc / classes / main / helper / class_BaseHelper.php
index 89a3babe5e27c6d7a6b6934053f11938499bf85d..8011081af8da8de89aab160e93e5a3de76eb160d 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * A generic helper class with generic methods
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007 - 2009 Roland Haeder, this is free software
+ * @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
@@ -25,7 +25,12 @@ class BaseHelper extends BaseFrameworkSystem {
        /**
         * Instance to the class which provides field values
         */
-       private $valueInstance = null;
+       private $valueInstance = NULL;
+
+       /**
+        * Extra instance to the class which provides field values
+        */
+       private $extraInstance = NULL;
 
        /**
         * Rendered content created by the helper class
@@ -58,9 +63,6 @@ class BaseHelper extends BaseFrameworkSystem {
        private $totalCounter = 0;
 
        // Exception constants
-       const EXCEPTION_XML_PARSER_ERROR             = 0x1e0;
-       const EXCEPTION_XML_NODE_UNKNOWN             = 0x1e1;
-       const EXCEPTION_XML_NODE_MISMATCH            = 0x1e2;
        const EXCEPTION_GROUP_NOT_OPENED             = 0x1e3;
        const EXCEPTION_GROUP_ALREADY_FOUND          = 0x1e4;
        const EXCEPTION_SUB_GROUP_ALREADY_FOUND      = 0x1e5;
@@ -76,10 +78,6 @@ class BaseHelper extends BaseFrameworkSystem {
        protected function __construct ($className) {
                // Call parent constructor
                parent::__construct($className);
-
-               // Clean up a little
-               $this->removeNumberFormaters();
-               $this->removeSystemArray();
        }
 
        /**
@@ -89,7 +87,7 @@ class BaseHelper extends BaseFrameworkSystem {
         * @return      void
         */
        protected final function addContent ($newContent) {
-               $this->content .= (string) trim($newContent) . "\n";
+               $this->content .= (string) trim($newContent) . PHP_EOL;
        }
 
        /**
@@ -177,6 +175,7 @@ class BaseHelper extends BaseFrameworkSystem {
        public function assignFieldWithFilter ($fieldName, $filterMethod) {
                // Get the value
                $fieldValue = $this->getValueField($fieldName);
+               //* 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));
@@ -193,29 +192,33 @@ class BaseHelper extends BaseFrameworkSystem {
         * @return      void
         * @throws      NullPointerException    If recovery of requested value instance failed
         */
-       public function prefetchValueInstance ($registryKey, $extraKey = null) {
-               // Get the required instance
-               $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
-
-               // Is the value instance valid?
-               if (is_null($this->valueInstance)) {
-                       // Try to create it "from scratch", by first init extra instance
-                       $extraInstance = null;
+       public function prefetchValueInstance ($registryKey, $extraKey = NULL) {
+               //* 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: */ self::createDebugInstance(__CLASS__)->debugOutput($registryKey.'=NULL!');
+               }
 
-                       // Shall we get an extra instance?
-                       if (!is_null($extraKey)) {
+               // Shall we get an extra instance?
+               if (!is_null($extraKey)) {
+                       try {
                                // Get the extra instance.
-                               $extraInstance = Registry::getRegistry()->getInstance($extraKey);
-                       } // END - if
+                               $this->extraInstance = Registry::getRegistry()->getInstance($extraKey);
+                       } catch (NullPointerException $e) {
+                               // Try to create it
+                               $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', array($this->valueInstance));
+                       }
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!');
+               } // END - if
 
+               // Is the value instance valid?
+               if (is_null($this->valueInstance)) {
                        // Get the requested instance
-                       try {
-                               $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
-
-                       } catch (FrameworkException $e) {
-                               // Okay, nothing found so throw a null pointer exception here
-                               throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-                       }
+                       $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance));
                } // END - if
        }
 
@@ -243,9 +246,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(
-                       "<!-- group %s opened (length: %s, tag: %s) //-->%s\n",
+                       '<!-- group %s opened (length: %s, tag: %s) //-->%s' . PHP_EOL,
                        $groupId,
                        strlen($content),
                        $tag,
@@ -273,7 +276,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
@@ -299,7 +302,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('');
@@ -330,7 +333,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("<!-- sub-group %s opened (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $tag, $content);
                $this->subGroups[$subGroupId]['tag'] = $tag;
 
@@ -348,7 +351,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
@@ -359,12 +362,13 @@ class BaseHelper extends BaseFrameworkSystem {
                // Is the content empty?
                if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
                        // Get it from opener
-                       $content = sprintf("<!-- sub-group %s auto-closed //--></%s>", $subGroupId, $this->subGroups[$subGroupId]['tag']);
+                       $content = sprintf('<!-- sub-group %s auto-closed //--></%s>', $subGroupId, $this->subGroups[$subGroupId]['tag']);
                } // END - if
 
                // Add content to it and mark it as closed
-               $this->subGroups[$subGroupId]['content'] .= sprintf("<!-- sub-group %s closed (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
-               $this->subGroups[$subGroupId]['opened'] = false;
+               $this->subGroups[$subGroupId]['content'] .= sprintf('<!-- sub-group %s closed (length: %s, tag: %s) //-->%s' . PHP_EOL, $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
+               $this->subGroups[$subGroupId]['opened'] = FALSE
+               ;
 
                // Mark previous sub group as closed
                $this->setPreviousSubGroupId('');
@@ -383,7 +387,7 @@ class BaseHelper extends BaseFrameworkSystem {
                // Is header content there?
                if (isset($this->groups['header'])) {
                        // Then add it
-                       $content .= $this->groups['header']['content'] . "\n";
+                       $content .= $this->groups['header']['content'] . PHP_EOL;
                } // END - if
 
                // Initiate content
@@ -391,27 +395,27 @@ class BaseHelper extends BaseFrameworkSystem {
 
                // Now "walk" through all groups and sub-groups
                for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
-                       // Is this a group and is it closed?
-                       if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
+                       // Is this a sub/group and is it closed?
+                       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=<pre>".htmlentities($groupContent)."</pre><br />\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=<pre>".htmlentities($subGroupContent)."</pre><br />\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'] . "\n";
+                       $content .= $this->groups['footer']['content'] . PHP_EOL;
                } // END - if
 
                // Return it
@@ -420,14 +424,14 @@ class BaseHelper extends BaseFrameworkSystem {
        }
 
        /**
-        * Checks wether the specified group is opened
+        * Checks whether the specified group is opened
         *
         * @param       $groupId        Id of group to check
-        * @return      $isOpened       Wether the specified group is open
+        * @return      $isOpened       Whether the specified group is open
         */
        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;
@@ -438,10 +442,24 @@ class BaseHelper extends BaseFrameworkSystem {
         *
         * @param       $fieldName              Name of the field we shall fetch
         * @return      $fieldValue             Value from field
+        * @throws      NullPointerException    Thrown if $valueInstance is null
         */
        public function getValueField ($fieldName) {
+               // 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 = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
+               $fieldValue = $this->getValueInstance()->getField($fieldName);
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).')');
+
+               // Is it null?
+               if ((is_null($fieldValue)) && (!is_null($this->extraInstance))) {
+                       // So try the extra instance
+                       $fieldValue = $this->extraInstance->getField($fieldName);
+               } // END - if
 
                // Return it
                return $fieldValue;
@@ -457,9 +475,9 @@ class BaseHelper extends BaseFrameworkSystem {
        }
 
        /**
-        * Check wether a group was opened previously
+        * Check whether a group was opened previously
         *
-        * @return      $groupOpened    Wether any group was opened before
+        * @return      $groupOpened    Whether any group was opened before
         */
        protected final function ifGroupOpenedPreviously () {
                $groupOpened = (!empty($this->previousGroupId));
@@ -467,9 +485,9 @@ class BaseHelper extends BaseFrameworkSystem {
        }
 
        /**
-        * Check wether a group was opened previously
+        * Check whether a group was opened previously
         *
-        * @return      $subGroupOpened         Wether any group was opened before
+        * @return      $subGroupOpened         Whether any group was opened before
         */
        protected final function ifSubGroupOpenedPreviously () {
                $subGroupOpened = (!empty($this->previousSubGroupId));