419a85658052f91d3e217286395d856294f473af
[shipsimu.git] / inc / classes / main / language / class_LanguageSystem.php
1 <?php
2 /**
3  * The language sub-system for handling language strings being used in the
4  * application and whole framework
5  *
6  * @author              Roland Haeder <webmaster@mxchange.org>
7  * @version             0.3.0
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.mxchange.org
11  *
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.
16  *
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.
21  *
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/>.
24  */
25 class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage {
26         /**
27          * The full-qualified base path for the language include files
28          */
29         private $basePath = "";
30
31         /**
32          * The 2-char language code
33          */
34         private $langCode = "xx"; // This will later be overwritten!
35
36         /**
37          * The array-object for all language strings
38          */
39         private $langStrings = null;
40
41         /**
42          * An instance of this class
43          */
44         private static $thisInstance = null;
45
46         /**
47          * Private constructor
48          *
49          * @return      void
50          */
51         private final function __construct () {
52                 // Call parent constructor
53                 parent::constructor(__CLASS__);
54
55                 // Set part description
56                 $this->setPartDescr("Sprachsystem");
57
58                 // Create unique ID number
59                 $this->createUniqueID();
60
61                 // Clean up a little
62                 $this->removeNumberFormaters();
63                 $this->removeSystemArray();
64         }
65
66         /**
67          * Creates an instance of the class LanguageSystem and prepares it for usage
68          *
69          * @param               $basePath               The local base path for all language strings
70          * @return      $langInstance   An instance of LanguageSystem
71          * @throws      LanguagePathIsEmptyException            If the provided $basePath is empty
72          * @throws      InvalidLanguagePathStringException      If $basePath is no string
73          * @throws      LanguagePathIsNoDirectoryException      If $basePath is no
74          *                                                                              directory or not found
75          * @throws      LanguagePathReadProtectedException      If $basePath is
76          *                                                                              read-protected
77          */
78         public final static function createLanguageSystem ($basePath) {
79                 // Get a new instance
80                 $langInstance = new LanguageSystem();
81
82                 // Is the base path valid?
83                 if (empty($basePath)) {
84                         // Language path is empty
85                         throw new LanguagePathIsEmptyException($langInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
86                 } elseif (!is_string($basePath)) {
87                         // Is not a string
88                         throw new InvalidLanguagePathStringException(array($langInstance, $basePath), self::EXCEPTION_INVALID_STRING);
89                 } elseif (!is_dir($basePath)) {
90                         // Is not a path
91                         throw new LanguagePathIsNoDirectoryException(array($langInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
92                 } elseif (!is_readable($basePath)) {
93                         // Is not readable
94                         throw new LanguagePathReadProtectedException(array($langInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
95                 }
96
97                 // Set the base path
98                 $langInstance->setBasePath($basePath);
99
100                 // Initialize the variable stack
101                 $langInstance->initLanguageStrings();
102
103                 // Set language code from default config
104                 $langInstance->setLanguageCode($langInstance->getConfigInstance()->readConfig("default_lang"));
105
106                 // Remember this instance
107                 self::$thisInstance = $langInstance;
108
109                 // Return the prepared instance
110                 return $langInstance;
111         }
112
113         /**
114          * Singleton getter for this instance
115          *
116          * @return      $thisInstance           An instance of this class
117          */
118         public final static function getInstance () {
119                 return self::$thisInstance;
120         }
121
122         /**
123          * Initialize the array-object for all later language strings
124          *
125          * @return      void
126          */
127         public function initLanguageStrings () {
128                 $this->langStrings = new FrameworkArrayObject();
129         }
130
131         /**
132          * Setter for base path
133          *
134          * @param               $basePath               The local base path for all templates
135          * @return      void
136          */
137         public final function setBasePath ($basePath) {
138                 // Cast it
139                 $basePath = (string) $basePath;
140
141                 // And set it
142                 $this->basePath = $basePath;
143         }
144
145         /**
146          * Getter for language code
147          *
148          * @return      $langCode               The language code for the current application
149          */
150         public final function getLanguageCode () {
151                 return $this->langCode;
152         }
153
154         /**
155          * Setter for language code
156          *
157          * @param               $langCode               The language code for the current application
158          * @return      void
159          */
160         public final function setLanguageCode ($langCode) {
161                 // Cast it
162                 $langCode = (string) $langCode;
163
164                 // And set it (only 2 chars)
165                 $this->langCode = substr($langCode, 0, 2);
166         }
167 }
168
169 // [EOF]
170 ?>