7924d4c92e38cde0c25c68bea0ef53057e4ee0e7
[mailer.git] / 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@mxchange.org>
7  * @version             0.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  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class SerializationContainer extends FrameworkArrayObject {
26         /**
27          * Public constructor, if you like to have an object of this class...
28          *
29          * @return      void
30          */
31         public function __construct () {
32                 // Call parent constructor
33                 parent::__construct(__CLASS__);
34         }
35
36         /**
37          * Create a SerializationContainer object by applying the limitations
38          * in $limitInstance on $object. The resulting data container will only
39          * hold the attributed and their values which we have specified in
40          * the limitation object.
41          *
42          * @param               $limitInstance                  The instance to the object ObjectLimits
43          * @param               $object                         The origin object. We don't touch it here.
44          * @return      $containerInstance              An instance of SerializationContainer
45          * @throws      GetterNotFoundException If a getter was not found
46          */
47         public final static function createSerializationContainer (ObjectLimits $limitInstance, FrameworkInterface $object) {
48                 // Get an instance
49                 $containerInstance = new SerializationContainer();
50
51                 // Iterate through the whole limitation array
52                 for ($idx = $limitInstance->getLimitArray()->getIterator(); $idx->valid(); $idx->next()) {
53                         // Get current item from list
54                         $curr = ucfirst($idx->current());
55
56                         // Is the required method available?
57                         if (method_exists($object, sprintf("get%s", $curr))) {
58                                 // Generate a command for getting it
59                                 $eval = sprintf("\$value = \$object->get%s();",
60                                         $curr
61                                 );
62
63                                 // Debug eval command
64                                 if (defined('DEBUG_EVAL')) $limitInstance->getDebugInstance()->output(sprintf("[%s:] Constructed PHP command: <pre><em>%s</em></pre><br />\n",
65                                         $this->__toString(),
66                                         htmlentities($eval)
67                                 ));
68
69                                 // Run it
70                                 eval($eval);
71
72                                 // Add this item to the container list
73                                 $containerInstance->append(array(
74                                         'name'  => $curr,
75                                         'value' => $value
76                                 ));
77                         } else {
78                                 // Throw an exception
79                                 throw new GetterNotFoundException(array($object, $curr), self::EXCEPTION_GETTER_IS_MISSING);
80                         }
81                 } // END - for
82
83                 // Return container instance
84                 return $containerInstance;
85         }
86 }
87
88 // [EOF]
89 ?>