PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Translator/Translator.php

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