1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Language; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class LanguageService |
7
|
|
|
* |
8
|
|
|
* @package Oc\Language |
9
|
|
|
* @author Nick Lubisch <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class LanguageService |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var LanguageRepository |
15
|
|
|
*/ |
16
|
|
|
private $languageRepository; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* LanguageService constructor. |
20
|
|
|
* |
21
|
|
|
* @param LanguageRepository $languageRepository |
22
|
|
|
*/ |
23
|
|
|
public function __construct(LanguageRepository $languageRepository) |
24
|
|
|
{ |
25
|
|
|
$this->languageRepository = $languageRepository; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Fetches all languages. |
30
|
|
|
* |
31
|
|
|
* @return LanguageEntity[] |
32
|
|
|
*/ |
33
|
|
|
public function fetchAll() |
34
|
|
|
{ |
35
|
|
|
return $this->languageRepository->fetchAll(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Fetches all translated languages. |
40
|
|
|
* |
41
|
|
|
* @return LanguageEntity[] |
42
|
|
|
*/ |
43
|
|
|
public function fetchAllTranslated() |
44
|
|
|
{ |
45
|
|
|
return $this->languageRepository->fetchAllTranslated(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates a language in the database. |
50
|
|
|
* |
51
|
|
|
* @param LanguageEntity $entity |
52
|
|
|
* |
53
|
|
|
* @return LanguageEntity |
54
|
|
|
*/ |
55
|
|
|
public function create(LanguageEntity $entity) |
56
|
|
|
{ |
57
|
|
|
return $this->languageRepository->create($entity); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Update a language in the database. |
62
|
|
|
* |
63
|
|
|
* @param LanguageEntity $entity |
64
|
|
|
* |
65
|
|
|
* @return LanguageEntity |
66
|
|
|
*/ |
67
|
|
|
public function update(LanguageEntity $entity) |
68
|
|
|
{ |
69
|
|
|
return $this->languageRepository->update($entity); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Removes a language from the database. |
74
|
|
|
* |
75
|
|
|
* @param LanguageEntity $entity |
76
|
|
|
* |
77
|
|
|
* @return LanguageEntity |
78
|
|
|
*/ |
79
|
|
|
public function remove(LanguageEntity $entity) |
80
|
|
|
{ |
81
|
|
|
return $this->languageRepository->remove($entity); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Fetches all translated languages and aggregates them to an array of locales. |
86
|
|
|
* |
87
|
|
|
* @return string[] |
88
|
|
|
*/ |
89
|
|
|
public function getAvailableTranslations() |
90
|
|
|
{ |
91
|
|
|
$translatedLanguages = $this->fetchAllTranslated(); |
92
|
|
|
|
93
|
|
|
$locales = []; |
94
|
|
|
|
95
|
|
|
foreach ($translatedLanguages as $translatedLanguage) { |
|
|
|
|
96
|
|
|
$locales[] = strtolower($translatedLanguage->short); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $locales; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.