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