酒店预订平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

77 rader
1.4 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. * Dict File loader.
  14. */
  15. class FileDictLoader implements DictLoaderInterface
  16. {
  17. /**
  18. * Words segment name.
  19. *
  20. * @var string
  21. */
  22. protected $segmentName = 'words_%s';
  23. /**
  24. * Dict path.
  25. *
  26. * @var string
  27. */
  28. protected $path;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $path
  33. */
  34. public function __construct($path)
  35. {
  36. $this->path = $path;
  37. }
  38. /**
  39. * Load dict.
  40. *
  41. * @param Closure $callback
  42. */
  43. public function map(Closure $callback)
  44. {
  45. for ($i = 0; $i < 100; ++$i) {
  46. $segment = $this->path.'/'.sprintf($this->segmentName, $i);
  47. if (file_exists($segment)) {
  48. $dictionary = (array) include $segment;
  49. $callback($dictionary);
  50. }
  51. }
  52. }
  53. /**
  54. * Load surname dict.
  55. *
  56. * @param Closure $callback
  57. */
  58. public function mapSurname(Closure $callback)
  59. {
  60. $surnames = $this->path.'/surnames';
  61. if (file_exists($surnames)) {
  62. $dictionary = (array) include $surnames;
  63. $callback($dictionary);
  64. }
  65. }
  66. }