0f9ea835e08f41f6e8e1a81e47e910c9ac59d264
[shipsimu.git] / inc / classes / main / extended / class_ObjectLimits.php
1 <?php
2 /**
3  * This object limits other objects. This is mostly being used to prepare
4  * objects to the datatabase connection or else a lot heap would be saved.
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.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 ObjectLimits extends BaseFrameworkSystem {
26         /**
27          * Limitation array for storing all attribute names we will use later
28          * only.
29          */
30         private $limitArray = null;
31
32         /**
33          * Protected constructor
34          *
35          * @return      void
36          */
37         protected function __construct () {
38                 // Call parent constructor
39                 parent::__construct(__CLASS__);
40
41                 // Set part description
42                 $this->setObjectDescription("Class for &quot;limiting&quot; other classes. See description for details.");
43
44                 // Create unique ID number
45                 $this->generateUniqueId();
46
47                 // Clean up a little
48                 $this->removeNumberFormaters();
49         }
50
51         /**
52          * Create a new ObjectLimits object and (maybe prepare it a little)
53          *
54          * @param               $limitationArray        The limitation array we "walk" through
55          * @return      $limitInstance          The instance to an ObjectLimits object
56          */
57         public final static function createObjectLimits (array $limitationArray) {
58                 // Is there a limitation array given?
59                 if (count($limitationArray) > 0) {
60                         // Get instance
61                         $limitInstance = new ObjectLimits();
62
63                         // Get all limitations and do them
64                         foreach ($limitationArray as $limit) {
65                                 // What shall we limitate?
66                                 if ($limit instanceof FrameworkInterface) {
67                                         // Add an object
68                                         $limitInstance->addObject($limit);
69                                 } elseif (is_string($limit)) {
70                                         // Add a string
71                                         $limitInstance->addString($limit);
72                                 } else {
73                                         // Others are not supported (yet)
74                                         throw new UnsupportedLimitationPartException($limit, self::EXCEPTION_LIMIT_ELEMENT_IS_UNSUPPORTED);
75                                 }
76                         }
77
78                         // Return instance
79                         return $limitInstance;
80                 } else {
81                         // No limitation given so we send "null" back
82                         return null;
83                 }
84         }
85
86         /**
87          * Add an object's name to the limitation list
88          *
89          * @param               $object The object's name we shall add to the list
90          * @return      void
91          */
92         private final function addObject (FrameworkInterface $object) {
93                 // Auto-initialization
94                 if (is_null($this->limitArray)) {
95                         // Initialize this array
96                         $this->limitArray = new FrameworkArrayObject("FakedLimitArray");
97                 }
98
99                 // Add the object's name to it
100                 $this->limitArray->append($object->__toString());
101         }
102
103         /**
104          * Add a string directly to the limitation list
105          *
106          * @param               $str            The string we want to add directly
107          * @return      void
108          */
109         private final function addString ($str) {
110                 // Auto-initialization
111                 if (is_null($this->limitArray)) {
112                         // Initialize this array
113                         $this->limitArray = new FrameworkArrayObject("FakedLimitArray");
114                 }
115
116                 // Add the direct string to ArrayObject
117                 $this->limitArray->append($str);
118         }
119
120         /**
121          * Getter for limitArray
122          *
123          * @return      $limitArray     The object ArrayObject which holds limitations
124          */
125         public final function getLimitArray () {
126                 return $this->limitArray;
127         }
128 }
129
130 // [EOF]
131 ?>