All XML-parsing-related stuff refactured to new XmlParser class
[core.git] / inc / classes / main / helper / class_BaseHelper.php
index b4e893f4bcee531260a179f4cf0b54fde375cbcc..ff4646f749a7cb2d9e5318b3845e2eddd635bded 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -27,10 +27,15 @@ class BaseHelper extends BaseFrameworkSystem {
         */
        private $valueInstance = null;
 
+       /**
+        * Extra instance to the class which provides field values
+        */
+       private $extraInstance = null;
+
        /**
         * Rendered content created by the helper class
         */
-       private $content = "";
+       private $content = '';
 
        /**
         * Array with groups
@@ -45,12 +50,12 @@ class BaseHelper extends BaseFrameworkSystem {
        /**
         * Previously opened group
         */
-       private $previousGroupId = "";
+       private $previousGroupId = '';
 
        /**
         * Previously opened sub group
         */
-       private $previousSubGroupId = "";
+       private $previousSubGroupId = '';
 
        /**
         * Total counter for groups and sub groups
@@ -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) . "\n";
        }
 
        /**
@@ -177,6 +175,7 @@ class BaseHelper extends BaseFrameworkSystem {
        public function assignFieldWithFilter ($fieldName, $filterMethod) {
                // Get the value
                $fieldValue = $this->getValueField($fieldName);
+               //* DEBUG: */ $this->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));
@@ -194,24 +193,33 @@ class BaseHelper extends BaseFrameworkSystem {
         * @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;
+               //* DEBUG: */ $this->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!');
+               }
 
-                       // 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: */ $this->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!');
+               } // END - if
 
-                       // Get the requested instance
+               // Is the value instance valid?
+               if (is_null($this->valueInstance)) {
                        try {
-                               $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance));
-
+                               // 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);
@@ -244,7 +252,13 @@ class BaseHelper extends BaseFrameworkSystem {
                // Add the group to the stack
                $this->groups[$this->totalCounter] = $groupId;
                $this->groups[$groupId]['opened']  = true;
-               $this->groups[$groupId]['content'] = sprintf("<!-- group %s opened (length: %s, tag: %s) //-->%s\n", $groupId, strlen($content), $tag, $content);
+               $this->groups[$groupId]['content'] = sprintf(
+                       "<!-- group %s opened (length: %s, tag: %s) //-->%s\n",
+                       $groupId,
+                       strlen($content),
+                       $tag,
+                       $content
+               );
                $this->groups[$groupId]['tag'] = $tag;
 
                // Mark this group as previously opened
@@ -259,7 +273,7 @@ class BaseHelper extends BaseFrameworkSystem {
         * @return      void
         * @throws      HelperNoPreviousOpenedGroupException    If no previously opened group was found
         */
-       public function closePreviousGroupByContent ($content = "") {
+       public function closePreviousGroupByContent ($content = '') {
                // Check if any sub group was opened before
                if ($this->ifSubGroupOpenedPreviously()) {
                        // Close it automatically
@@ -278,15 +292,25 @@ class BaseHelper extends BaseFrameworkSystem {
                // Is the content empty?
                if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
                        // Get it from opener
-                       $content = sprintf("<!-- group %s auto-closed //--></%s>", $groupId, $this->groups[$groupId]['tag']);
+                       $content = sprintf(
+                               "<!-- group %s auto-closed //--></%s>",
+                               $groupId,
+                               $this->groups[$groupId]['tag']
+                       );
                } // END - if
 
                // Add content to it and mark it as closed
-               $this->groups[$groupId]['content'] .= sprintf("<!-- group %s closed (length: %s, tag: %s) //-->%s\n", $groupId, strlen($content), $this->groups[$groupId]['tag'], $content);
+               $this->groups[$groupId]['content'] .= sprintf(
+                       "<!-- group %s closed (length: %s, tag: %s) //-->%s\n",
+                       $groupId,
+                       strlen($content),
+                       $this->groups[$groupId]['tag'],
+                       $content
+               );
                $this->groups[$groupId]['opened'] = false;
 
                // Mark previous group as closed
-               $this->setPreviousGroupId("");
+               $this->setPreviousGroupId('');
                //* DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
        }
 
@@ -330,7 +354,7 @@ class BaseHelper extends BaseFrameworkSystem {
         * @return      void
         * @throws      HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
         */
-       public function closePreviousSubGroupByContent ($content = "") {
+       public function closePreviousSubGroupByContent ($content = '') {
                // Check if any sub group was opened before
                if ($this->ifSubGroupOpenedPreviously() === false) {
                        // Then throw an exception
@@ -351,7 +375,7 @@ class BaseHelper extends BaseFrameworkSystem {
                $this->subGroups[$subGroupId]['opened'] = false;
 
                // Mark previous sub group as closed
-               $this->setPreviousSubGroupId("");
+               $this->setPreviousSubGroupId('');
                //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
        }
 
@@ -362,12 +386,12 @@ class BaseHelper extends BaseFrameworkSystem {
         */
        public function renderContent () {
                // Initialize content
-               $content = "";
+               $content = '';
 
                // Is header content there?
                if (isset($this->groups['header'])) {
                        // Then add it
-                       $content .= $this->groups['header']['content']."\n";
+                       $content .= $this->groups['header']['content'] . "\n";
                } // END - if
 
                // Initiate content
@@ -375,7 +399,7 @@ 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?
+                       // 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']);
@@ -395,7 +419,7 @@ class BaseHelper extends BaseFrameworkSystem {
                // Is footer content there?
                if (isset($this->groups['footer'])) {
                        // Then add it
-                       $content .= $this->groups['footer']['content']."\n";
+                       $content .= $this->groups['footer']['content'] . "\n";
                } // END - if
 
                // Return it
@@ -422,10 +446,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: */ $this->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;