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->createUniqueID();
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.<br />\n",
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.<br />\n",
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 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personell-Liste erstellt.<br />\n",
201 $this->personellList = new FrameworkArrayObject();
203 throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
207 // Remove the personell list
208 private function removePersonellList () {
209 if (defined('DEBUG_PERSONELL')) $this->getDebugInstance()->output(sprintf("[%s:%d] Personell-Liste entfernt.<br />\n",
213 unset($this->personellList);
216 // Add new personell object to our list
217 public function addRandomPersonell () {
219 $genders = array('M', 'F');
221 // Create new personell members
222 $personellInstance = new SimulatorPersonell();
224 // Set a randomized gender
225 $personellInstance->setGender($genders[mt_rand(0, 1)]);
227 // Set a randomized birthday (maximum age required, see const MAX_AGE)
228 $personellInstance->createBirthday();
230 // Married? Same values means: married
231 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
234 $personellInstance->removePersonellList();
235 $personellInstance->removeMinMaxAge();
236 $personellInstance->removeCache();
237 $personellInstance->removeSystemArray();
239 // Add new member to the list
240 $this->personellList->append($personellInstance);
244 * Get a specifyable list of our people, null or empty string will be ignored!
246 * @return $cacheList A list of cached personells
248 function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
249 // Serialize the conditions for checking if we can take the cache
250 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
252 // The same (last) conditions?
253 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
254 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Gecachte Liste wird verwendet.<br />\n",
259 // Return cached list
260 return $this->cacheList;
263 // Output debug message
264 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...<br />\n",
269 // Remember the conditions
270 $this->setCacheCond($serialized);
272 // Create cached list
273 $this->setAllCacheList(new FrameworkArrayObject());
275 // Search all unemployed personells
276 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
278 $el = $idx->current();
280 // Check currenylt all single conditions (combined conditions are not yet supported)
281 if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
282 // Add this one (employed status asked)
283 $this->cacheList->append($el);
284 } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
285 // Add this one (marrital status asked)
286 $this->cacheList->append($el);
287 } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
288 // Add this one (specified gender)
289 $this->cacheList->append($el);
293 // Return the completed list
294 return $this->cacheList;
298 * Get amount of unemployed personell
300 * @return $count Amount of unemployed personell
302 public final function getAllUnemployed () {
303 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Es werden alle erwerbslosen Personen gesucht.<br />\n",
308 // Get a temporary list
309 $list = $this->getSpecialPersonellList(false);
311 // Anzahl zurueckliefern
312 return $list->count();
316 * Remove cache things
320 private function removeCache () {
321 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Cache-Attribute entfernt.<br />\n",
327 unset($this->cacheList);
328 unset($this->cacheCond);
332 * Setter for cache list
334 * @param $cacheList The new cache list to set or null for initialization/reset
337 private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
338 $this->cacheList = $cacheList;
342 * Setter for cache conditions
344 * @param $cacheCond The new cache conditions to set
347 private final function setCacheCond ($cacheCond) {
348 $this->cacheCond = (string) $cacheCond;
356 public function resetCache () {
357 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Cache-Liste zurückgesetzt.<br />\n",
361 $this->setAllCacheList(null);
362 $this->setCacheCond("");
366 * Getter for surname. If no surname is set then default surnames are set
367 * for male and female personells.
369 * @return $surname The personell' surname
371 public final function getSurname () {
372 $surname = parent::getSurname();
373 // Make sure every one has a surname...
374 if (empty($surname)) {
375 if ($this->isMale()) {
379 // Typical female name
380 $surname = "Jennifer";
383 // Set typical family name
384 parent::setFamily("Smith");
390 * Saves only the personell list to the database
394 public function saveObjectToDatabase () {
396 $dbInstance = $this->getDatabaseInstance();
398 // Prepare the limitation object. We just need the personellList array object.
399 $limitInstance = ObjectLimits::createObjectLimits(array("personellList"));
401 // Limitate the saving amount
402 $dbInstance->limitObject($limitInstance);
405 $dbInstance->saveObject($this);
409 * Getter for personell list
411 * @return $personellList The list of all personells
413 public final function getPersonellList () {
414 return $this->personellList;
418 * Loads the mostly pre-cached personell list
420 * @param $idNumber The ID number we shall use for looking up
423 * @throws ContainerItemIsNullException If a container item is null
424 * @throws ContainerItemIsNoArrayException If a container item is
426 * @throws ContainerMaybeDamagedException If the container item
427 * is missing the indexes
428 * 'name' and/or 'value'
429 * @see SerializationContainer A special container class which
430 * helps storing only some attributes
433 public function loadPersonellList ($idNumber) {
434 // Get database instance
435 $dbInstance = $this->getDatabaseInstance();
437 // Get the serialization container within the personell list from
438 // the database layer
439 $containerInstance = $dbInstance->getObjectFromCachedData($idNumber);
441 // Iterate through the whole container
442 for ($idx = $containerInstance->getIterator(); $idx->valid(); $idx->next()) {
443 // Get current item from container
444 $item = $idx->current();
447 if (is_null($item)) {
449 throw new ContainerItemIsNullException($this, self::EXCEPTION_CONTAINER_ITEM_IS_NULL);
450 } elseif (!is_array($item)) {
452 throw new ContainerItemIsNoArrayException($this, self::EXCEPTION_ITEM_IS_NO_ARRAY);
453 } elseif ((!isset($item['name'])) || (!isset($item['value']))) {
455 throw new ContainerMaybeDamagedException($this, self::EXCEPTION_CONTAINER_MAYBE_DAMAGED);
458 // Okay, now we can get the item and generate a valid command for eval().
459 // We need to convert the first letter to lower-case but keep all others intact
460 $eval = sprintf("\$this->%s = \$item['value'];",
461 strtolower(substr($item['name'], 0, 1))
463 substr($item['name'], 1)
467 if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",