Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * Contains the Translation2_Container base class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The name of the author may not be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @category  Internationalization
31
 * @package   Translation2
32
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
33
 * @copyright 2004-2005 Lorenzo Alberton
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id: Container.php 305985 2010-12-05 22:55:33Z clockwerx $
36
 * @link      http://pear.php.net/package/Translation2
37
 */
38
 
39
/**
40
 * Base class for Translation2 drivers/containers
41
 *
42
 * Extend this class to provide custom containers.
43
 * Some containers are already bundled with the package.
44
 *
45
 * @category  Internationalization
46
 * @package   Translation2
47
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
48
 * @copyright 2004-2005 Lorenzo Alberton
49
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
50
 * @link      http://pear.php.net/package/Translation2
51
 */
52
class Translation2_Container
53
{
54
    // {{{ Class vars
55
 
56
    /**
57
     * Additional options for the storage container
58
     * @var array
59
     */
60
    var $options = array();
61
 
62
    /**
63
     * @var array
64
     * @access private
65
     */
66
    var $currentLang = array();
67
 
68
    /**
69
     * @var array
70
     * @access private
71
     */
72
    var $langs = array();
73
 
74
    // }}}
75
    // {{{ Constructor
76
 
77
    /**
78
     * Constructor
79
     * Has to be overwritten by each storage class
80
     *
81
     * @access public
82
     */
83
    function Translation2_Container()
84
    {
85
    }
86
 
87
    // }}}
88
    // {{{ _parseOptions()
89
 
90
    /**
91
     * Parse options passed to the container class
92
     *
93
     * @param array $array options
94
     *
95
     * @return void
96
     * @access protected
97
     */
98
    function _parseOptions($array)
99
    {
100
        if (!is_array($array)) {
101
            return;
102
        }
103
        foreach ($array as $key => $value) {
104
            if (isset($this->options[$key])) {
105
                $this->options[$key] = $value;
106
            }
107
        }
108
    }
109
 
110
    // }}}
111
    // {{{ _getLangID()
112
 
113
    /**
114
     * Get a valid langID or raise an error when no valid language is set
115
     *
116
     * @param string $langID language ID
117
     *
118
     * @return string language ID or PEAR_Error on error
119
     * @access private
120
     */
121
    function _getLangID($langID)
122
    {
123
        if (!empty($langID) || (0 === $langID)) {
124
            return $langID;
125
        }
126
        if (!empty($this->currentLang['id']) || (0 === $this->currentLang['id'])) {
127
            return $this->currentLang['id'];
128
        }
129
        $msg = 'No valid language set. Use Translation2::setLang().';
130
        return $this->raiseError($msg, TRANSLATION2_ERROR_UNKNOWN_LANG);
131
    }
132
 
133
    // }}}
134
    // {{{ setCharset()
135
 
136
    /**
137
     * Set charset used to read/store the translations
138
     *
139
     * @param string $charset character set (encoding)
140
     *
141
     * @return PEAR_Error on error
142
     */
143
    function setCharset($charset)
144
    {
145
        if (method_exists($this->storage, 'setCharset')) {
146
            return $this->storage->setCharset($charset);
147
        }
148
        return $this->raiseError(TRANSLATION2_ERROR_UNSUPPORTED, null, null,
149
            'method not implemented', __FUNCTION__);
150
    }
151
 
152
    // }}}
153
    // {{{ setLang()
154
 
155
    /**
156
     * Sets the current language
157
     *
158
     * @param string $langID language ID
159
     *
160
     * @return array|PEAR_Error language information
161
     */
162
    function setLang($langID)
163
    {
164
        $res = $this->getLangs(); //load available languages, if not loaded yet
165
        if (PEAR::isError($res)) {
166
            return $res;
167
        }
168
        if (!array_key_exists($langID, $this->langs)) {
169
            return $this->raiseError('unknown language: "'.$langID.'"',
170
                                    TRANSLATION2_ERROR_UNKNOWN_LANG,
171
                                    PEAR_ERROR_RETURN,
172
                                    E_USER_WARNING);
173
        }
174
        $this->currentLang = $this->langs[$langID];
175
        return $this->langs[$langID];
176
    }
177
 
178
    // }}}
179
    // {{{ getLang()
180
 
181
    /**
182
     * Gets the current lang
183
     *
184
     * @param string $format what must be returned
185
     *
186
     * @return mixed array with current lang data or null if not set yet
187
     */
188
    function getLang($format = 'id')
189
    {
190
        return isset($this->currentLang['id']) ? $this->currentLang : null;
191
    }
192
 
193
    // }}}
194
    // {{{ getLangData()
195
 
196
    /**
197
     * Gets the array data for the lang
198
     *
199
     * @param string $langID language ID
200
     * @param string $format what must be returned
201
     *
202
     * @return mixed array with lang data or null if not available
203
     */
204
    function getLangData($langID, $format = 'id')
205
    {
206
        $langs = $this->getLangs('array');
207
        return isset($langs[$langID]) ? $langs[$langID] : null;
208
    }
209
 
210
    // }}}
211
    // {{{ getLangs()
212
 
213
    /**
214
     * Gets the available languages
215
     *
216
     * @param string $format ['array' | 'ids' | 'names' | 'encodings']
217
     *
218
     * @return array|PEAR_Error
219
     */
220
    function getLangs($format = 'array')
221
    {
222
        //if not cached yet, fetch langs data from the container
223
        if (empty($this->langs) || !count($this->langs)) {
224
            $res = $this->fetchLangs(); //container-specific method
225
            if (PEAR::isError($res)) {
226
                return $res;
227
            }
228
        }
229
 
230
        $tmp = array();
231
        switch ($format) {
232
        case 'array':
233
            foreach ($this->langs as $aLang) {
234
                $aLang['lang_id']  = $aLang['id'];
235
                $tmp[$aLang['id']] = $aLang;
236
            }
237
            break;
238
        case 'id':
239
        case 'ids':
240
            foreach ($this->langs as $aLang) {
241
                $tmp[] = $aLang['id'];
242
            }
243
            break;
244
        case 'encoding':
245
        case 'encodings':
246
            foreach ($this->langs as $aLang) {
247
                $tmp[] = $aLang['encoding'];
248
            }
249
            break;
250
        case 'name':
251
        case 'names':
252
        default:
253
            foreach ($this->langs as $aLang) {
254
                $tmp[$aLang['id']] = $aLang['name'];
255
            }
256
        }
257
        return $tmp;
258
    }
259
 
260
    // }}}
261
    // {{{ fetchLangs()
262
 
263
    /**
264
     * Fetch the available langs if they're not cached yet.
265
     * Containers should implement this method.
266
     *
267
     * @return PEAR_Error on error
268
     */
269
    function fetchLangs()
270
    {
271
        return $this->raiseError('method "fetchLangs" not supported',
272
                                 TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
273
    }
274
 
275
    // }}}
276
    // {{{ getPage()
277
 
278
    /**
279
     * Returns an array of the strings in the selected page
280
     * Containers should implement this method.
281
     *
282
     * @param string $pageID page/group ID
283
     * @param string $langID language ID
284
     *
285
     * @return array
286
     */
287
    function getPage($pageID = null, $langID = null)
288
    {
289
        return $this->raiseError('method "getPage" not supported',
290
                                 TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
291
    }
292
 
293
    // }}}
294
    // {{{ getOne()
295
 
296
    /**
297
     * Get a single item from the container, without caching the whole page
298
     * Containers should implement this method.
299
     *
300
     * @param string $stringID string ID
301
     * @param string $pageID   page/group ID
302
     * @param string $langID   language ID
303
     *
304
     * @return string
305
     */
306
    function getOne($stringID, $pageID = null, $langID = null)
307
    {
308
        return $this->raiseError('method "getOne" not supported',
309
                                 TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
310
    }
311
 
312
    // }}}
313
    // {{{ getStringID()
314
 
315
    /**
316
     * Get the stringID for the given string
317
     *
318
     * @param string $string string
319
     * @param string $pageID page/group ID
320
     *
321
     * @return string
322
     */
323
    function getStringID($string, $pageID = null)
324
    {
325
        return $this->raiseError('method "getStringID" not supported',
326
                                 TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
327
    }
328
 
329
    // }}}
330
    // {{{ raiseError()
331
 
332
    /**
333
     * Trigger a PEAR error
334
     *
335
     * @param string $msg    error message
336
     * @param int    $code   error code
337
     * @param int    $mode   PEAR error mode
338
     * @param int    $option error severity
339
     *
340
     * @return void|PEAR_Error
341
     * @access public
342
     */
343
    function raiseError($msg, $code, $mode = PEAR_ERROR_TRIGGER, $option = E_USER_WARNING)
344
    {
345
        if (isset($GLOBALS['_PEAR_default_error_mode'])) {
346
            $mode = $GLOBALS['_PEAR_default_error_mode'];
347
        }
348
        if (isset($GLOBALS['_PEAR_default_error_options'])) {
349
            $option = $GLOBALS['_PEAR_default_error_options'];
350
        }
351
        if ($mode == PEAR_ERROR_RETURN) {
352
            return PEAR::raiseError($msg, $code, $mode, $option);
353
        } else {
354
            PEAR::raiseError($msg, $code, $mode, $option);
355
        }
356
    }
357
 
358
    // }}}
359
}
360
?>