]> git.mxchange.org Git - hub.git/blob - application/hub/main/filter/class_BaseHubFilter.php
Moved communicatorInstance to 'core'.
[hub.git] / application / hub / main / filter / class_BaseHubFilter.php
1 <?php
2 /**
3  * A generic filter for hub project
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseHubFilter extends BaseFilter {
25         /**
26          * Array with all data XML nodes (which hold the actual data) and their values
27          */
28         protected $dataXmlNodes = array();
29
30         /**
31          * Protected constructor
32          *
33          * @param       $className      Real name of class
34          * @return      void
35          */
36         protected function __construct ($className) {
37                 // Call parent constructor
38                 parent::__construct($className);
39         }
40
41         /**
42          * Processes the given raw message content. The method renderXmlContent
43          * may throw (not the method itself) several exceptions:
44          *
45          * InvalidXmlNodeException  - If an invalid XML node has been found (e.g.
46          *                            wrong/out-dated template used)
47          * XmlNodeMismatchException - Again might be caused by invalid XML node
48          *                            usage
49          * XmlParserException       - If the XML message is damaged or not
50          *                            well-formed
51          *
52          * @param       $messageType            Type of message
53          * @param       $messageData            Raw message data array
54          * @param       $packageInstance        An instance of a Receivable class
55          * @return      void
56          * @todo        Exceptions from renderXmlContent() are currently unhandled
57          */
58         protected function genericProcessMessage ($messageType, array $messageData, Receivable $packageInstance) {
59                 // Make sure the wanted element is there
60                 assert(isset($messageData[NetworkPackage::PACKAGE_CONTENT_MESSAGE]));
61
62                 // Get a template instance from the factory
63                 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('node_' . $messageType . '_template_class');
64
65                 // Get message content
66                 $messageContent = $messageData[NetworkPackage::PACKAGE_CONTENT_MESSAGE];
67
68                 // And render the XML content (aka message)
69                 $templateInstance->renderXmlContent($messageContent);
70
71                 // Debug message
72                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: Handling ' . strlen($messageContent) . ' bytes: ' . $messageContent);
73
74                 /*
75                  * The template system now stores all required data as 'general'
76                  * variables, so simply get them. If there is an invalid XML node
77                  * inside the message, the above method call will cause exceptions.
78                  */
79                 foreach ($this->dataXmlNodes as $key => $dummy) {
80                         // Call it
81                         $value = $templateInstance->readXmlData($key);
82
83                         /*
84                          * If value is NULL, a variable hasn't been found. This could mean
85                          * that *this* node is running an out-dated software or the other
86                          * peer is using an out-dated $messageType.xml template.
87                          */
88                         if (is_null($value)) {
89                                 // Output a warning
90                                 self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: Found not fully supported variable ' . $key . ' - skipping.');
91
92                                 // Skip this part, don't write NULLs to the array
93                                 continue;
94                         } // END - if
95
96                         // Debug message
97                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: key=' . $key . ',value=' . $value);
98
99                         // Set it now
100                         $this->dataXmlNodes[$key] = $value;
101                 } // END - foreach
102
103                 // Construct an array for pushing it on next stack
104                 $messageArray = array(
105                         // Message data itself
106                         NetworkPackage::MESSAGE_ARRAY_DATA => $this->dataXmlNodes,
107                         // Message type (which is $messageType)
108                         NetworkPackage::MESSAGE_ARRAY_TYPE => $messageType
109                 );
110
111                 // Push the processed message back on stack
112                 $packageInstance->getStackInstance()->pushNamed(NetworkPackage::STACKER_NAME_PROCESSED_MESSAGE, $messageArray);
113         }
114 }
115
116 // [EOF]
117 ?>