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

/src/Symfony/Component/Locale/Stub/StubLocale.php

https://github.com/Exercise/symfony
PHP | 493 lines | 143 code | 43 blank | 307 comment | 4 complexity | b93b754630d088d35c95a99b8ebd17e9 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Locale\Stub;
  11. use Symfony\Component\Locale\Exception\NotImplementedException;
  12. use Symfony\Component\Locale\Exception\MethodNotImplementedException;
  13. /**
  14. * Provides a stub Locale for the 'en' locale.
  15. *
  16. * @author Eriksen Costa <eriksen.costa@infranology.com.br>
  17. */
  18. class StubLocale
  19. {
  20. const DEFAULT_LOCALE = null;
  21. /** Locale method constants */
  22. const ACTUAL_LOCALE = 0;
  23. const VALID_LOCALE = 1;
  24. /** Language tags constants */
  25. const LANG_TAG = 'language';
  26. const EXTLANG_TAG = 'extlang';
  27. const SCRIPT_TAG = 'script';
  28. const REGION_TAG = 'region';
  29. const VARIANT_TAG = 'variant';
  30. const GRANDFATHERED_LANG_TAG = 'grandfathered';
  31. const PRIVATE_TAG = 'private';
  32. /**
  33. * Caches the countries
  34. *
  35. * @var array
  36. */
  37. protected static $countries = array();
  38. /**
  39. * Caches the languages
  40. *
  41. * @var array
  42. */
  43. protected static $languages = array();
  44. /**
  45. * Caches the locales
  46. *
  47. * @var array
  48. */
  49. protected static $locales = array();
  50. /**
  51. * Caches the currencies
  52. *
  53. * @var array
  54. */
  55. protected static $currencies = array();
  56. /**
  57. * Caches the currencies names
  58. *
  59. * @var array
  60. */
  61. protected static $currenciesNames = array();
  62. /**
  63. * Returns the country names for a locale
  64. *
  65. * @param string $locale The locale to use for the country names
  66. *
  67. * @return array The country names with their codes as keys
  68. *
  69. * @throws InvalidArgumentException When the locale is different than 'en'
  70. */
  71. static public function getDisplayCountries($locale)
  72. {
  73. return self::getStubData($locale, 'countries', 'region');
  74. }
  75. /**
  76. * Returns all available country codes
  77. *
  78. * @return array The country codes
  79. */
  80. static public function getCountries()
  81. {
  82. return array_keys(self::getDisplayCountries(self::getDefault()));
  83. }
  84. /**
  85. * Returns the language names for a locale
  86. *
  87. * @param string $locale The locale to use for the language names
  88. *
  89. * @return array The language names with their codes as keys
  90. *
  91. * @throws InvalidArgumentException When the locale is different than 'en'
  92. */
  93. static public function getDisplayLanguages($locale)
  94. {
  95. return self::getStubData($locale, 'languages', 'lang');
  96. }
  97. /**
  98. * Returns all available language codes
  99. *
  100. * @return array The language codes
  101. */
  102. static public function getLanguages()
  103. {
  104. return array_keys(self::getDisplayLanguages(self::getDefault()));
  105. }
  106. /**
  107. * Returns the locale names for a locale
  108. *
  109. * @param string $locale The locale to use for the locale names
  110. *
  111. * @return array The locale names with their codes as keys
  112. *
  113. * @throws InvalidArgumentException When the locale is different than 'en'
  114. */
  115. static public function getDisplayLocales($locale)
  116. {
  117. return self::getStubData($locale, 'locales', 'names');
  118. }
  119. /**
  120. * Returns all available locale codes
  121. *
  122. * @return array The locale codes
  123. */
  124. static public function getLocales()
  125. {
  126. return array_keys(self::getDisplayLocales(self::getDefault()));
  127. }
  128. /**
  129. * Returns the currencies data
  130. *
  131. * @param string $locale
  132. *
  133. * @return array The currencies data
  134. */
  135. static public function getCurrenciesData($locale)
  136. {
  137. return self::getStubData($locale, 'currencies', 'curr');
  138. }
  139. /**
  140. * Returns the currencies names for a locale
  141. *
  142. * @param string $locale The locale to use for the currencies names
  143. *
  144. * @return array The currencies names with their codes as keys
  145. *
  146. * @throws InvalidArgumentException When the locale is different than 'en'
  147. */
  148. static public function getDisplayCurrencies($locale)
  149. {
  150. $currencies = self::getCurrenciesData($locale);
  151. if (!empty(self::$currenciesNames)) {
  152. return self::$currenciesNames;
  153. }
  154. foreach ($currencies as $code => $data) {
  155. self::$currenciesNames[$code] = $data['name'];
  156. }
  157. return self::$currenciesNames;
  158. }
  159. /**
  160. * Returns all available currencies codes
  161. *
  162. * @return array The currencies codes
  163. */
  164. static public function getCurrencies()
  165. {
  166. return array_keys(self::getCurrenciesData(self::getDefault()));
  167. }
  168. /**
  169. * Returns the best available locale based on HTTP "Accept-Language" header according to RFC 2616
  170. *
  171. * @param string $header The string containing the "Accept-Language" header value
  172. *
  173. * @return string The corresponding locale code
  174. *
  175. * @see http://www.php.net/manual/en/locale.acceptfromhttp.php
  176. *
  177. * @throws MethodNotImplementedException
  178. */
  179. static public function acceptFromHttp($header)
  180. {
  181. throw new MethodNotImplementedException(__METHOD__);
  182. }
  183. /**
  184. * Returns a correctly ordered and delimited locale code
  185. *
  186. * @param array $subtags A keyed array where the keys identify the particular locale code subtag
  187. *
  188. * @return string The corresponding locale code
  189. *
  190. * @see http://www.php.net/manual/en/locale.composelocale.php
  191. *
  192. * @throws MethodNotImplementedException
  193. */
  194. static public function composeLocale(array $subtags)
  195. {
  196. throw new MethodNotImplementedException(__METHOD__);
  197. }
  198. /**
  199. * Checks if a language tag filter matches with locale
  200. *
  201. * @param string $langtag The language tag to check
  202. * @param string $locale The language range to check against
  203. * @param Boolean $canonicalize
  204. *
  205. * @return string The corresponding locale code
  206. *
  207. * @see http://www.php.net/manual/en/locale.filtermatches.php
  208. *
  209. * @throws MethodNotImplementedException
  210. */
  211. static public function filterMatches($langtag, $locale, $canonicalize = false)
  212. {
  213. throw new MethodNotImplementedException(__METHOD__);
  214. }
  215. /**
  216. * Returns the variants for the input locale
  217. *
  218. * @param string $locale The locale to extract the variants from
  219. *
  220. * @return array The locale variants
  221. *
  222. * @see http://www.php.net/manual/en/locale.getallvariants.php
  223. *
  224. * @throws MethodNotImplementedException
  225. */
  226. static public function getAllVariants($locale)
  227. {
  228. throw new MethodNotImplementedException(__METHOD__);
  229. }
  230. /**
  231. * Returns the default locale
  232. *
  233. * @return string The default locale code. Always returns 'en'
  234. *
  235. * @see http://www.php.net/manual/en/locale.getdefault.php
  236. *
  237. * @throws MethodNotImplementedException
  238. */
  239. static public function getDefault()
  240. {
  241. return 'en';
  242. }
  243. /**
  244. * Returns the localized display name for the locale language
  245. *
  246. * @param string $locale The locale code to return the display language from
  247. * @param string $inLocale Optional format locale code to use to display the language name
  248. *
  249. * @return string The localized language display name
  250. *
  251. * @see http://www.php.net/manual/en/locale.getdisplaylanguage.php
  252. *
  253. * @throws MethodNotImplementedException
  254. */
  255. static public function getDisplayLanguage($locale, $inLocale = null)
  256. {
  257. throw new MethodNotImplementedException(__METHOD__);
  258. }
  259. /**
  260. * Returns the localized display name for the locale
  261. *
  262. * @param string $locale The locale code to return the display locale name from
  263. * @param string $inLocale Optional format locale code to use to display the locale name
  264. *
  265. * @return string The localized locale display name
  266. *
  267. * @see http://www.php.net/manual/en/locale.getdisplayname.php
  268. *
  269. * @throws MethodNotImplementedException
  270. */
  271. static public function getDisplayName($locale, $inLocale = null)
  272. {
  273. throw new MethodNotImplementedException(__METHOD__);
  274. }
  275. /**
  276. * Returns the localized display name for the locale region
  277. *
  278. * @param string $locale The locale code to return the display region from
  279. * @param string $inLocale Optional format locale code to use to display the region name
  280. *
  281. * @return string The localized region display name
  282. *
  283. * @see http://www.php.net/manual/en/locale.getdisplayregion.php
  284. *
  285. * @throws MethodNotImplementedException
  286. */
  287. static public function getDisplayRegion($locale, $inLocale = null)
  288. {
  289. throw new MethodNotImplementedException(__METHOD__);
  290. }
  291. /**
  292. * Returns the localized display name for the locale script
  293. *
  294. * @param string $locale The locale code to return the display script from
  295. * @param string $inLocale Optional format locale code to use to display the script name
  296. *
  297. * @return string The localized script display name
  298. *
  299. * @see http://www.php.net/manual/en/locale.getdisplayscript.php
  300. *
  301. * @throws MethodNotImplementedException
  302. */
  303. static public function getDisplayScript($locale, $inLocale = null)
  304. {
  305. throw new MethodNotImplementedException(__METHOD__);
  306. }
  307. /**
  308. * Returns the localized display name for the locale variant
  309. *
  310. * @param string $locale The locale code to return the display variant from
  311. * @param string $inLocale Optional format locale code to use to display the variant name
  312. *
  313. * @return string The localized variant display name
  314. *
  315. * @see http://www.php.net/manual/en/locale.getdisplayvariant.php
  316. *
  317. * @throws MethodNotImplementedException
  318. */
  319. static public function getDisplayVariant($locale, $inLocale = null)
  320. {
  321. throw new MethodNotImplementedException(__METHOD__);
  322. }
  323. /**
  324. * Returns the keywords for the locale
  325. *
  326. * @param string $locale The locale code to extract the keywords from
  327. *
  328. * @return array Associative array with the extracted variants
  329. *
  330. * @see http://www.php.net/manual/en/locale.getkeywords.php
  331. *
  332. * @throws MethodNotImplementedException
  333. */
  334. static public function getKeywords($locale)
  335. {
  336. throw new MethodNotImplementedException(__METHOD__);
  337. }
  338. /**
  339. * Returns the primary language for the locale
  340. *
  341. * @param string $locale The locale code to extract the language code from
  342. *
  343. * @return string|null The extracted language code or null in case of error
  344. *
  345. * @see http://www.php.net/manual/en/locale.getprimarylanguage.php
  346. *
  347. * @throws MethodNotImplementedException
  348. */
  349. static public function getPrimaryLanguage($locale)
  350. {
  351. throw new MethodNotImplementedException(__METHOD__);
  352. }
  353. /**
  354. * Returns the region for the locale
  355. *
  356. * @param string $locale The locale code to extract the region code from
  357. *
  358. * @return string|null The extracted region code or null if not present
  359. *
  360. * @see http://www.php.net/manual/en/locale.getregion.php
  361. *
  362. * @throws MethodNotImplementedException
  363. */
  364. static public function getRegion($locale)
  365. {
  366. throw new MethodNotImplementedException(__METHOD__);
  367. }
  368. /**
  369. * Returns the script for the locale
  370. *
  371. * @param string $locale The locale code to extract the script code from
  372. *
  373. * @return string|null The extracted script code or null if not present
  374. *
  375. * @see http://www.php.net/manual/en/locale.getscript.php
  376. *
  377. * @throws MethodNotImplementedException
  378. */
  379. static public function getScript($locale)
  380. {
  381. throw new MethodNotImplementedException(__METHOD__);
  382. }
  383. /**
  384. * Returns the closest language tag for the locale
  385. *
  386. * @param array $langtag A list of the language tags to compare to locale
  387. * @param string $locale The locale to use as the language range when matching
  388. * @param Boolean $canonicalize If true, the arguments will be converted to canonical form before matching
  389. * @param string $default The locale to use if no match is found
  390. *
  391. * @see http://www.php.net/manual/en/locale.lookup.php
  392. *
  393. * @throws RuntimeException When the intl extension is not loaded
  394. */
  395. static public function lookup(array $langtag, $locale, $canonicalize = false, $default = null)
  396. {
  397. throw new MethodNotImplementedException(__METHOD__);
  398. }
  399. /**
  400. * Returns an associative array of locale identifier subtags
  401. *
  402. * @param string $locale The locale code to extract the subtag array from
  403. *
  404. * @return array Associative array with the extracted subtags
  405. *
  406. * @see http://www.php.net/manual/en/locale.parselocale.php
  407. *
  408. * @throws MethodNotImplementedException
  409. */
  410. static public function parseLocale($locale)
  411. {
  412. throw new MethodNotImplementedException(__METHOD__);
  413. }
  414. /**
  415. * Sets the default runtime locale
  416. *
  417. * @param string $locale The locale code
  418. *
  419. * @return Boolean true on success or false on failure
  420. *
  421. * @see http://www.php.net/manual/en/locale.parselocale.php
  422. *
  423. * @throws MethodNotImplementedException
  424. */
  425. static public function setDefault($locale)
  426. {
  427. throw new MethodNotImplementedException(__METHOD__);
  428. }
  429. /**
  430. * Returns the stub ICU data
  431. *
  432. * @param string $locale The locale code
  433. * @param string $cacheVariable The name of a static attribute to cache the data to
  434. * @param string $stubDataDir The stub data directory name
  435. *
  436. * @return array
  437. *
  438. * @throws InvalidArgumentException When the locale is different than 'en'
  439. */
  440. static private function getStubData($locale, $cacheVariable, $stubDataDir)
  441. {
  442. if ('en' != $locale) {
  443. throw new \InvalidArgumentException(sprintf('Only the \'en\' locale is supported. %s', NotImplementedException::INTL_INSTALL_MESSAGE));
  444. }
  445. if (empty(self::${$cacheVariable})) {
  446. self::${$cacheVariable} = include __DIR__.'/../Resources/data/stub/'.$stubDataDir.'/en.php';
  447. }
  448. return self::${$cacheVariable};
  449. }
  450. }