1<?php
2/**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category   Zend
16 * @package    Zend_Translate
17 * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
19 * @version    $Id$
20 */
21
22/**
23 * @see Zend_Loader
24 */
25
26/**
27 * @see Zend_Translate_Adapter
28 */
29
30
31/**
32 * @category   Zend
33 * @package    Zend_Translate
34 * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
35 * @license    http://framework.zend.com/license/new-bsd     New BSD License
36 */
37class Zend_Translate {
38    /**
39     * Adapter names constants
40     */
41    const AN_ARRAY   = 'Array';
42    const AN_CSV     = 'Csv';
43    const AN_GETTEXT = 'Gettext';
44    const AN_INI     = 'Ini';
45    const AN_QT      = 'Qt';
46    const AN_TBX     = 'Tbx';
47    const AN_TMX     = 'Tmx';
48    const AN_XLIFF   = 'Xliff';
49    const AN_XMLTM   = 'XmlTm';
50
51    const LOCALE_DIRECTORY = 'directory';
52    const LOCALE_FILENAME  = 'filename';
53
54    /**
55     * Adapter
56     *
57     * @var Zend_Translate_Adapter
58     */
59    private $_adapter;
60
61    /**
62     * Generates the standard translation object
63     *
64     * @param  array|Zend_Config|Zend_Translate_Adapter $options Options to use
65     * @param  string|array [$content] Path to content, or content itself
66     * @param  string|Zend_Locale [$locale]
67     * @throws Zend_Translate_Exception
68     */
69    public function __construct($options = array())
70    {
71        if ($options instanceof Zend_Config) {
72            $options = $options->toArray();
73        } else if (func_num_args() > 1) {
74            $args               = func_get_args();
75            $options            = array();
76            $options['adapter'] = array_shift($args);
77            if (!empty($args)) {
78                $options['content'] = array_shift($args);
79            }
80
81            if (!empty($args)) {
82                $options['locale'] = array_shift($args);
83            }
84
85            if (!empty($args)) {
86                $opt     = array_shift($args);
87                $options = array_merge($opt, $options);
88            }
89        } else if (!is_array($options)) {
90            $options = array('adapter' => $options);
91        }
92
93        $this->setAdapter($options);
94    }
95
96    /**
97     * Sets a new adapter
98     *
99     * @param  array|Zend_Config|Zend_Translate_Adapter $options Options to use
100     * @param  string|array [$content] Path to content, or content itself
101     * @param  string|Zend_Locale [$locale]
102     * @throws Zend_Translate_Exception
103     */
104    public function setAdapter($options = array())
105    {
106        if ($options instanceof Zend_Config) {
107            $options = $options->toArray();
108        } else if (func_num_args() > 1) {
109            $args               = func_get_args();
110            $options            = array();
111            $options['adapter'] = array_shift($args);
112            if (!empty($args)) {
113                $options['content'] = array_shift($args);
114            }
115
116            if (!empty($args)) {
117                $options['locale'] = array_shift($args);
118            }
119
120            if (!empty($args)) {
121                $opt     = array_shift($args);
122                $options = array_merge($opt, $options);
123            }
124        } else if (!is_array($options)) {
125            $options = array('adapter' => $options);
126        }
127
128        if (Zend_Loader::isReadable('Zend/Translate/Adapter/' . ucfirst($options['adapter']). '.php')) {
129            $options['adapter'] = 'Zend_Translate_Adapter_' . ucfirst($options['adapter']);
130        }
131
132        if (!class_exists($options['adapter'])) {
133            Zend_Loader::loadClass($options['adapter']);
134        }
135
136        if (array_key_exists('cache', $options)) {
137            Zend_Translate_Adapter::setCache($options['cache']);
138        }
139
140        $adapter = $options['adapter'];
141        unset($options['adapter']);
142        $this->_adapter = new $adapter($options);
143        if (!$this->_adapter instanceof Zend_Translate_Adapter) {
144            throw new Zend_Translate_Exception("Adapter " . $adapter . " does not extend Zend_Translate_Adapter");
145        }
146    }
147
148    /**
149     * Returns the adapters name and it's options
150     *
151     * @return Zend_Translate_Adapter
152     */
153    public function getAdapter()
154    {
155        return $this->_adapter;
156    }
157
158    /**
159     * Returns the set cache
160     *
161     * @return Zend_Cache_Core The set cache
162     */
163    public static function getCache()
164    {
165        return Zend_Translate_Adapter::getCache();
166    }
167
168    /**
169     * Sets a cache for all instances of Zend_Translate
170     *
171     * @param  Zend_Cache_Core $cache Cache to store to
172     * @return void
173     */
174    public static function setCache(Zend_Cache_Core $cache)
175    {
176        Zend_Translate_Adapter::setCache($cache);
177    }
178
179    /**
180     * Returns true when a cache is set
181     *
182     * @return boolean
183     */
184    public static function hasCache()
185    {
186        return Zend_Translate_Adapter::hasCache();
187    }
188
189    /**
190     * Removes any set cache
191     *
192     * @return void
193     */
194    public static function removeCache()
195    {
196        Zend_Translate_Adapter::removeCache();
197    }
198
199    /**
200     * Clears all set cache data
201     *
202     * @param string $tag Tag to clear when the default tag name is not used
203     * @return void
204     */
205    public static function clearCache($tag = null)
206    {
207        Zend_Translate_Adapter::clearCache($tag);
208    }
209
210    /**
211     * Calls all methods from the adapter
212     */
213    public function __call($method, array $options)
214    {
215        if (method_exists($this->_adapter, $method)) {
216            return call_user_func_array(array($this->_adapter, $method), $options);
217        }
218        throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");
219    }
220}
221