3 * The simulator system class is the super class of all other classes. This
4 * class handles saving of games etc.
6 * @author Roland Haeder <webmaster@ship-simu.org>
8 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
9 * @license GNU GPL 3.0 or any newer version
10 * @link http://www.ship-simu.org
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
27 * Instance to an application helper class
29 private static $applicationInstance = null;
32 * The language instance for the template loader
34 private static $langInstance = null;
39 private static $debugInstance = null;
42 * Instance of a request class
44 private $requestInstance = null;
47 * Instance of a response class
49 private $responseInstance = null;
52 * Search criteria instance
54 private $searchInstance = null;
57 * The file I/O instance for the template loader
59 private $fileIoInstance = null;
64 private $resolverInstance = null;
67 * Template engine instance
69 private $templateInstance = null;
72 * Database result instance
74 private $resultInstance = null;
77 * Instance for user class
79 private $userInstance = null;
84 private $realClass = "FrameworkSystem";
89 private $thousands = "."; // German
94 private $decimals = ","; // German
96 /***********************
97 * Exception codes.... *
98 ***********************/
100 const EXCEPTION_IS_NULL_POINTER = 0x001;
101 const EXCEPTION_IS_NO_OBJECT = 0x002;
102 const EXCEPTION_IS_NO_ARRAY = 0x003;
103 const EXCEPTION_MISSING_METHOD = 0x004;
104 const EXCEPTION_CLASSES_NOT_MATCHING = 0x005;
105 const EXCEPTION_INDEX_OUT_OF_BOUNDS = 0x006;
106 const EXCEPTION_DIMENSION_ARRAY_INVALID = 0x007;
107 const EXCEPTION_ITEM_NOT_TRADEABLE = 0x008;
108 const EXCEPTION_ITEM_NOT_IN_PRICE_LIST = 0x009;
109 const EXCEPTION_GENDER_IS_WRONG = 0x00a;
110 const EXCEPTION_BIRTH_DATE_IS_INVALID = 0x00b;
111 const EXCEPTION_EMPTY_STRUCTURES_ARRAY = 0x00c;
112 const EXCEPTION_HAS_ALREADY_PERSONELL_LIST = 0x00d;
113 const EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES = 0x00e;
114 const EXCEPTION_TOTAL_PRICE_NOT_CALCULATED = 0x00f;
115 const EXCEPTION_HARBOR_HAS_NO_SHIPYARDS = 0x010;
116 const EXCEPTION_CONTRACT_PARTNER_INVALID = 0x011;
117 const EXCEPTION_CONTRACT_PARTNER_MISMATCH = 0x012;
118 const EXCEPTION_CONTRACT_ALREADY_SIGNED = 0x013;
119 const EXCEPTION_UNEXPECTED_EMPTY_STRING = 0x014;
120 const EXCEPTION_PATH_NOT_FOUND = 0x015;
121 const EXCEPTION_INVALID_PATH_NAME = 0x016;
122 const EXCEPTION_READ_PROTECED_PATH = 0x017;
123 const EXCEPTION_WRITE_PROTECED_PATH = 0x018;
124 const EXCEPTION_DIR_POINTER_INVALID = 0x019;
125 const EXCEPTION_FILE_POINTER_INVALID = 0x01a;
126 const EXCEPTION_INVALID_DIRECTORY_POINTER = 0x01b;
127 const EXCEPTION_UNEXPECTED_OBJECT = 0x01c;
128 const EXCEPTION_LIMIT_ELEMENT_IS_UNSUPPORTED = 0x01d;
129 const EXCEPTION_GETTER_IS_MISSING = 0x01e;
130 const EXCEPTION_ARRAY_EXPECTED = 0x01f;
131 const EXCEPTION_ARRAY_HAS_INVALID_COUNT = 0x020;
132 const EXCEPTION_ID_IS_INVALID_FORMAT = 0x021;
133 const EXCEPTION_MD5_CHECKSUMS_MISMATCH = 0x022;
134 const EXCEPTION_UNEXPECTED_STRING_SIZE = 0x023;
135 const EXCEPTION_SIMULATOR_ID_INVALID = 0x024;
136 const EXCEPTION_MISMATCHING_COMPRESSORS = 0x025;
137 const EXCEPTION_CONTAINER_ITEM_IS_NULL = 0x026;
138 const EXCEPTION_ITEM_IS_NO_ARRAY = 0x027;
139 const EXCEPTION_CONTAINER_MAYBE_DAMAGED = 0x028;
140 const EXCEPTION_INVALID_STRING = 0x029;
141 const EXCEPTION_VARIABLE_NOT_SET = 0x02a;
142 const EXCEPTION_ATTRIBUTES_ARE_MISSING = 0x02b;
143 const EXCEPTION_ARRAY_ELEMENTS_MISSING = 0x02c;
144 const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED = 0x02d;
145 const EXCEPTION_MISSING_LANGUAGE_HANDLER = 0x02e;
146 const EXCEPTION_MISSING_FILE_IO_HANDLER = 0x02f;
147 const EXCEPTION_MISSING_ELEMENT = 0x030;
148 const EXCEPTION_HEADERS_ALREADY_SENT = 0x031;
149 const EXCEPTION_DEFAULT_CONTROLLER_GONE = 0x032;
150 const EXCEPTION_CLASS_NOT_FOUND = 0x033;
151 const EXCEPTION_REQUIRED_INTERFACE_MISSING = 0x034;
152 const EXCEPTION_FATAL_ERROR = 0x035;
153 const EXCEPTION_FILE_NOT_FOUND = 0x036;
154 const EXCEPTION_ASSERTION_FAILED = 0x037;
155 const EXCEPTION_FILE_CANNOT_BE_READ = 0x038;
156 const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x039;
159 * In the super constructor these system classes shall be ignored or else
160 * we would get an endless calling loop.
162 *---------------------------------------------------------------------*
163 * ATTENTION: IF YOU REMOVE ONE OF THEM YOU WILL RUN YOUR SERVER IN AN *
165 *---------------------------------------------------------------------*
167 private $systemClasses = array(
168 "DebugMiddleware", // Debug middleware output sub-system
169 "Registry", // Object registry
170 "ObjectFactory", // Object factory
171 "DebugWebOutput", // Debug web output sub-system
172 "WebOutput", // Web output sub-system
173 "CompressorChannel", // Compressor sub-system
174 "DebugConsoleOutput", // Debug console output sub-system
175 "DebugErrorLogOutput", // Debug error_log() output sub-system
176 "FrameworkDirectoryPointer", // Directory handler sub-system
177 "NullCompressor", // Null compressor
178 "Bzip2Compressor", // BZIP2 compressor
179 "GzipCompressor", // GZIP compressor
186 * Private super constructor
188 * @param $className Name of the class
191 protected function __construct ($className) {
193 $this->setRealClass($className);
195 // Initialize the class if the registry is there
196 if ((class_exists('Registry')) && (Registry::isInitialized() === false)) {
197 $this->initInstance();
202 * Destructor reached...
205 * @todo This is old code. Do we still need this old lost code?
207 public function __destruct() {
208 // Is this object already destroyed?
209 if ($this->__toString() != 'DestructedObject') {
210 // Destroy all informations about this class but keep some text about it alive
211 $this->setRealClass('DestructedObject');
212 } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
213 // Already destructed object
214 $this->debugOutput(sprintf("[%s:] The object <strong>%s</strong> is already destroyed.",
215 __CLASS__, $this->__toString()
221 * The call method where all non-implemented methods end up
225 public final function __call ($methodName, $args) {
226 // Implode all given arguments
230 $argsString = "NULL";
231 } elseif (is_array($args)) {
232 // Some arguments are there
233 foreach ($args as $arg) {
235 $argsString .= $arg." (".gettype($arg);
237 // Add length if type is string
238 if (gettype($arg) == 'string') $argsString .= ", ".strlen($arg);
241 $argsString .= "), ";
245 if (substr($argsString, -2, 1) === ",") {
246 $argsString = substr($argsString, 0, -2);
249 // Invalid arguments!
250 $argsString = sprintf("!INVALID:%s!", $args);
253 // Output stub message
254 $this->debugOutput(sprintf("[%s->%s] Stub! Args: %s",
265 * Private initializer for this class
269 private final function initInstance () {
270 // Is this a system class?
271 if (!in_array($this->__toString(), $this->systemClasses)) {
272 // Set configuration instance
273 $this->setConfigInstance(FrameworkConfiguration::getInstance());
275 // Add application helper to our class
276 $this->systemclasses[] = $this->getConfigInstance()->readConfig('app_helper_class');
278 // Set debug instance
279 $this->setDebugInstance(DebugMiddleware::createDebugMiddleware($this->getConfigInstance()->readConfig('debug_class')));
281 // Get output instance and set it
282 $outputInstance = ObjectFactory::createObjectByConfiguredName('web_engine', array($this->getConfigInstance()->readConfig('web_content_type')));
283 $this->setWebOutputInstance($outputInstance);
285 // Set the compressor channel
286 $this->setCompressorChannel(CompressorChannel::createCompressorChannel(sprintf("%s%s",
287 $this->getConfigInstance()->readConfig('base_path'),
288 $this->getConfigInstance()->readConfig('compressor_base_path')
291 // Initialization done! :D
292 Registry::isInitialized('OK');
293 } elseif ($this->__toString() == 'DebugMiddleware') {
294 // Set configuration instance
295 $this->setConfigInstance(FrameworkConfiguration::getInstance());
300 * Setter for database result instance
302 * @param $resultInstance An instance of a database result class
304 * @todo SearchableResult and UpdateableResult shall have a super interface to use here
306 protected final function setResultInstance (SearchableResult $resultInstance) {
307 $this->resultInstance = $resultInstance;
311 * Getter for database result instance
313 * @return $resultInstance An instance of a database result class
315 public final function getResultInstance () {
316 return $this->resultInstance;
320 * Setter for template engine instances
322 * @param $templateInstance An instance of a template engine class
325 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
326 $this->templateInstance = $templateInstance;
330 * Getter for template engine instances
332 * @return $templateInstance An instance of a template engine class
334 protected final function getTemplateInstance () {
335 return $this->templateInstance;
339 * Setter for search instance
341 * @param $searchInstance Searchable criteria instance
344 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
345 $this->searchInstance = $searchInstance;
349 * Getter for search instance
351 * @return $searchInstance Searchable criteria instance
353 public final function getSearchInstance () {
354 return $this->searchInstance;
358 * Setter for resolver instance
360 * @param $resolverInstance Instance of a command resolver class
363 public final function setResolverInstance (Resolver $resolverInstance) {
364 $this->resolverInstance = $resolverInstance;
368 * Getter for resolver instance
370 * @return $resolverInstance Instance of a command resolver class
372 public final function getResolverInstance () {
373 return $this->resolverInstance;
377 * Setter for language instance
379 * @param $configInstance The configuration instance which shall
380 * be FrameworkConfiguration
383 public final function setConfigInstance (FrameworkConfiguration $configInstance) {
384 Registry::getRegistry()->addInstance('config', $configInstance);
388 * Getter for configuration instance
390 * @return $cfgInstance Configuration instance
392 public final function getConfigInstance () {
393 $cfgInstance = Registry::getRegistry()->getInstance('config');
398 * Setter for debug instance
400 * @param $debugInstance The instance for debug output class
403 public final function setDebugInstance (DebugMiddleware $debugInstance) {
404 self::$debugInstance = $debugInstance;
408 * Getter for debug instance
410 * @return $debugInstance Instance to class DebugConsoleOutput or DebugWebOutput
412 public final function getDebugInstance () {
413 return self::$debugInstance;
417 * Setter for web output instance
419 * @param $webInstance The instance for web output class
422 public final function setWebOutputInstance (OutputStreamer $webInstance) {
423 Registry::getRegistry()->addInstance('web_output', $webInstance);
427 * Getter for web output instance
429 * @return $webOutput - Instance to class WebOutput
431 public final function getWebOutputInstance () {
432 return Registry::getRegistry()->getInstance('web_output');
436 * Setter for database instance
438 * @param $dbInstance The instance for the database connection
439 * (forced DatabaseConnection)
442 public final function setDatabaseInstance (DatabaseConnection $dbInstance) {
443 Registry::getRegistry()->addInstance('dbInstance', $dbInstance);
447 * Getter for database layer
449 * @return $dbInstance The database layer instance
451 public final function getDatabaseInstance () {
452 // Default is invalid db instance
455 // Is the registry there and initialized?
456 if ((class_exists('Registry')) && (Registry::isInitialized() === true)) {
457 $dbInstance = Registry::getRegistry()->getInstance('dbInstance');
465 * Setter for compressor channel
467 * @param $compressorChannel An instance of CompressorChannel
470 public final function setCompressorChannel (CompressorChannel $compressorChannel) {
471 Registry::getRegistry()->addInstance('compressor', $compressorChannel);
475 * Getter for compressor channel
477 * @return $compressor The compressor channel
479 public final function getCompressorChannel () {
480 return Registry::getRegistry()->getInstance('compressor');
484 * Protected getter for a manageable application helper class
486 * @return $applicationInstance An instance of a manageable application helper class
488 protected final function getApplicationInstance () {
489 return self::$applicationInstance;
493 * Setter for a manageable application helper class
495 * @param $applicationInstance An instance of a manageable application helper class
498 public final function setApplicationInstance (ManageableApplication $applicationInstance) {
499 self::$applicationInstance = $applicationInstance;
503 * Setter for request instance
505 * @param $requestInstance An instance of a Requestable class
508 public final function setRequestInstance (Requestable $requestInstance) {
509 $this->requestInstance = $requestInstance;
513 * Getter for request instance
515 * @return $requestInstance An instance of a Requestable class
517 public final function getRequestInstance () {
518 return $this->requestInstance;
522 * Setter for response instance
524 * @param $responseInstance An instance of a Responseable class
527 public final function setResponseInstance (Responseable $responseInstance) {
528 $this->responseInstance = $responseInstance;
532 * Getter for response instance
534 * @return $responseInstance An instance of a Responseable class
536 public final function getResponseInstance () {
537 return $this->responseInstance;
541 * Getter for $realClass
543 * @return $realClass The name of the real class (not BaseFrameworkSystem)
545 public final function __toString () {
546 return $this->realClass;
550 * Setter for the real class name
552 * @param $realClass Class name (string)
555 public final function setRealClass ($realClass) {
557 $realClass = (string) $realClass;
560 $this->realClass = $realClass;
564 * Compare class name of this and given class name
566 * @param $className The class name as string from the other class
567 * @return boolean The result of comparing both class names
569 public final function isClass ($className) {
570 return ($this->__toString() == $className);
574 * Stub method (only real cabins shall override it)
576 * @return boolean false = is no cabin, true = is a cabin
579 public function isCabin () {
584 * Stub method for tradeable objects
586 * @return boolean false = is not tradeable by the Merchant class,
587 * true = is a tradeable object
590 public function isTradeable () {
595 * Formats computer generated price values into human-understandable formats
596 * with thousand and decimal seperators.
598 * @param $value The in computer format value for a price
599 * @param $currency The currency symbol (use HTML-valid characters!)
600 * @param $decNum Number of decimals after commata
601 * @return $price The for the current language formated price string
602 * @throws MissingDecimalsThousandsSeperatorException If decimals or
603 * thousands seperator
606 public function formatCurrency ($value, $currency = "€", $decNum = 2) {
607 // Are all required attriutes set?
608 if ((!isset($this->decimals)) || (!isset($this->thousands))) {
609 // Throw an exception
610 throw new MissingDecimalsThousandsSeperatorException($this, self::EXCEPTION_ATTRIBUTES_ARE_MISSING);
614 $value = (float) $value;
616 // Reformat the US number
617 $price = sprintf("%s %s",
618 number_format($value, $decNum, $this->decimals, $this->thousands),
622 // Return as string...
627 * Removes number formating characters
631 public final function removeNumberFormaters () {
632 unset($this->thousands);
633 unset($this->decimals);
637 * Private getter for language instance
639 * @return $langInstance An instance to the language sub-system
641 protected final function getLanguageInstance () {
642 return self::$langInstance;
646 * Setter for language instance
648 * @param $langInstance An instance to the language sub-system
650 * @see LanguageSystem
652 public final function setLanguageInstance (ManageableLanguage $langInstance) {
653 self::$langInstance = $langInstance;
657 * Remove the $systemClasses array from memory
661 public final function removeSystemArray () {
662 unset($this->systemClasses);
666 * Appends a trailing slash to a string
668 * @param $str A string (maybe) without trailing slash
669 * @return $str A string with an auto-appended trailing slash
671 public final function addMissingTrailingSlash ($str) {
672 // Is there a trailing slash?
673 if (substr($str, -1, 1) != "/") $str .= "/";
678 * Private getter for file IO instance
680 * @return $fileIoInstance An instance to the file I/O sub-system
682 protected final function getFileIoInstance () {
683 return $this->fileIoInstance;
687 * Setter for file I/O instance
689 * @param $fileIoInstance An instance to the file I/O sub-system
692 public final function setFileIoInstance (FileIoHandler $fileIoInstance) {
693 $this->fileIoInstance = $fileIoInstance;
697 * Prepare the template engine (WebTemplateEngine by default) for a given
698 * application helper instance (ApplicationHelper by default).
700 * @param $appInstance An application helper instance or
701 * null if we shall use the default
702 * @return $templateInstance The template engine instance
703 * @throws NullPointerException If the template engine could not
705 * @throws UnsupportedTemplateEngineException If $templateInstance is an
706 * unsupported template engine
707 * @throws MissingLanguageHandlerException If the language sub-system
708 * is not yet initialized
709 * @throws NullPointerException If the discovered application
710 * instance is still null
712 protected function prepareTemplateInstance (BaseFrameworkSystem $appInstance=null) {
713 // Is the application instance set?
714 if (is_null($appInstance)) {
715 // Get the current instance
716 $appInstance = $this->getApplicationInstance();
719 if (is_null($appInstance)) {
720 // Thrown an exception
721 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
725 // Generate FQFN for all application templates
726 $fqfn = sprintf("%s%s/%s",
727 $this->getConfigInstance()->readConfig('application_path'),
728 strtolower($appInstance->getAppShortName()),
729 $this->getConfigInstance()->readConfig('tpl_base_path')
732 // Are both instances set?
733 if ($appInstance->getLanguageInstance() === null) {
734 // Invalid language instance
735 throw new MissingLanguageHandlerException($appInstance, self::EXCEPTION_MISSING_LANGUAGE_HANDLER);
736 } elseif ($appInstance->getFileIoInstance() === null) {
737 // Invalid language instance
738 throw new MissingFileIoHandlerException($appInstance, self::EXCEPTION_MISSING_FILE_IO_HANDLER);
741 // Initialize the template engine
742 $templateInstance = ObjectFactory::createObjectByConfiguredName('template_class', array($fqfn, $appInstance->getLanguageInstance(), $appInstance->getFileIoInstance()));
744 // Return the prepared instance
745 return $templateInstance;
749 * Debugs this instance by putting out it's full content
753 public final function debugInstance () {
754 // Restore the error handler to avoid trouble with missing array elements or undeclared variables
755 restore_error_handler();
757 // Generate the output
758 $content = sprintf("<pre>%s</pre>",
767 ApplicationEntryPoint::app_die(sprintf("<strong>%s debug output:</strong><div id=\"debug_content\">%s</div>\nLoaded includes: <div id=\"debug_include_list\">%s</div>",
770 ClassLoader::getInstance()->getPrintableIncludeList()
775 * Output a partial stub message for the caller method
777 * @param $message An optional message to display
780 protected function partialStub ($message = "") {
782 $backtrace = debug_backtrace();
784 // Generate the class::method string
785 $methodName = "UnknownClass->unknownMethod";
786 if ((isset($backtrace[1]['class'])) && (isset($backtrace[1]['function']))) {
787 $methodName = $backtrace[1]['class']."->".$backtrace[1]['function'];
790 // Construct the full message
791 $stubMessage = sprintf("[%s:] Partial stub!",
795 // Is the extra message given?
796 if (!empty($message)) {
797 // Then add it as well
798 $stubMessage .= sprintf(" Message: <span id=\"stub_message\">%s</span>", $message);
801 // Debug instance is there?
802 if (!is_null($this->getDebugInstance())) {
803 // Output stub message
804 $this->debugOutput($stubMessage);
807 trigger_error($stubMessage."<br />\n");
812 * Outputs a debug backtrace and stops further script execution
816 public function debugBackTrace () {
817 // Sorry, there is no other way getting this nice backtrace
819 debug_print_backtrace();
825 * Outputs a debug message wether to debug instance (should be set!) or dies with or pints the message
827 * @param $message Message we shall send out...
828 * @param $doPrint Wether we shall print or die here which first is the default
831 public function debugOutput ($message, $doPrint = true) {
832 // Get debug instance
833 $debugInstance = $this->getDebugInstance();
835 // Is the debug instance there?
836 if (is_object($debugInstance)) {
837 // Use debug output handler
838 $debugInstance->output($message);
839 if (!$doPrint) die(); // Die here if not printed
845 // DO NOT REWRITE THIS TO app_die() !!!
852 * Converts e.g. a command from URL to a valid class by keeping out bad characters
854 * @param $str The string, what ever it is needs to be converted
855 * @return $className Generated class name
857 public function convertToClassName ($str) {
861 // Convert all dashes in underscores
862 $str = str_replace("-", "_", $str);
864 // Now use that underscores to get classname parts for hungarian style
865 foreach (explode("_", $str) as $strPart) {
866 // Make the class name part lower case and first upper case
867 $className .= ucfirst(strtolower($strPart));
875 * Marks up the code by adding e.g. line numbers
877 * @param $phpCode Unmarked PHP code
878 * @return $markedCode Marked PHP code
880 public function markupCode ($phpCode) {
885 $errorArray = error_get_last();
887 // Init the code with error message
888 if (is_array($errorArray)) {
890 $markedCode = sprintf("<div id=\"error_header\">File: <span id=\"error_data\">%s</span>, Line: <span id=\"error_data\">%s</span>, Message: <span id=\"error_data\">%s</span>, Type: <span id=\"error_data\">%s</span></div>",
891 basename($errorArray['file']),
893 $errorArray['message'],
898 // Add line number to the code
899 foreach (explode("\n", $phpCode) as $lineNo => $code) {
901 $markedCode .= sprintf("<span id=\"code_line\">%s</span>: %s\n",
903 htmlentities($code, ENT_QUOTES)
912 * Filter a given GMT timestamp (non Uni* stamp!) to make it look more
913 * beatiful for web-based front-ends. If null is given a message id
914 * null_timestamp will be resolved and returned.
916 * @param $timestamp Timestamp to prepare (filter) for display
917 * @return $readable A readable timestamp
919 public function doFilterFormatTimestamp ($timestamp) {
920 // Default value to return
923 // Is the timestamp null?
924 if (is_null($timestamp)) {
925 // Get a message string
926 $readable = $this->getLanguageInstance()->getMessage('null_timestamp');
928 switch ($this->getLanguageInstance()->getLanguageCode()) {
929 case "de": // German format is a bit different to default
930 // Split the GMT stamp up
931 $dateTime = explode(" ", $timestamp);
932 $dateArray = explode("-", $dateTime[0]);
933 $timeArray = explode(":", $dateTime[1]);
935 // Construct the timestamp
936 $readable = sprintf($this->getConfigInstance()->readConfig('german_date_time'),
946 default: // Default is pass-through
947 $readable = $timestamp;
957 * "Getter" for databse entry
959 * @return $entry An array with database entries
960 * @throws NullPointerException If the database result is not found
961 * @throws InvalidDatabaseResultException If the database result is invalid
963 protected final function getDatabaseEntry () {
964 // Is there an instance?
965 if (is_null($this->getResultInstance())) {
966 // Throw an exception here
967 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
971 $this->getResultInstance()->rewind();
973 // Do we have an entry?
974 if (!$this->getResultInstance()->valid()) {
975 throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
979 $this->getResultInstance()->next();
982 $entry = $this->getResultInstance()->current();
989 * Getter for field name
991 * @param $fieldName Field name which we shall get
992 * @return $fieldValue Field value from the user
993 * @throws NullPointerException If the result instance is null
995 public final function getField ($fieldName) {
996 // Default field value
999 // Get result instance
1000 $resultInstance = $this->getResultInstance();
1002 // Is this instance null?
1003 if (is_null($resultInstance)) {
1004 // Then the user instance is no longer valid (expired cookies?)
1005 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1008 // Get current array
1009 $fieldArray = $resultInstance->current();
1011 // Does the field exist?
1012 if (isset($fieldArray[$fieldName])) {
1014 $fieldValue = $fieldArray[$fieldName];
1022 * Protected setter for user instance
1024 * @param $userInstance An instance of a user class
1027 protected final function setUserInstance (ManageableAccount $userInstance) {
1028 $this->userInstance = $userInstance;
1032 * Getter for user instance
1034 * @return $userInstance An instance of a user class
1036 public final function getUserInstance () {
1037 return $this->userInstance;