1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Language; |
4
|
|
|
|
5
|
|
|
use Oc\Repository\Exception\RecordsNotFoundException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class LanguageService |
9
|
|
|
* |
10
|
|
|
* @package Oc\Language |
11
|
|
|
*/ |
12
|
|
|
class LanguageService |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var LanguageRepository |
16
|
|
|
*/ |
17
|
|
|
private $languageRepository; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* LanguageService constructor. |
21
|
|
|
* |
22
|
|
|
* @param LanguageRepository $languageRepository |
23
|
|
|
*/ |
24
|
|
|
public function __construct(LanguageRepository $languageRepository) |
25
|
|
|
{ |
26
|
|
|
$this->languageRepository = $languageRepository; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Fetches all languages. |
31
|
|
|
* |
32
|
|
|
* @return LanguageEntity[] |
33
|
|
|
*/ |
34
|
|
|
public function fetchAll() |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
$result = $this->languageRepository->fetchAll(); |
38
|
|
|
} catch (RecordsNotFoundException $e) { |
39
|
|
|
$result = []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $result; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Fetches all translated languages. |
47
|
|
|
* |
48
|
|
|
* @return LanguageEntity[] |
49
|
|
|
*/ |
50
|
|
|
public function fetchAllTranslated() |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
|
|
$result = $this->languageRepository->fetchAll(); |
54
|
|
|
} catch (RecordsNotFoundException $e) { |
55
|
|
|
$result = []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Creates a language in the database. |
63
|
|
|
* |
64
|
|
|
* @param LanguageEntity $entity |
65
|
|
|
* |
66
|
|
|
* @return LanguageEntity |
67
|
|
|
*/ |
68
|
|
|
public function create(LanguageEntity $entity) |
69
|
|
|
{ |
70
|
|
|
return $this->languageRepository->create($entity); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Update a language in the database. |
75
|
|
|
* |
76
|
|
|
* @param LanguageEntity $entity |
77
|
|
|
* |
78
|
|
|
* @return LanguageEntity |
79
|
|
|
*/ |
80
|
|
|
public function update(LanguageEntity $entity) |
81
|
|
|
{ |
82
|
|
|
return $this->languageRepository->update($entity); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Removes a language from the database. |
87
|
|
|
* |
88
|
|
|
* @param LanguageEntity $entity |
89
|
|
|
* |
90
|
|
|
* @return LanguageEntity |
91
|
|
|
*/ |
92
|
|
|
public function remove(LanguageEntity $entity) |
93
|
|
|
{ |
94
|
|
|
return $this->languageRepository->remove($entity); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Fetches all translated languages and aggregates them to an array of locales. |
99
|
|
|
* |
100
|
|
|
* @return string[] |
101
|
|
|
*/ |
102
|
|
|
public function getAvailableTranslations() |
103
|
|
|
{ |
104
|
|
|
$translatedLanguages = $this->fetchAllTranslated(); |
105
|
|
|
|
106
|
|
|
$locales = []; |
107
|
|
|
|
108
|
|
|
foreach ($translatedLanguages as $translatedLanguage) { |
109
|
|
|
$locales[] = strtolower($translatedLanguage->short); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $locales; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|