3 namespace Org\Mxchange\City\Filter;
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;
11 * A generic filter for hub project
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2015 - 2023 City Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
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.
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.
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/>.
32 abstract class BaseCityFilter extends BaseFilter {
34 * Array with all data XML nodes (which hold the actual data) and their values
36 protected $dataXmlNodes = array();
39 * Protected constructor
41 * @param $className Real name of class
44 protected function __construct (string $className) {
45 // Call parent constructor
46 parent::__construct($className);
50 * Processes the given raw message content. The method renderXmlContent
51 * may throw (not the method itself) several exceptions:
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
57 * XmlParserException - If the XML message is damaged or not
60 * @param $messageType Type of message
61 * @param $messageContent Raw message content
62 * @param $packageInstance An instance of a Receivable class
64 * @todo Exceptions from renderXmlContent() are currently unhandled
66 protected function genericProcessMessage (string $messageType, string $messageContent, Receivable $packageInstance) {
67 // Get a template instance from the factory
68 $templateInstance = XmlTemplateEngineFactory::createXmlTemplateEngineInstance('city_' . $messageType . '_template_class');
70 // And render the XML content (aka message)
71 $templateInstance->renderXmlContent($messageContent);
74 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: Handling ' . strlen($messageContent) . ' bytes: ' . $messageContent);
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.
81 foreach ($this->dataXmlNodes as $key => $dummy) {
83 $value = $templateInstance->readXmlData($key);
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.
90 if (is_null($value)) {
92 self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: Found not fully supported variable ' . $key . ' - skipping.');
94 // Skip this part, don't write NULLs to the array
99 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(str_replace('_', '-', strtoupper($messageType)) . '-TAG: key=' . $key . ',value=' . $value);
102 $this->dataXmlNodes[$key] = $value;
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
113 // Push the processed message back on stack
114 $packageInstance->getStackInstance()->pushNamed(NetworkPackage::STACKER_NAME_PROCESSED_MESSAGE, $messageArray);