]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/user/class_User.php
62c4f17e9d15b913a1a993fad06fd58fbbf6f6bd
[shipsimu.git] / inc / classes / main / user / class_User.php
1 <?php
2 /**
3  * A generic class for handling users
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class User extends BaseFrameworkSystem implements ManageableUser {
25         /**
26          * Username
27          */
28         private $username = "";
29
30         /**
31          * An instance of a database wrapper
32          */
33         private $userWrapper = null;
34
35         // Exceptions
36         const EXCEPTION_USERNAME_NOT_FOUND = 0xd00;
37
38         /**
39          * Private constructor
40          *
41          * @return      void
42          */
43         protected function __construct ($class = "") {
44                 // Is the class name empty? Then this is not a specialized user class
45                 if (empty($class)) $class = __CLASS__;
46
47                 // Call parent constructor
48                 parent::__construct($class);
49
50                 // Set part description
51                 $this->setObjectDescription("Generic user class");
52
53                 // Create unique ID number
54                 $this->createUniqueID();
55
56                 // Clean up a little
57                 $this->removeNumberFormaters();
58                 $this->removeSystemArray();
59         }
60
61         /**
62          * Creates an instance of this user class by a provided username. This
63          * factory method will check if the username is already taken and if not
64          * so it will throw an exception.
65          *
66          * @param       $username               Username we need a class instance for
67          * @return      $userInstance   An instance of this user class
68          * @throws      UsernameMissingException        If the username does not exist
69          */
70         public final static function createUserByUsername ($userName) {
71                 // Get a new instance
72                 $userInstance = new User();
73
74                 // Set the username
75                 $userInstance->setUsername($userName);
76
77                 // Check if the username exists
78                 if (!$userInstance->ifUsernameExists()) {
79                         // Throw an exception here
80                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
81                 }
82
83                 // Return the instance
84                 return $userInstance;
85         }
86
87         /**
88          * Setter for username
89          *
90          * @param       $userName       The username to set
91          * @return      void
92          */
93         protected final function setUsername ($userName) {
94                 $this->userNane = $userName;
95         }
96
97         /**
98          * Getter for username
99          *
100          * @return      $userName       The username to get
101          */
102         public final function getUsername () {
103                 return $this->userNane;
104         }
105
106         /**
107          * Determines wether the username exists or not
108          *
109          * @return      $exists         Wether the username exists
110          */
111         protected function ifUsernameExists () {
112                 // By default the username does exist
113                 $exists = true;
114
115                 // Try to get a UserDatabaseWrapper object back
116                 try {
117                         // Get the instance by providing this class
118                         $this->userWrapper = UserDatabaseWrapper::createUserDatabaseWrapper($this);
119                 } catch (WrapperUserNameNotFoundException $e) {
120                         // Does not exist!
121                         $exists = false;
122                 }
123
124                 // Return the status
125                 return $exists;
126         }
127 }
128
129 // [EOF]
130 ?>