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