酒店预订平台
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.
 
 
 
 
 
 

97 lines
1.7 KiB

  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. /**
  13. * Memory Dict File loader.
  14. */
  15. class MemoryFileDictLoader implements DictLoaderInterface
  16. {
  17. /**
  18. * Data directory.
  19. *
  20. * @var string
  21. */
  22. protected $path;
  23. /**
  24. * Words segment name.
  25. *
  26. * @var string
  27. */
  28. protected $segmentName = 'words_%s';
  29. /**
  30. * Segment files.
  31. *
  32. * @var array
  33. */
  34. protected $segments = array();
  35. /**
  36. * Surname cache.
  37. *
  38. * @var array
  39. */
  40. protected $surnames = array();
  41. /**
  42. * Constructor.
  43. *
  44. * @param string $path
  45. */
  46. public function __construct($path)
  47. {
  48. $this->path = $path;
  49. for ($i = 0; $i < 100; ++$i) {
  50. $segment = $path.'/'.sprintf($this->segmentName, $i);
  51. if (file_exists($segment)) {
  52. $this->segments[] = (array) include $segment;
  53. }
  54. }
  55. }
  56. /**
  57. * Load dict.
  58. *
  59. * @param Closure $callback
  60. */
  61. public function map(Closure $callback)
  62. {
  63. foreach ($this->segments as $dictionary) {
  64. $callback($dictionary);
  65. }
  66. }
  67. /**
  68. * Load surname dict.
  69. *
  70. * @param Closure $callback
  71. */
  72. public function mapSurname(Closure $callback)
  73. {
  74. if (empty($this->surnames)) {
  75. $surnames = $this->path.'/surnames';
  76. if (file_exists($surnames)) {
  77. $this->surnames = (array) include $surnames;
  78. }
  79. }
  80. $callback($this->surnames);
  81. }
  82. }