Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileDictLoader.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the overtrue/pinyin.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\Pinyin;
  11. use Closure;
  12. class FileDictLoader implements DictLoaderInterface
  13. {
  14. /**
  15. * Words segment name.
  16. *
  17. * @var string
  18. */
  19. protected $segmentName = 'words_%s';
  20. /**
  21. * Dict path.
  22. *
  23. * @var string
  24. */
  25. protected $path;
  26. /**
  27. * Constructor.
  28. *
  29. * @param string $path
  30. */
  31. public function __construct($path)
  32. {
  33. $this->path = $path;
  34. }
  35. /**
  36. * Load dict.
  37. *
  38. * @param Closure $callback
  39. */
  40. public function map(Closure $callback)
  41. {
  42. for ($i = 0; $i < 100; ++$i) {
  43. $segment = $this->path . '/' . sprintf($this->segmentName, $i);
  44. if (file_exists($segment)) {
  45. $dictionary = (array) include $segment;
  46. $callback($dictionary);
  47. }
  48. }
  49. }
  50. /**
  51. * Load surname dict.
  52. *
  53. * @param Closure $callback
  54. */
  55. public function mapSurname(Closure $callback)
  56. {
  57. $surnames = $this->path . '/surnames';
  58. if (file_exists($surnames)) {
  59. $dictionary = (array) include $surnames;
  60. $callback($dictionary);
  61. }
  62. }
  63. }