/library/Zend/Translator/Translator.php

https://github.com/leerbag/zf2 · PHP · 226 lines · 114 code · 26 blank · 86 comment · 20 complexity · 8750065507ba3cf66533a8c489f1d18a MD5 · raw file

  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_Translator
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Translator;
  24. use Zend\Translator\Adapter,
  25. Zend\Translator\Exception\InvalidArgumentException,
  26. Zend\Translator\Exception\BadMethodCallException;
  27. /**
  28. * @uses \Zend\Loader
  29. * @uses \Zend\Translator\Exception\InvalidArgumentException
  30. * @uses \Zend\Translator\Exception\BadMethodCallException
  31. * @category Zend
  32. * @package Zend_Translator
  33. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Translator
  37. {
  38. /**
  39. * Adapter names constants
  40. */
  41. const AN_ARRAY = 'ArrayAdapter';
  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. const LOCALE_DIRECTORY = 'directory';
  51. const LOCALE_FILENAME = 'filename';
  52. /**
  53. * Adapter
  54. *
  55. * @var \Zend\Translator\Adapter\AbstractAdapter
  56. */
  57. private $_adapter;
  58. /**
  59. * Generates the standard translation object
  60. *
  61. * @param array|\Zend\Config $options Options to use
  62. * @throws \Zend\Translator\Exception\InvalidArgumentException
  63. */
  64. public function __construct($options = array())
  65. {
  66. if ($options instanceof \Zend\Config\Config) {
  67. $options = $options->toArray();
  68. } else if (func_num_args() > 1) {
  69. $args = func_get_args();
  70. $options = array();
  71. $options['adapter'] = array_shift($args);
  72. if (!empty($args)) {
  73. $options['content'] = array_shift($args);
  74. }
  75. if (!empty($args)) {
  76. $options['locale'] = array_shift($args);
  77. }
  78. if (!empty($args)) {
  79. $opt = array_shift($args);
  80. $options = array_merge($opt, $options);
  81. }
  82. } else if (!is_array($options)) {
  83. $options = array('adapter' => $options);
  84. }
  85. $this->setAdapter($options);
  86. }
  87. /**
  88. * Sets a new adapter
  89. *
  90. * @param array|\Zend\Config $options Options to use
  91. * @throws \Zend\Translator\Exception\InvalidArgumentException
  92. */
  93. public function setAdapter($options = array())
  94. {
  95. if ($options instanceof \Zend\Config\Config) {
  96. $options = $options->toArray();
  97. } elseif (func_num_args() > 1) {
  98. $args = func_get_args();
  99. $options = array();
  100. $options['adapter'] = array_shift($args);
  101. if (!empty($args)) {
  102. $options['content'] = array_shift($args);
  103. }
  104. if (!empty($args)) {
  105. $options['locale'] = array_shift($args);
  106. }
  107. if (!empty($args)) {
  108. $opt = array_shift($args);
  109. $options = array_merge($opt, $options);
  110. }
  111. } else if (!is_array($options)) {
  112. $options = array('adapter' => $options);
  113. }
  114. if (empty($options['adapter'])) {
  115. throw new InvalidArgumentException("No adapter given");
  116. }
  117. if (\Zend\Loader::isReadable('Zend/Translator/Adapter/' . ucfirst($options['adapter']). '.php')) {
  118. $options['adapter'] = 'Zend\Translator\Adapter\\' . ucfirst($options['adapter']);
  119. }
  120. if (!class_exists($options['adapter'])) {
  121. throw new InvalidArgumentException("Adapter " . $options['adapter'] . " does not exist and cannot be loaded");
  122. }
  123. if (array_key_exists('cache', $options)) {
  124. Adapter\AbstractAdapter::setCache($options['cache']);
  125. }
  126. $adapter = $options['adapter'];
  127. unset($options['adapter']);
  128. $this->_adapter = new $adapter($options);
  129. if (!$this->_adapter instanceof Adapter\AbstractAdapter) {
  130. throw new InvalidArgumentException("Adapter " . $adapter . " does not extend Zend\Translator\Adapter\AbstractAdapter");
  131. }
  132. }
  133. /**
  134. * Returns the adapters name and it's options
  135. *
  136. * @return \Zend\Translator\Adapter\AbstractAdapter
  137. */
  138. public function getAdapter()
  139. {
  140. return $this->_adapter;
  141. }
  142. /**
  143. * Returns the set cache
  144. *
  145. * @return \Zend\Cache\Frontend\Core The set cache
  146. */
  147. public static function getCache()
  148. {
  149. return Adapter\AbstractAdapter::getCache();
  150. }
  151. /**
  152. * Sets a cache for all instances of Zend_Translator
  153. *
  154. * @param \Zend\Cache\Frontend $cache Cache to store to
  155. * @return void
  156. */
  157. public static function setCache(\Zend\Cache\Frontend $cache)
  158. {
  159. Adapter\AbstractAdapter::setCache($cache);
  160. }
  161. /**
  162. * Returns true when a cache is set
  163. *
  164. * @return boolean
  165. */
  166. public static function hasCache()
  167. {
  168. return Adapter\AbstractAdapter::hasCache();
  169. }
  170. /**
  171. * Removes any set cache
  172. *
  173. * @return void
  174. */
  175. public static function removeCache()
  176. {
  177. Adapter\AbstractAdapter::removeCache();
  178. }
  179. /**
  180. * Clears all set cache data
  181. *
  182. * @param string $tag Tag to clear when the default tag name is not used
  183. * @return void
  184. */
  185. public static function clearCache($tag = null)
  186. {
  187. Adapter\AbstractAdapter::clearCache($tag);
  188. }
  189. /**
  190. * Calls all methods from the adapter
  191. * @throws \Zend\Translator\Exception\BadMethodCallException
  192. */
  193. public function __call($method, array $options)
  194. {
  195. if (method_exists($this->_adapter, $method)) {
  196. return call_user_func_array(array($this->_adapter, $method), $options);
  197. }
  198. throw new BadMethodCallException("Unknown method '" . $method . "' called!");
  199. }
  200. }