]> git.mxchange.org Git - city.git/blob - application/city/main/filter/class_BaseCityFilter.php
Updated 'core'.
[city.git] / application / city / main / filter / class_BaseCityFilter.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) 2015 City 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 BaseCityFilter 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       $messageContent         Raw message content
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, $messageContent, Receivable $packageInstance) {
59                 // Get a template instance from the factory
60                 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('city_' . $messageType . '_template_class');
61
62                 // And render the XML content (aka message)
63                 $templateInstance->renderXmlContent($messageContent);
64
65                 // Debug message
66                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: Handling ' . strlen($messageContent) . ' bytes: ' . $messageContent);
67
68                 /*
69                  * The template system now stores all required data as 'general'
70                  * variables, so simply get them. If there is an invalid XML node
71                  * inside the message, the above method call will cause exceptions.
72                  */
73                 foreach ($this->dataXmlNodes as $key => $dummy) {
74                         // Call it
75                         $value = $templateInstance->readXmlData($key);
76
77                         /*
78                          * If value is NULL, a variable hasn't been found. This could mean
79                          * that *this* node is running an out-dated software or the other
80                          * peer is using an out-dated $messageType.xml template.
81                          */
82                         if (is_null($value)) {
83                                 // Output a warning
84                                 self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: Found not fully supported variable ' . $key . ' - skipping.');
85
86                                 // Skip this part, don't write NULLs to the array
87                                 continue;
88                         } // END - if
89
90                         // Debug message
91                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: key=' . $key . ',value=' . $value);
92
93                         // Set it now
94                         $this->dataXmlNodes[$key] = $value;
95                 } // END - foreach
96
97                 // Construct an array for pushing it on next stack
98                 $messageArray = array(
99                         // Message data itself
100                         NetworkPackage::MESSAGE_ARRAY_DATA => $this->dataXmlNodes,
101                         // Message type (which is $messageType)
102                         NetworkPackage::MESSAGE_ARRAY_TYPE => $messageType
103                 );
104
105                 // Push the processed message back on stack
106                 $packageInstance->getStackInstance()->pushNamed(NetworkPackage::STACKER_NAME_PROCESSED_MESSAGE, $messageArray);
107         }
108 }
109
110 // [EOF]
111 ?>