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 this class
127 * @throws InvalidIDFormatException If the given id number
128 * $idNumber is invalid
129 * @throws MissingSimulatorIdException If an ID number was not found
131 public final static function createSimulatorPersonellByID ($idNumber) {
132 // Add the class name if it was not found
133 if (count(explode("@", $idNumber)) < 2) {
134 // Add class name in front of the incomplete ID number
135 $tempID = sprintf("%s@%s", __CLASS__, $idNumber);
137 // Use the direct ID number
141 // Validate the ID number
142 if (!preg_match(sprintf("/%s\@([a-f0-9]){32}/i", __CLASS__), $tempID)) {
144 throw new InvalidIDFormatException(new SimulatorPersonell(), self::EXCEPTION_ID_IS_INVALID_FORMAT);
148 $personellInstance = new SimulatorPersonell(false);
150 // Get database instance
151 $dbInstance = $personellInstance->getDatabaseInstance();
153 // Is the unique ID already used? Then it must be there!
154 if (!$dbInstance->isUniqueIdUsed($tempID)) {
156 throw new MissingSimulatorIdException(array($personellInstance, $idNumber), self::EXCEPTION_SIMULATOR_ID_INVALID);
159 // Load the personell list and add it to this object
160 $personellInstance->loadPersonellList($tempID);
163 $personellInstance->removeGender();
164 $personellInstance->removeNames();
165 $personellInstance->removeBirthday();
166 $personellInstance->removeSalary();
167 $personellInstance->removeEmployed();
168 $personellInstance->removeMarried();
169 $personellInstance->removeNumberFormaters();
170 //$personellInstance->removeCache();
171 $personellInstance->removeSystemArray();
174 return $personellInstance;
177 // Create personell list
178 public function createPersonellList () {
179 if (is_null($this->personellList)) {
180 $this->personellList = new FrameworkArrayObject("FakedPersonellList");
182 throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
186 // Remove the personell list
187 private function removePersonellList () {
188 unset($this->personellList);
191 // Add new personell object to our list
192 public function addRandomPersonell () {
194 $genders = array("M", "F");
196 // Create new personell members
197 $personellInstance = new SimulatorPersonell();
199 // Set a randomized gender
200 $personellInstance->setGender($genders[mt_rand(0, 1)]);
202 // Set a randomized birthday (maximum age required, see const MAX_AGE)
203 $personellInstance->createBirthday();
205 // Married? Same values means: married
206 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
209 $personellInstance->removePersonellList();
210 $personellInstance->removeMinMaxAge();
211 $personellInstance->removeCache();
212 $personellInstance->removeSystemArray();
214 // Add new member to the list
215 $this->personellList->append($personellInstance);
219 * Get a specifyable list of our people, null or empty string will be ignored!
221 * @return $cacheList A list of cached personells
223 function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
224 // Serialize the conditions for checking if we can take the cache
225 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
227 // The same (last) conditions?
228 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
229 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
234 // Return cached list
235 return $this->cacheList;
238 // Output debug message
239 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
244 // Remember the conditions
245 $this->setCacheCond($serialized);
247 // Create cached list
248 $this->setAllCacheList(new FrameworkArrayObject('FakedCacheList'));
250 // Search all unemployed personells
251 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
253 $el = $idx->current();
255 // Check currenylt all single conditions (combined conditions are not yet supported)
256 if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
257 // Add this one (employed status asked)
258 $this->cacheList->append($el);
259 } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
260 // Add this one (marrital status asked)
261 $this->cacheList->append($el);
262 } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
263 // Add this one (specified gender)
264 $this->cacheList->append($el);
268 // Return the completed list
269 return $this->cacheList;
273 * Get amount of unemployed personell
275 * @return $count Amount of unemployed personell
277 public final function getAllUnemployed () {
278 // Get a temporary list
279 $list = $this->getSpecialPersonellList(false);
281 // Anzahl zurueckliefern
282 return $list->count();
286 * Remove cache things
290 private function removeCache () {
292 unset($this->cacheList);
293 unset($this->cacheCond);
297 * Setter for cache list
299 * @param $cacheList The new cache list to set or null for initialization/reset
302 private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
303 $this->cacheList = $cacheList;
307 * Setter for cache conditions
309 * @param $cacheCond The new cache conditions to set
312 private final function setCacheCond ($cacheCond) {
313 $this->cacheCond = (string) $cacheCond;
321 public function resetCache () {
322 $this->setAllCacheList(null);
323 $this->setCacheCond("");
327 * Getter for surname. If no surname is set then default surnames are set
328 * for male and female personells.
330 * @return $surname The personell' surname
332 public final function getSurname () {
333 $surname = parent::getSurname();
335 // Make sure every one has a surname...
336 if (empty($surname)) {
337 if ($this->isMale()) {
341 // Typical female name
342 $surname = "Jennifer";
345 // Set typical family name
346 parent::setFamily("Smith");
354 * Getter for personell list
356 * @return $personellList The list of all personells
358 public final function getPersonellList () {
359 return $this->personellList;
363 * Loads the mostly pre-cached personell list
365 * @param $idNumber The ID number we shall use for looking up
368 * @throws ContainerItemIsNullException If a container item is null
369 * @throws ContainerItemIsNoArrayException If a container item is
371 * @throws ContainerMaybeDamagedException If the container item
372 * is missing the indexes
373 * 'name' and/or 'value'
375 public function loadPersonellList ($idNumber) {
376 // Cleared because old code
377 $this->partialStub("Cleared because old lost code was used.");