1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace KRDigital\NamesDetector\Config; |
6
|
|
|
|
7
|
|
|
use KRDigital\NamesDetector\Dictionary\Dictionary; |
8
|
|
|
use KRDigital\NamesDetector\Dictionary\DictionaryInterface; |
9
|
|
|
use KRDigital\NamesDetector\Exception\InvalidDictionarySourceException; |
10
|
|
|
|
11
|
|
|
class Config implements ConfigInterface |
12
|
|
|
{ |
13
|
|
|
private const EXTENSION_JSON = 'json'; |
14
|
|
|
|
15
|
|
|
private const EXTENSION_PHP = 'php'; |
16
|
|
|
|
17
|
|
|
private const ALLOWED_EXTENSIONS = [ |
18
|
|
|
self::EXTENSION_JSON, |
19
|
|
|
self::EXTENSION_PHP, |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
protected $dictionaryPath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array|null |
29
|
|
|
*/ |
30
|
|
|
protected $dictionaryData; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var DictionaryInterface |
34
|
|
|
*/ |
35
|
|
|
protected $dictionary; |
36
|
|
|
|
37
|
5 |
|
public function __construct(string $dictionaryPath = null, array $dictionaryData = []) |
38
|
|
|
{ |
39
|
5 |
|
if (empty($dictionaryData) && null === $dictionaryPath) { |
40
|
|
|
throw new InvalidDictionarySourceException('No input data is provided'); |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
if (null !== $dictionaryPath && !\file_exists($dictionaryPath)) { |
44
|
1 |
|
throw new InvalidDictionarySourceException(\sprintf('Path %s is invalid', $dictionaryPath)); |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
$this->dictionaryPath = $dictionaryPath; |
48
|
4 |
|
$this->dictionaryData = $dictionaryData; |
49
|
4 |
|
} |
50
|
|
|
|
51
|
4 |
|
public function getDictionary(): DictionaryInterface |
52
|
|
|
{ |
53
|
4 |
|
if (null === $this->dictionary) { |
54
|
4 |
|
$this->dictionary = $this->initDictionary(); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
return $this->dictionary; |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
protected function initDictionary(): DictionaryInterface |
61
|
|
|
{ |
62
|
4 |
|
if (!empty($this->dictionaryData)) { |
63
|
1 |
|
return Dictionary::fromArray($this->dictionaryData); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
return $this->createDictionaryFromFile($this->dictionaryPath); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
protected function createDictionaryFromFile(string $dictionaryPath): DictionaryInterface |
70
|
|
|
{ |
71
|
3 |
|
$dictionaryExtension = \pathinfo($dictionaryPath, PATHINFO_EXTENSION); |
72
|
|
|
|
73
|
|
|
switch ($dictionaryExtension) { |
74
|
3 |
|
case self::EXTENSION_PHP: |
75
|
|
|
try { |
76
|
1 |
|
$data = include $dictionaryPath; |
77
|
|
|
|
78
|
1 |
|
return Dictionary::fromArray($data); |
79
|
|
|
} catch (\ParseError $error) { |
80
|
|
|
throw new InvalidDictionarySourceException('Dictionary php file is invalid'); |
81
|
|
|
} |
82
|
2 |
|
case self::EXTENSION_JSON: |
83
|
|
|
// JSON_THROW_ON_ERROR polyfill |
84
|
1 |
|
if (null === $data = \json_decode(\file_get_contents($dictionaryPath), true)) { |
85
|
|
|
throw new InvalidDictionarySourceException('Dictionary json file is invalid'); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return Dictionary::fromArray($data); |
89
|
|
|
default: |
90
|
1 |
|
throw new InvalidDictionarySourceException(\sprintf('Extension %s is not valid, allowed extensions are %s', $dictionaryExtension, \implode(', ', self::ALLOWED_EXTENSIONS))); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|