NumberTextLib.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. App::import('Vendor', 'Tools.Soros/Soros');
  3. if (!class_exists('Soros')) {
  4. throw new CakeException('Vendor class Soros cannot be found');
  5. }
  6. /**
  7. * Wrapper class for Soros number parsing into text
  8. * based on the source code from http://numbertext.org/
  9. *
  10. * @author Pavel Astakhov <pastakhov@yandex.ru>
  11. * @licence LGPL/BSD dual-license
  12. */
  13. class NumberTextLib {
  14. protected static $_dir = null;
  15. public function __construct() {
  16. }
  17. /**
  18. * Set language
  19. *
  20. * @param string $lang (defaults to en_US)
  21. * @return language
  22. * @throws CakeException
  23. */
  24. public static function setLang($lang = null) {
  25. if (!$lang) {
  26. $lang = 'en_US';
  27. }
  28. if (!static::$_dir) {
  29. static::$_dir = CakePlugin::path('Tools') . 'Vendor' . DS . 'Soros' . DS;
  30. }
  31. if (!file_exists(static::$_dir . "$lang.sor")) {
  32. throw new CakeException(sprintf('Language file %s.sor not found', $lang));
  33. }
  34. return $lang;
  35. }
  36. /**
  37. * Number to text conversion
  38. *
  39. * @param Parser $parser
  40. * @param string $input
  41. * @param string $lang default 'en_US'
  42. * @return string
  43. */
  44. public static function numberText($input = '', $lang = '') {
  45. $s = static::getLangModule($lang);
  46. if ($s === null) {
  47. $s = static::load($lang);
  48. }
  49. if ($s === null) {
  50. return null;
  51. }
  52. return $s->run($input);
  53. }
  54. /**
  55. * Money to text conversion
  56. *
  57. * @param Parser $parser
  58. * @param string $input
  59. * @param string $money
  60. * @param string $lang default 'en_US'
  61. * @return string
  62. */
  63. public static function moneyText($input = '', $money = '', $lang = '') {
  64. return static::numbertext($money . " " . $input, $lang);
  65. }
  66. protected static function load($lang) {
  67. $lang = static::setLang($lang);
  68. $file = static::$_dir . "$lang.sor";
  69. $st = file_get_contents($file);
  70. if ($st === false) {
  71. return null;
  72. }
  73. $s = new Soros($st);
  74. if ($lang != null) {
  75. static::addModule([$lang, $s]);
  76. }
  77. return $s;
  78. }
  79. protected static function getModules($m = null) {
  80. static $modules = [];
  81. if (is_array($m)) {
  82. $modules[] = $m;
  83. }
  84. return $modules;
  85. }
  86. protected static function getLangModule($lang) {
  87. $modules = static::getModules();
  88. if (isset($modules[$lang])) {
  89. return $modules[$lang];
  90. }
  91. return null;
  92. }
  93. protected static function addModule($m) {
  94. static::getModules($m);
  95. }
  96. }