3 // Die Schiffsbesatzung
4 class SimulatorPersonell extends BasePersonell {
6 private $personellList = null;
9 private $cacheList = null;
11 // A string for cached conditions
12 private $cacheCond = null;
19 private function __construct () {
20 // Eltern-Konstruktor aufrufen
21 parent::constructor(__CLASS__);
23 if (((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) $this->getDebugInstance()->output(sprintf("[%s:] Konstruktor erreicht.<br />\n",
28 $this->setPartDescr("Simulationspersonal");
31 $this->createUniqueID();
34 $this->removeSystemArray();
38 * Magic wake-up method called when unserialize() is called. This is
39 * neccessary because in this case a personell does not need to know the
40 * min/max ages range and system classes. This would anyway use more RAM
41 * what is not required.
45 public function __wakeup () {
47 $this->removePersonellList();
48 $this->removeMinMaxAge();
50 $this->removeSystemArray();
54 * Generate a specified amount of personell and return the prepared instance
56 * @param $amountPersonell Number of personell we shall
58 * @return $personellInstance An instance of this object with a
61 public static function createSimulatorPersonell ($amountPersonell) {
62 // Make sure only integer can pass
63 $amountPersonell = (int) $amountPersonell;
66 $personellInstance = new SimulatorPersonell();
68 // Generate unique ID number
69 $personellInstance->createUniqueID();
72 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.<br />\n",
78 // Initialize the personell list
79 $personellInstance->createPersonellList();
81 // Create requested amount of personell
82 for ($idx = 0; $idx < $amountPersonell; $idx++) {
83 $personellInstance->addPersonell();
87 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.<br />\n",
94 $personellInstance->removeGender();
95 $personellInstance->removeNames();
96 $personellInstance->removeBirthday();
97 $personellInstance->removeSalary();
98 $personellInstance->removeEmployed();
99 $personellInstance->removeMarried();
100 $personellInstance->removeNumberFormaters();
101 //$personellInstance->removeCache();
102 $personellInstance->removeSystemArray();
104 // Instanz zurueckgeben
105 return $personellInstance;
109 * Create a SimulatorPersonell object by loading the specified personell
110 * list from an existing database backend
112 * @param $idNumber The ID number (only right part) of the list
113 * @return $personellInstance An instance of
114 * @throws InvalidIDFormatException If the given id number
115 * $idNumber is invalid
116 * @throws NullPointerException If a null pointer (instance)
118 * @throws NoObjectException If a non-object has been
120 * @throws MissingMethodException If a required method is missing
121 * @throws MissingSimulatorIDException If an ID number was not found
123 public final static function createSimulatorPersonellByID ($idNumber) {
124 // Add the class name if it was not found
125 if (count(explode("@", $idNumber)) < 2) {
126 // Add class name in front of the incomplete ID number
127 $tempID = sprintf("%s@%s", __CLASS__, $idNumber);
129 // Use the direct ID number
133 // Validate the ID number
134 if (!preg_match(sprintf("/%s\@([a-f0-9]){32}/i", __CLASS__), $tempID)) {
136 throw new InvalidIDFormatException(new SimulatorPersonell(), self::EXCEPTION_ID_IS_INVALID_FORMAT);
140 $personellInstance = new SimulatorPersonell(false);
142 // Get database instance
143 $dbInstance = $personellInstance->getDatabaseInstance();
145 // Is this a valid database instance?
146 if (is_null($dbInstance)) {
148 throw new NullPointerException($personellInstance, self::EXCEPTION_IS_NULL_POINTER);
149 } elseif (!is_object($dbInstance)) {
150 // Not an object! ;-(
151 throw new NoObjectException($dbInstance, self::EXCEPTION_IS_NO_OBJECT);
152 } elseif (!method_exists($dbInstance, 'isUniqueIdUsed')) {
153 // Required method not found
154 throw new MissingMethodException(array($dbInstance, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD);
157 // Is the unique ID already used? Then it must be there!
158 if (!$dbInstance->isUniqueIdUsed($tempID)) {
160 throw new MissingSimulatorIDException(array($personellInstance, $idNumber), self::EXCEPTION_SIMULATOR_ID_INVALID);
163 // Load the personell list and add it to this object
164 $personellInstance->loadPersonellList($tempID);
167 $personellInstance->removeGender();
168 $personellInstance->removeNames();
169 $personellInstance->removeBirthday();
170 $personellInstance->removeSalary();
171 $personellInstance->removeEmployed();
172 $personellInstance->removeMarried();
173 $personellInstance->removeNumberFormaters();
174 //$personellInstance->removeCache();
175 $personellInstance->removeSystemArray();
178 return $personellInstance;
181 // Create personell list
182 public function createPersonellList () {
183 if (is_null($this->personellList)) {
184 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personell-Liste erstellt.<br />\n",
188 $this->personellList = new FrameworkArrayObject();
190 throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
194 // Remove the personell list
195 private function removePersonellList () {
196 if (defined('DEBUG_PERSONELL')) $this->getDebugInstance()->output(sprintf("[%s:%d] Personell-Liste entfernt.<br />\n",
200 unset($this->personellList);
203 // Add new personell object to our list
204 public function addPersonell () {
206 $genders = array('M', 'F');
208 // Create new personell members
209 $personellInstance = new SimulatorPersonell();
211 // Set a randomized gender
212 $personellInstance->setGender($genders[mt_rand(0, 1)]);
214 // Set a randomized birthday (maximum age required, see const MAX_AGE)
215 $personellInstance->createBirthday();
217 // Married? Same values means: married
218 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
221 $personellInstance->removePersonellList();
222 $personellInstance->removeMinMaxAge();
223 $personellInstance->removeCache();
224 $personellInstance->removeSystemArray();
226 // Add new member to the list
227 $this->personellList->append($personellInstance);
231 * Get a specifyable list of our people, null or empty string will be ignored!
233 * @return $cacheList A list of cached personells
235 function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
236 // Serialize the conditions for checking if we can take the cache
237 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
239 // The same (last) conditions?
240 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
241 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Gecachte Liste wird verwendet.<br />\n",
246 // Return cached list
247 return $this->cacheList;
250 // Output debug message
251 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...<br />\n",
256 // Remember the conditions
257 $this->setCacheCond($serialized);
259 // Create cached list
260 $this->setAllCacheList(new FrameworkArrayObject());
262 // Search all unemployed personells
263 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
265 $el = $idx->current();
267 // Check currenylt all single conditions (combined conditions are not yet supported)
268 if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
269 // Add this one (employed status asked)
270 $this->cacheList->append($el);
271 } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
272 // Add this one (marrital status asked)
273 $this->cacheList->append($el);
274 } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
275 // Add this one (specified gender)
276 $this->cacheList->append($el);
280 // Return the completed list
281 return $this->cacheList;
285 * Get amount of unemployed personell
287 * @return $count Amount of unemployed personell
289 public function getAllUnemployed () {
290 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Es werden alle erwerbslosen Personen gesucht.<br />\n",
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 () {
308 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Cache-Attribute entfernt.<br />\n",
314 unset($this->cacheList);
315 unset($this->cacheCond);
319 * Setter for cache list
321 * @param $cacheList The new cache list to set or null for initialization/reset
324 private function setAllCacheList (FrameworkArrayObject $cacheList = null) {
325 $this->cacheList = $cacheList;
329 * Setter for cache conditions
331 * @param $cacheCond The new cache conditions to set
334 private function setCacheCond ($cacheCond) {
335 $this->cacheCond = (string) $cacheCond;
343 public function resetCache () {
344 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Cache-Liste zurückgesetzt.<br />\n",
348 $this->setAllCacheList(null);
349 $this->setCacheCond("");
353 * Getter for surname. If no surname is set then default surnames are set
354 * for male and female personells.
356 * @return $surname The personell' surname
358 public final function getSurname () {
359 $surname = parent::getSurname();
360 // Make sure every one has a surname...
361 if (empty($surname)) {
362 if ($this->isMale()) {
366 // Typical female name
367 $surname = "Jennifer";
370 // Set typical family name
371 parent::setFamily("Smith");
377 * Saves only the personell list to the database
381 public function saveObjectToDatabase () {
383 $dbInstance = $this->getDatabaseInstance();
385 // Prepare the limitation object. We just need the personellList array object.
386 $limitInstance = ObjectLimits::createObjectLimits(array("personellList"));
388 // Limitate the saving amount
389 $dbInstance->limitObject($limitInstance);
392 $dbInstance->saveObject($this);
396 * Getter for personell list
398 * @return $personellList The list of all personells
400 public function getPersonellList () {
401 return $this->personellList;
405 * Loads the mostly pre-cached personell list
407 * @param $idNumber The ID number we shall use for looking up
410 * @throws ContainerItemIsNullException If a container item is null
411 * @throws ContainerItemIsNoArrayException If a container item is
413 * @throws ContainerMaybeDamagedException If the container item
414 * is missing the indexes
415 * 'name' and/or 'value'
416 * @see SerializationContainer A special container class which
417 * helps storing only some attributes
420 public function loadPersonellList ($idNumber) {
421 // Get database instance
422 $dbInstance = $this->getDatabaseInstance();
424 // Get the serialization container within the personell list from
425 // the database layer
426 $containerInstance = $dbInstance->getObjectFromCachedData($idNumber);
428 // Iterate through the whole container
429 for ($idx = $containerInstance->getIterator(); $idx->valid(); $idx->next()) {
430 // Get current item from container
431 $item = $idx->current();
434 if (is_null($item)) {
436 throw new ContainerItemIsNullException($this, self::EXCEPTION_CONTAINER_ITEM_IS_NULL);
437 } elseif (!is_array($item)) {
439 throw new ContainerItemIsNoArrayException($this, self::EXCEPTION_ITEM_IS_NO_ARRAY);
440 } elseif ((!isset($item['name'])) || (!isset($item['value']))) {
442 throw new ContainerMaybeDamagedException($this, self::EXCEPTION_CONTAINER_MAYBE_DAMAGED);
445 // Okay, now we can get the item and generate a valid command for eval().
446 // We need to convert the first letter to lower-case but keep all others intact
447 $eval = sprintf("\$this->%s = \$item['value'];",
448 strtolower(substr($item['name'], 0, 1))
450 substr($item['name'], 1)
454 if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",