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