]> git.mxchange.org Git - hub.git/blob - application/hub/main/lists/class_BaseList.php
Exceptions, interfaces and many classes added/rewritten:
[hub.git] / application / hub / main / lists / class_BaseList.php
1 <?php
2 /**
3  * A general list class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
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 BaseList extends BaseHubSystem implements IteratorAggregate {
25         // Exception constants
26         const EXCEPTION_GROUP_ALREADY_ADDED = 0xf20;
27         const EXCEPTION_GROUP_NOT_FOUND     = 0xf21;
28
29         /**
30          * List groups array
31          */
32         private $listGroups = array();
33
34         /**
35          * List entries array
36          */
37         private $listEntries = array();
38
39         /**
40          * List index array
41          */
42         private $listIndex = array();
43
44         /**
45          * Protected constructor
46          *
47          * @param       $className      Name of the class
48          * @return      void
49          */
50         protected function __construct ($className) {
51                 // Call parent constructor
52                 parent::__construct($className);
53
54                 // Remove some attributes
55                 $this->removeNumberFormaters();
56                 $this->removeSystemArray();
57         }
58
59         /**
60          * Getter for iterator instance from this list
61          *
62          * @return      $iteratorInstance       An instance of a Iterator class
63          */
64         public function getIterator () {
65                 // Prepare a default iterator
66                 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('default_iterator_class');
67
68                 // And return it
69                 return $iteratorInstance;
70         }
71
72         /**
73          * Checks wether the given group is set
74          *
75          * @param       $groupName      Group to check if found in list
76          * @return      $isset          Wether the group is valid
77          */
78         public function isGroupSet ($groupName) {
79                 //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName);
80                 return isset($this->listGroups[$groupName]);
81         }
82
83         /**
84          * Adds the given group or if already added issues a ListGroupAlreadyAddedException
85          *
86          * @param       $groupName      Group to add
87          * @return      void
88          * @throws      ListGroupAlreadyAddedException  If the given group is already added
89          */
90         public function addGroup ($groupName) {
91                 //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - START');
92                 // Is the group already added?
93                 if ($this->isGroupSet($groupName)) {
94                         // Throw the exception here
95                         throw new ListGroupAlreadyAddedException(array($this, $groupName), self::EXCEPTION_GROUP_ALREADY_ADDED);
96                 } // END - if
97
98                 // Add the group which is a simple array
99                 $this->listGroups[$groupName] = ObjectFactory::createObjectByConfiguredName('list_group_class');
100                 //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - FINISHED');
101         }
102
103         /**
104          * Adds the given instance to list group and sub group
105          *
106          * @param       $groupName      Group to add instance to
107          * @param       $subGroup       Sub group to add instance to
108          * @param       $instance       An instance of Visitable
109          * @return      void
110          * @throws      NoListGroupException    If the given group is not found
111          */
112         public function addInstance ($groupName, $subGroup, Visitable $instance) {
113                 //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName  . '/' . $subGroup . ' - START');
114                 // Is the group there?
115                 if (!$this->isGroupSet($groupName)) {
116                         // Throw the exception here
117                         throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
118                 } // END - if
119
120                 // Is the sub group there?
121                 if (!$this->listGroups[$groupName]->isGroupSet($subGroup)) {
122                         // Automatically add it
123                         $this->listGroups[$groupName]->addGroup($subGroup);
124                 } // END - if
125
126                 // Generate the hash
127                 $hash = $this->generateHash($groupName, $subGroup, $instance);
128
129                 // Now add it to the group list and hash it
130                 $this->listGroups[$groupName]->addEntry($subGroup, $hash);
131
132                 // Add the hash to the index
133                 $this->listIndex[] = $hash;
134
135                 // Add the instance itself to the list
136                 $this->listEntries[$hash] = $instance;
137                 //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName  . '/' . $subGroup . ' - START');
138         }
139
140         /**
141          * Adds the given entry to list group
142          *
143          * @param       $groupName      Group to add instance to
144          * @param       $entry          An entry of any type
145          * @return      void
146          * @throws      NoListGroupException    If the given group is not found
147          */
148         public function addEntry ($groupName, $entry) {
149                 //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - START');
150                 // Is the group already added?
151                 if (!$this->isGroupSet($groupName)) {
152                         // Throw the exception here
153                         throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND);
154                 } // END - if
155
156                 // Generate hash
157                 $hash = $this->generateHash($groupName, $groupName, $entry);
158
159                 // Add the hash to the index
160                 $this->listIndex[] = $hash;
161
162                 // Now add the entry to the list
163                 $this->listEntries[$hash] = $entry;
164                 //* DEBUG: */ $this->debugOutput(__METHOD__.': '.$groupName . ' - FINISHED');
165         }
166
167         /**
168          * Generates a hash from given group, sub group and entry
169          *
170          * @param       $groupName      Group to add instance to
171          * @param       $subGroup       Sub group to add instance to
172          * @param       $entry          An entry of any type
173          * @return      $hash           The generated
174          */
175         private function generateHash ($groupName, $subGroup, $entry) {
176                 // Created entry, 'null' is default
177                 $entry2 = 'null';
178
179                 // Determine type of entry
180                 if (is_null($entry)) {
181                         // Ignore this
182                 } elseif ($entry instanceof FrameworkInterface) {
183                         // Own instance detected
184                         $entry2 = $entry->hashCode();
185                 } elseif (!is_array($entry)) {
186                         // Non-array found, use it directly with type
187                         $entry2 = gettype($entry) . ':' . $entry2;
188                 } else {
189                         // Arrays are unsupported at the momement
190                         $this->debugOutut(__METHOD__ . ': entry is an array. UNSUPPORTED!');
191
192                         // @TODO Extend this somehow?
193                         $entry2 = gettype($entry);
194                 }
195
196                 // Construct string which we shall hash
197                 $hashString = $groupName . ':' . $subGroup . ':' . $entry2;
198
199                 // Hash it with fastest hasher
200                 $hash = crc32($hashString);
201
202                 // And return it
203                 return $hash;
204         }
205 }
206
207 //
208 ?>