3 * The general simulator personell class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
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.
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.
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/>.
24 class SimulatorPersonell extends BasePersonell {
26 private $personellList = null;
29 private $cacheList = null;
31 // A string for cached conditions
32 private $cacheCond = null;
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
44 $this->setObjectDescription("Simulationspersonal");
47 $this->generateUniqueId();
50 $this->removeSystemArray();
54 * Magic wake-up method called when unserialize() is called. This is
55 * neccessary because in this case a personell does not need to know the
56 * min/max ages range and system classes. This would anyway use more RAM
57 * what is not required.
61 public function __wakeup () {
63 $this->removePersonellList();
64 $this->removeMinMaxAge();
66 $this->removeSystemArray();
70 * Generate a specified amount of personell and return the prepared instance
72 * @param $amountPersonell Number of personell we shall
74 * @return $personellInstance An instance of this object with a
77 public final static function createSimulatorPersonell ($amountPersonell) {
78 // Make sure only integer can pass
79 $amountPersonell = (int) $amountPersonell;
82 $personellInstance = new SimulatorPersonell();
85 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.",
91 // Initialize the personell list
92 $personellInstance->createPersonellList();
94 // Create requested amount of personell
95 for ($idx = 0; $idx < $amountPersonell; $idx++) {
96 $personellInstance->addRandomPersonell();
100 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.",
107 $personellInstance->removeGender();
108 $personellInstance->removeNames();
109 $personellInstance->removeBirthday();
110 $personellInstance->removeSalary();
111 $personellInstance->removeEmployed();
112 $personellInstance->removeMarried();
113 $personellInstance->removeNumberFormaters();
114 //$personellInstance->removeCache();
115 $personellInstance->removeSystemArray();
117 // Instanz zurueckgeben
118 return $personellInstance;
122 * Create a SimulatorPersonell object by loading the specified personell
123 * list from an existing database backend
125 * @param $idNumber The ID number (only right part) of the list
126 * @return $personellInstance An instance of
127 * @throws InvalidIDFormatException If the given id number
128 * $idNumber is invalid
129 * @throws NullPointerException If a null pointer (instance)
131 * @throws NoObjectException If a non-object has been
133 * @throws MissingMethodException If a required method is missing
134 * @throws MissingSimulatorIDException If an ID number was not found
136 public final static function createSimulatorPersonellByID ($idNumber) {
137 // Add the class name if it was not found
138 if (count(explode("@", $idNumber)) < 2) {
139 // Add class name in front of the incomplete ID number
140 $tempID = sprintf("%s@%s", __CLASS__, $idNumber);
142 // Use the direct ID number
146 // Validate the ID number
147 if (!preg_match(sprintf("/%s\@([a-f0-9]){32}/i", __CLASS__), $tempID)) {
149 throw new InvalidIDFormatException(new SimulatorPersonell(), self::EXCEPTION_ID_IS_INVALID_FORMAT);
153 $personellInstance = new SimulatorPersonell(false);
155 // Get database instance
156 $dbInstance = $personellInstance->getDatabaseInstance();
158 // Is this a valid database instance?
159 if (is_null($dbInstance)) {
161 throw new NullPointerException($personellInstance, self::EXCEPTION_IS_NULL_POINTER);
162 } elseif (!is_object($dbInstance)) {
163 // Not an object! ;-(
164 throw new NoObjectException($dbInstance, self::EXCEPTION_IS_NO_OBJECT);
165 } elseif (!method_exists($dbInstance, 'isUniqueIdUsed')) {
166 // Required method not found
167 throw new MissingMethodException(array($dbInstance, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD);
170 // Is the unique ID already used? Then it must be there!
171 if (!$dbInstance->isUniqueIdUsed($tempID)) {
173 throw new MissingSimulatorIDException(array($personellInstance, $idNumber), self::EXCEPTION_SIMULATOR_ID_INVALID);
176 // Load the personell list and add it to this object
177 $personellInstance->loadPersonellList($tempID);
180 $personellInstance->removeGender();
181 $personellInstance->removeNames();
182 $personellInstance->removeBirthday();
183 $personellInstance->removeSalary();
184 $personellInstance->removeEmployed();
185 $personellInstance->removeMarried();
186 $personellInstance->removeNumberFormaters();
187 //$personellInstance->removeCache();
188 $personellInstance->removeSystemArray();
191 return $personellInstance;
194 // Create personell list
195 public function createPersonellList () {
196 if (is_null($this->personellList)) {
197 $this->personellList = new FrameworkArrayObject("FakedPersonellList");
199 throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
203 // Remove the personell list
204 private function removePersonellList () {
205 unset($this->personellList);
208 // Add new personell object to our list
209 public function addRandomPersonell () {
211 $genders = array('M', 'F');
213 // Create new personell members
214 $personellInstance = new SimulatorPersonell();
216 // Set a randomized gender
217 $personellInstance->setGender($genders[mt_rand(0, 1)]);
219 // Set a randomized birthday (maximum age required, see const MAX_AGE)
220 $personellInstance->createBirthday();
222 // Married? Same values means: married
223 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
226 $personellInstance->removePersonellList();
227 $personellInstance->removeMinMaxAge();
228 $personellInstance->removeCache();
229 $personellInstance->removeSystemArray();
231 // Add new member to the list
232 $this->personellList->append($personellInstance);
236 * Get a specifyable list of our people, null or empty string will be ignored!
238 * @return $cacheList A list of cached personells
240 function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
241 // Serialize the conditions for checking if we can take the cache
242 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
244 // The same (last) conditions?
245 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
246 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
251 // Return cached list
252 return $this->cacheList;
255 // Output debug message
256 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
261 // Remember the conditions
262 $this->setCacheCond($serialized);
264 // Create cached list
265 $this->setAllCacheList(new FrameworkArrayObject("FakedCacheList"));
267 // Search all unemployed personells
268 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
270 $el = $idx->current();
272 // Check currenylt all single conditions (combined conditions are not yet supported)
273 if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
274 // Add this one (employed status asked)
275 $this->cacheList->append($el);
276 } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
277 // Add this one (marrital status asked)
278 $this->cacheList->append($el);
279 } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
280 // Add this one (specified gender)
281 $this->cacheList->append($el);
285 // Return the completed list
286 return $this->cacheList;
290 * Get amount of unemployed personell
292 * @return $count Amount of unemployed personell
294 public final function getAllUnemployed () {
295 // Get a temporary list
296 $list = $this->getSpecialPersonellList(false);
298 // Anzahl zurueckliefern
299 return $list->count();
303 * Remove cache things
307 private function removeCache () {
309 unset($this->cacheList);
310 unset($this->cacheCond);
314 * Setter for cache list
316 * @param $cacheList The new cache list to set or null for initialization/reset
319 private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
320 $this->cacheList = $cacheList;
324 * Setter for cache conditions
326 * @param $cacheCond The new cache conditions to set
329 private final function setCacheCond ($cacheCond) {
330 $this->cacheCond = (string) $cacheCond;
338 public function resetCache () {
339 $this->setAllCacheList(null);
340 $this->setCacheCond("");
344 * Getter for surname. If no surname is set then default surnames are set
345 * for male and female personells.
347 * @return $surname The personell' surname
349 public final function getSurname () {
350 $surname = parent::getSurname();
351 // Make sure every one has a surname...
352 if (empty($surname)) {
353 if ($this->isMale()) {
357 // Typical female name
358 $surname = "Jennifer";
361 // Set typical family name
362 parent::setFamily("Smith");
368 * Getter for personell list
370 * @return $personellList The list of all personells
372 public final function getPersonellList () {
373 return $this->personellList;
377 * Loads the mostly pre-cached personell list
379 * @param $idNumber The ID number we shall use for looking up
382 * @throws ContainerItemIsNullException If a container item is null
383 * @throws ContainerItemIsNoArrayException If a container item is
385 * @throws ContainerMaybeDamagedException If the container item
386 * is missing the indexes
387 * 'name' and/or 'value'
388 * @see SerializationContainer A special container class which
389 * helps storing only some attributes
392 public function loadPersonellList ($idNumber) {
393 // Cleared because old code
394 $this->partialStub("Clear because of old lost code was usaged.");