1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReliqArts\DirectTranslator\Vocabulary\Builder; |
6
|
|
|
|
7
|
|
|
use DomainException; |
8
|
|
|
use ReliqArts\DirectTranslator\Utility\LanguageCodeChecker; |
9
|
|
|
use ReliqArts\DirectTranslator\Vocabulary; |
10
|
|
|
use ReliqArts\DirectTranslator\Vocabulary\Builder; |
11
|
|
|
use ReliqArts\DirectTranslator\Vocabulary\Exception\InvalidContent; |
12
|
|
|
use ReliqArts\DirectTranslator\Vocabulary\Standard; |
13
|
|
|
|
14
|
|
|
final class StandardVocabularyBuilder implements Builder |
15
|
|
|
{ |
16
|
|
|
private const CONTENT_KEY_NAME = 'name'; |
17
|
|
|
private const CONTENT_KEY_BASE_LANGUAGE = 'baseLanguage'; |
18
|
|
|
private const CONTENT_KEY_WORDS = 'words'; |
19
|
|
|
private const CONTENT_KEY_PHRASES = 'phrases'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var LanguageCodeChecker |
23
|
|
|
*/ |
24
|
|
|
private $languageCodeValidator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* VocabularyBuilder constructor. |
28
|
|
|
* |
29
|
|
|
* @param LanguageCodeChecker $languageCodeChecker |
30
|
|
|
*/ |
31
|
|
|
public function __construct(LanguageCodeChecker $languageCodeChecker) |
32
|
|
|
{ |
33
|
|
|
$this->languageCodeValidator = $languageCodeChecker; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
* |
39
|
|
|
* @throws InvalidContent |
40
|
|
|
* |
41
|
|
|
* @return Vocabulary |
42
|
|
|
*/ |
43
|
|
|
public function create(array $content): Vocabulary |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
|
|
$this->validateContent($content); |
47
|
|
|
|
48
|
|
|
return new Standard( |
49
|
|
|
$content[self::CONTENT_KEY_NAME], |
50
|
|
|
$content[self::CONTENT_KEY_PHRASES], |
51
|
|
|
$content[self::CONTENT_KEY_WORDS], |
52
|
|
|
$content[self::CONTENT_KEY_BASE_LANGUAGE] |
53
|
|
|
); |
54
|
|
|
} catch (DomainException $exception) { |
55
|
|
|
throw new InvalidContent( |
56
|
|
|
sprintf( |
57
|
|
|
'Content of vocabulary is invalid! %s', |
58
|
|
|
$exception->getMessage() |
59
|
|
|
), |
60
|
|
|
$exception->getCode(), |
61
|
|
|
$exception |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $parsedContent |
68
|
|
|
* |
69
|
|
|
* @throws DomainException |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
private function validateContent(array $parsedContent): bool |
74
|
|
|
{ |
75
|
|
|
$name = $parsedContent[self::CONTENT_KEY_NAME] ?? ''; |
76
|
|
|
$baseLanguage = $parsedContent[self::CONTENT_KEY_BASE_LANGUAGE] ?? ''; |
77
|
|
|
$words = $parsedContent[self::CONTENT_KEY_WORDS] ?? ''; |
78
|
|
|
$phrases = $parsedContent[self::CONTENT_KEY_PHRASES] ?? ''; |
79
|
|
|
|
80
|
|
|
if (empty($name) || !is_string($name)) { |
81
|
|
|
throw new DomainException('Invalid vocabulary name!'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (!$this->languageCodeValidator->languageCodeExists($baseLanguage)) { |
85
|
|
|
throw new DomainException(sprintf('Invalid base language: `%s`', $baseLanguage)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!is_array($words)) { |
89
|
|
|
throw new DomainException('Invalid type specified for words.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (empty($words)) { |
93
|
|
|
throw new DomainException('No words defined for vocabulary!'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!is_array($phrases)) { |
97
|
|
|
throw new DomainException('Invalid type specified for phrases.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|