File   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 2
b 0
f 0
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A map() 0 9 3
A mapSurname() 0 7 2
1
<?php
2
/**
3
 * This file is part of the mucts/pinyin.
4
 *
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 *
8
 * @version 1.0
9
 * @author herry<[email protected]>
10
 * @copyright © 2020 MuCTS.com All Rights Reserved.
11
 */
12
13
namespace MuCTS\Pinyin\Loaders;
14
15
use Closure;
16
use MuCTS\Pinyin\Exceptions\PinyinException;
17
use MuCTS\Pinyin\Interfaces\DictLoader;
18
use MuCTS\Support\Arr;
19
20
class File implements DictLoader
21
{
22
    /**
23
     * Words segment name.
24
     *
25
     * @var string
26
     */
27
    protected string $segmentName = 'words_*';
28
29
    /**
30
     * Dict path.
31
     *
32
     * @var string
33
     */
34
    protected string $path;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param string $path
40
     */
41
    public function __construct($path)
42
    {
43
        $this->path = $path;
44
    }
45
46
    /**
47
     * Load dict.
48
     *
49
     * @param Closure $callback
50
     * @throws PinyinException
51
     */
52
    public function map(Closure $callback)
53
    {
54
        $segments = glob($this->path . '/' . $this->segmentName);
55
        if ($segments == false) {
56
            throw new PinyinException('CC-CEDICT dictionary data does not exist');
57
        }
58
        while (($segment = array_shift($segments))) {
59
            $dictionary = Arr::wrap(include $segment);
60
            $callback($dictionary);
61
        }
62
    }
63
64
    /**
65
     * Load surname dict.
66
     *
67
     * @param Closure $callback
68
     */
69
    public function mapSurname(Closure $callback)
70
    {
71
        $surnames = $this->path . '/surnames';
72
73
        if (file_exists($surnames)) {
74
            $dictionary = Arr::wrap(include $surnames);
75
            $callback($dictionary);
76
        }
77
    }
78
}
79