NumberText.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * Sample classes
  8. * Is 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 Numbertext {
  14. public function __construct() {
  15. }
  16. /**
  17. * Number to text conversion
  18. *
  19. * @param Parser $parser
  20. * @param string $input
  21. * @param string $lang default 'en_US'
  22. * @return string
  23. */
  24. public static function numberText($input = '', $lang = '') {
  25. $s = self::getLangModule($lang);
  26. if (is_null($s))
  27. $s = self::load($lang);
  28. if (is_null($s))
  29. return null;
  30. return $s->run($input);
  31. }
  32. /**
  33. * Money to text conversion
  34. *
  35. * @param Parser $parser
  36. * @param string $input
  37. * @param string $money
  38. * @param string $lang default 'en_US'
  39. * @return string
  40. */
  41. public static function moneyText($input = '', $money = '', $lang = '') {
  42. return self::numbertext($money . " " . $input, $lang);
  43. }
  44. private static function load($lang) {
  45. $url = __dir__ . "/$lang.sor";
  46. $st = file_get_contents($url);
  47. if ($st === false)
  48. return null;
  49. $s = new Soros($st);
  50. if ($lang != null)
  51. self::addModule([$lang, $s]);
  52. return $s;
  53. }
  54. private static function getModules($m = null) {
  55. static $modules = [];
  56. if (is_array($m))
  57. $modules[] = $m;
  58. return $modules;
  59. }
  60. private static function getLangModule($lang) {
  61. $modules = self::getModules();
  62. if (isset($modules[$lang]))
  63. return $modules[$lang];
  64. return null;
  65. }
  66. private static function addModule($m) {
  67. self::getModules($m);
  68. }
  69. }