|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EasyDictionary; |
|
6
|
|
|
|
|
7
|
|
|
use EasyDictionary\Exception\InvalidConfigurationException; |
|
8
|
|
|
use EasyDictionary\Exception\RuntimeException; |
|
9
|
|
|
use EasyDictionary\Interfaces\ConfigInterface; |
|
10
|
|
|
use EasyDictionary\Interfaces\DataProviderInterface; |
|
11
|
|
|
use EasyDictionary\Interfaces\DictionaryInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Manager |
|
15
|
|
|
* |
|
16
|
|
|
* @package EasyDictionary |
|
17
|
|
|
*/ |
|
|
|
|
|
|
18
|
|
|
class Manager |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
|
|
|
|
|
21
|
|
|
* @var ConfigInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $config; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
|
|
|
|
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $dictionaries = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
|
|
|
|
|
31
|
|
|
* @param ConfigInterface $config |
|
|
|
|
|
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(ConfigInterface $config = null) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!is_null($config)) { |
|
36
|
|
|
$this->setConfig($config); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns Dictionary object |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $name |
|
|
|
|
|
|
44
|
|
|
* |
|
45
|
|
|
* @return DictionaryInterface |
|
46
|
|
|
* @throws InvalidConfigurationException |
|
47
|
|
|
* @throws RuntimeException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function get(string $name): ?DictionaryInterface |
|
50
|
|
|
{ |
|
51
|
|
|
if (!isset($this->dictionaries[$name])) { |
|
52
|
|
|
$config = $this->getConfig(); |
|
53
|
|
|
if (!$config) { |
|
54
|
|
|
throw new RuntimeException(sprintf('Config not found', $name)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$dictionaryConfig = $config->getDictionaryConfig()[$name] ?? null; |
|
58
|
|
|
if (!$dictionaryConfig) { |
|
59
|
|
|
throw new RuntimeException(sprintf('Dictionary with key "%s" not found', $name)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->add($this->create($name, $dictionaryConfig)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->dictionaries[$name] ?? null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
|
|
|
|
|
69
|
|
|
* @return ConfigInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getConfig(): ?ConfigInterface |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->config; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
|
|
|
|
|
77
|
|
|
* @param ConfigInterface $config |
|
|
|
|
|
|
78
|
|
|
* |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setConfig(ConfigInterface $config) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->config = $config; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
|
|
|
|
|
89
|
|
|
* @param DictionaryInterface $dictionary |
|
|
|
|
|
|
90
|
|
|
* |
|
91
|
|
|
* @return $this |
|
92
|
|
|
* @throws RuntimeException |
|
93
|
|
|
*/ |
|
94
|
|
|
public function add(DictionaryInterface $dictionary) |
|
95
|
|
|
{ |
|
96
|
|
|
$name = $dictionary->getName(); |
|
97
|
|
|
|
|
98
|
|
|
if (isset($this->dictionaries[$name])) { |
|
99
|
|
|
throw new RuntimeException(sprintf('The dictionary with key "%s" already exists', $name)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->dictionaries[$name] = $dictionary; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
|
|
|
|
|
108
|
|
|
* @param $name |
|
|
|
|
|
|
109
|
|
|
* @param $dictionaryConfig |
|
|
|
|
|
|
110
|
|
|
* @return DictionaryInterface |
|
|
|
|
|
|
111
|
|
|
* @throws InvalidConfigurationException |
|
|
|
|
|
|
112
|
|
|
*/ |
|
113
|
|
|
protected function create(string $name, array $dictionaryConfig): DictionaryInterface |
|
114
|
|
|
{ |
|
115
|
|
|
$config = $this->getConfig(); |
|
116
|
|
|
|
|
117
|
|
|
$dataProvider = $this->createDataProvider( |
|
118
|
|
|
$dictionaryConfig['data']['class'] ?? $config->getDefaultDataProviderClass(), |
|
119
|
|
|
$dictionaryConfig['data'] ?? [] |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
$dictionary = $this->createDictionary( |
|
123
|
|
|
$dictionaryConfig['class'] ?? $config->getDefaultDictionaryClass() |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
$dictionary->setName($name); |
|
127
|
|
|
$dictionary->setDataProvider($dataProvider); |
|
128
|
|
|
$dictionary->setDefaultView($dictionaryConfig['view'] ?? ($config->getDefaultView() ?? null)); |
|
129
|
|
|
$dictionary->setSearchFields($dictionaryConfig['searchFields'] ?? []); |
|
130
|
|
|
|
|
131
|
|
|
if (isset($dictionaryConfig['cache'])) { |
|
132
|
|
|
$dictionary->setCache( |
|
133
|
|
|
$config->getCache($dictionaryConfig['cache']), |
|
|
|
|
|
|
134
|
|
|
$dictionaryConfig['cacheTTL'] ?? ConfigInterface::DEFAULT_CACHE_TTL |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $dictionary; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
|
|
|
|
|
142
|
|
|
* @param string $class |
|
|
|
|
|
|
143
|
|
|
* @param array $config |
|
|
|
|
|
|
144
|
|
|
* |
|
145
|
|
|
* @return DataProviderInterface |
|
146
|
|
|
* @throws InvalidConfigurationException |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function createDataProvider(string $class, array $config): DataProviderInterface |
|
149
|
|
|
{ |
|
150
|
|
|
if (!class_exists($class)) { |
|
151
|
|
|
throw new InvalidConfigurationException(sprintf('Class "%s" not found', $class)); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$dataProvider = new $class($config); |
|
155
|
|
|
|
|
156
|
|
|
if (!($dataProvider instanceof DataProviderInterface)) { |
|
157
|
|
|
throw new InvalidConfigurationException(sprintf('Class "%s" is not implement required interface', $class)); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $dataProvider; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
|
|
|
|
|
164
|
|
|
* @param string $class |
|
|
|
|
|
|
165
|
|
|
* |
|
166
|
|
|
* @return DictionaryInterface |
|
167
|
|
|
* @throws InvalidConfigurationException |
|
168
|
|
|
*/ |
|
169
|
|
|
public function createDictionary(string $class): DictionaryInterface |
|
170
|
|
|
{ |
|
171
|
|
|
if (!class_exists($class)) { |
|
172
|
|
|
throw new InvalidConfigurationException(sprintf('Class "%s" not found', $class)); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$dictionary = new $class; |
|
176
|
|
|
|
|
177
|
|
|
if (!($dictionary instanceof DictionaryInterface)) { |
|
178
|
|
|
throw new InvalidConfigurationException(sprintf('Class "%s" not found', $class)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $dictionary; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|