TODO: We should find something better than BaseFrameworkSystem as a type-hint
[shipsimu.git] / ship-simu / inc / classes / main / extended / class_SerializationContainer.php
1 <?php
2 /**
3  * This class contains object attributes which we can now send together to
4  * other classes
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0
8  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
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 SerializationContainer extends FrameworkArrayObject {
25         /**
26          * Public constructor, if you like to have an object of this class...
27          *
28          * @return      void
29          */
30         public function __construct () {
31                 // Call parent constructor
32                 parent::constructor(__CLASS__);
33
34                 // Debug message
35                 if ((defined('DEBUG_CONTAINER')) && (defined('DEBUG_CONSTRUCT'))) $this->getDebugInstance()->output("[SerializationContainer:] Konstruktor erreicht.<br />\n");
36         }
37
38         /**
39          * Create a SerializationContainer object by applying the limitations
40          * in $limitInstance on $object. The resulting data container will only
41          * hold the attributed and their values which we have specified in
42          * the limitation object.
43          *
44          * @param               $limitInstance                  The instance to the object ObjectLimits
45          * @param               $object                         The origin object. We don't touch it here.
46          * @return      $containerInstance              An instance of SerializationContainer
47          * @throws      GetterNotFoundException If a getter was not found
48          */
49         public final static function createSerializationContainer (ObjectLimits $limitInstance, $object) {
50                 // Get an instance
51                 $containerInstance = new SerializationContainer();
52
53                 // Iterate through the whole limitation array
54                 for ($idx = $limitInstance->getLimitArray()->getIterator(); $idx->valid(); $idx->next()) {
55                         // Get current item from list
56                         $curr = ucfirst($idx->current());
57
58                         // Is the required method available?
59                         if (method_exists($object, sprintf("get%s", $curr))) {
60                                 // Generate a command for getting it
61                                 $eval = sprintf("\$value = \$object->get%s();",
62                                         $curr
63                                 );
64
65                                 // Debug eval command
66                                 if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $limitInstance->getDebugInstance()->output(sprintf("[%s:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
67                                         $this->__toString(),
68                                         htmlentities($eval)
69                                 ));
70
71                                 // Run it
72                                 @eval($eval);
73
74                                 // Add this item to the container list
75                                 $containerInstance->append(array(
76                                         'name'  => $curr,
77                                         'value' => $value
78                                 ));
79                         } else {
80                                 // Throw an exception
81                                 throw new GetterNotFoundException(array($object, $curr), self::EXCEPTION_GETTER_IS_MISSING);
82                         }
83                 } // END - for
84
85                 // Return container instance
86                 return $containerInstance;
87         }
88 }
89
90 // [EOF]
91 ?>