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