1 | <?php |
||
16 | class FileDictLoader implements DictLoaderInterface |
||
17 | { |
||
18 | /** |
||
19 | * Words segment name. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $segmentName = 'words_%s'; |
||
24 | |||
25 | /** |
||
26 | * Dict path. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $path; |
||
31 | |||
32 | /** |
||
33 | * Segment files |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $segments = array(); |
||
38 | |||
39 | /** |
||
40 | * Constructor. |
||
41 | * |
||
42 | * @param string $path |
||
43 | * @param boolean $preload |
||
44 | */ |
||
45 | 9 | public function __construct($path, $preload = false) |
|
46 | { |
||
47 | 9 | $this->path = $path; |
|
48 | 9 | for ($i = 0; $i < 100; ++$i) { |
|
49 | 9 | $segment = $this->path . '/' . sprintf($this->segmentName, $i); |
|
50 | 9 | if (file_exists($segment)) { |
|
51 | 9 | $this->segments[$i] = array('segment' => $segment); |
|
52 | 9 | if ($preload) { |
|
53 | $this->segments[$i]['dictionary'] = (array) include $segment; |
||
54 | } |
||
55 | 9 | } |
|
56 | 9 | } |
|
57 | 9 | } |
|
58 | |||
59 | /** |
||
60 | * Load dict. |
||
61 | * |
||
62 | * @param Closure $callback |
||
63 | */ |
||
64 | 9 | public function map(Closure $callback) |
|
72 | |||
73 | /** |
||
74 | * Load surname dict. |
||
75 | * |
||
76 | * @param Closure $callback |
||
77 | */ |
||
78 | 1 | public function mapSurname(Closure $callback) |
|
87 | } |
||
88 |