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__);
45 * Magic wake-up method called when unserialize() is called. This is
46 * neccessary because in this case a personell does not need to know the
47 * min/max ages range and system classes. This would anyway use more RAM
48 * what is not required.
52 public function __wakeup () {
54 $this->removePersonellList();
55 $this->removeMinMaxAge();
57 $this->removeSystemArray();
61 * Generate a specified amount of personell and return the prepared instance
63 * @param $amountPersonell Number of personell we shall
65 * @return $personellInstance An instance of this object with a
68 public final static function createSimulatorPersonell ($amountPersonell) {
69 // Make sure only integer can pass
70 $amountPersonell = (int) $amountPersonell;
73 $personellInstance = new SimulatorPersonell();
76 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.",
82 // Initialize the personell list
83 $personellInstance->createPersonellList();
85 // Create requested amount of personell
86 for ($idx = 0; $idx < $amountPersonell; $idx++) {
87 $personellInstance->addRandomPersonell();
91 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->debugOutput(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.",
98 $personellInstance->removeGender();
99 $personellInstance->removeNames();
100 $personellInstance->removeBirthday();
101 $personellInstance->removeSalary();
102 $personellInstance->removeEmployed();
103 $personellInstance->removeMarried();
104 $personellInstance->removeNumberFormaters();
105 //$personellInstance->removeCache();
106 $personellInstance->removeSystemArray();
108 // Instanz zurueckgeben
109 return $personellInstance;
113 * Create a SimulatorPersonell object by loading the specified personell
114 * list from an existing database backend
116 * @param $idNumber The ID number (only right part) of the list
117 * @return $personellInstance An instance of this class
118 * @throws InvalidIDFormatException If the given id number
119 * $idNumber is invalid
120 * @throws MissingSimulatorIdException If an ID number was not found
122 public final static function createSimulatorPersonellByID ($idNumber) {
123 // Use the direct ID number
126 // Add the class name if it was not found
127 if (count(explode("@", $idNumber)) < 2) {
128 // Add class name in front of the incomplete ID number
129 $tempID = sprintf("%s@%s", __CLASS__, $idNumber);
132 // Validate the ID number
133 if (!preg_match(sprintf("/%s\@([a-f0-9]){32}/i", __CLASS__), $tempID)) {
135 throw new InvalidIDFormatException(new SimulatorPersonell(), self::EXCEPTION_ID_IS_INVALID_FORMAT);
139 $personellInstance = new SimulatorPersonell(false);
141 // Get database instance
142 $dbInstance = $personellInstance->getDatabaseInstance();
144 // Is the unique ID already used? Then it must be there!
145 if (!$dbInstance->isUniqueIdUsed($tempID)) {
147 throw new MissingSimulatorIdException(array($personellInstance, $idNumber), self::EXCEPTION_SIMULATOR_ID_INVALID);
150 // Load the personell list and add it to this object
151 $personellInstance->loadPersonellList($tempID);
154 $personellInstance->removeGender();
155 $personellInstance->removeNames();
156 $personellInstance->removeBirthday();
157 $personellInstance->removeSalary();
158 $personellInstance->removeEmployed();
159 $personellInstance->removeMarried();
160 $personellInstance->removeNumberFormaters();
161 //$personellInstance->removeCache();
162 $personellInstance->removeSystemArray();
165 return $personellInstance;
168 // Create personell list
169 public function createPersonellList () {
170 // Is the list already created?
171 if ($this->personelllList instanceof FrameworkArrayObject) {
172 // Throw an exception
173 throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
176 // Initialize the array
177 $this->personellList = new FrameworkArrayObject("FakedPersonellList");
180 // Remove the personell list
181 private final function removePersonellList () {
182 unset($this->personellList);
185 // Add new personell object to our list
186 public function addRandomPersonell () {
188 $genders = array("M", "F");
190 // Create new personell members
191 $personellInstance = new SimulatorPersonell();
193 // Set a randomized gender
194 $personellInstance->setGender($genders[mt_rand(0, 1)]);
196 // Set a randomized birthday (maximum age required, see const MAX_AGE)
197 $personellInstance->createBirthday();
199 // Married? Same values means: married
200 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
203 $personellInstance->removePersonellList();
204 $personellInstance->removeMinMaxAge();
205 $personellInstance->removeCache();
206 $personellInstance->removeSystemArray();
208 // Add new member to the list
209 $this->personellList->append($personellInstance);
213 * Get a specifyable list of our people, null or empty string will be ignored!
215 * @return $cacheList A list of cached personells
217 function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
218 // Serialize the conditions for checking if we can take the cache
219 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
221 // The same (last) conditions?
222 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
223 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
228 // Return cached list
229 return $this->cacheList;
232 // Output debug message
233 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
238 // Remember the conditions
239 $this->setCacheCond($serialized);
241 // Create cached list
242 $this->setAllCacheList(new FrameworkArrayObject('FakedCacheList'));
244 // Search all unemployed personells
245 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
247 $el = $idx->current();
249 // Check currenylt all single conditions (combined conditions are not yet supported)
250 if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
251 // Add this one (employed status asked)
252 $this->cacheList->append($el);
253 } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
254 // Add this one (marrital status asked)
255 $this->cacheList->append($el);
256 } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
257 // Add this one (specified gender)
258 $this->cacheList->append($el);
262 // Return the completed list
263 return $this->cacheList;
267 * Get amount of unemployed personell
269 * @return $count Amount of unemployed personell
271 public final function getAllUnemployed () {
272 // Get a temporary list
273 $list = $this->getSpecialPersonellList(false);
275 // Anzahl zurueckliefern
276 return $list->count();
280 * Remove cache things
284 private function removeCache () {
286 unset($this->cacheList);
287 unset($this->cacheCond);
291 * Setter for cache list
293 * @param $cacheList The new cache list to set or null for initialization/reset
296 private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
297 $this->cacheList = $cacheList;
301 * Setter for cache conditions
303 * @param $cacheCond The new cache conditions to set
306 private final function setCacheCond ($cacheCond) {
307 $this->cacheCond = (string) $cacheCond;
315 public function resetCache () {
316 $this->setAllCacheList(null);
317 $this->setCacheCond("");
321 * Getter for surname. If no surname is set then default surnames are set
322 * for male and female personells.
324 * @return $surname The personell' surname
326 public final function getSurname () {
327 $surname = parent::getSurname();
329 // Make sure every one has a surname...
330 if (empty($surname)) {
331 if ($this->isMale()) {
335 // Typical female name
336 $surname = "Jennifer";
339 // Set typical family name
340 parent::setFamily("Smith");
348 * Getter for personell list
350 * @return $personellList The list of all personells
352 public final function getPersonellList () {
353 return $this->personellList;
357 * Loads the mostly pre-cached personell list
359 * @param $idNumber The ID number we shall use for looking up
362 * @throws ContainerItemIsNullException If a container item is null
363 * @throws ContainerItemIsNoArrayException If a container item is
365 * @throws ContainerMaybeDamagedException If the container item
366 * is missing the indexes
367 * 'name' and/or 'value'
370 public function loadPersonellList ($idNumber) {
371 // Cleared because old code
372 $this->partialStub("Cleared because old lost code was used.");