Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
28 / 28 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
LanguageAllowedFilter | |
100.00% |
28 / 28 |
|
100.00% |
3 / 3 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
filterByLanguageCollection | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
filterByValueCollection | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2024 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * @author Gabriel Felipe Soares <gabriel.felipe.soares@taotesting.com> |
21 | */ |
22 | |
23 | declare(strict_types=1); |
24 | |
25 | namespace oat\tao\model\Language\Filter; |
26 | |
27 | use oat\tao\model\Language\Language; |
28 | use oat\tao\model\Language\LanguageCollection; |
29 | use oat\tao\model\Lists\Business\Domain\ValueCollection; |
30 | use oat\tao\model\TaoOntology; |
31 | |
32 | class LanguageAllowedFilter |
33 | { |
34 | public const TAO_ALLOWED_TRANSLATION_LOCALES = 'TAO_ALLOWED_TRANSLATION_LOCALES'; |
35 | |
36 | /** @var string[] */ |
37 | private array $allowedLocales; |
38 | |
39 | public function __construct(?string $allowedLocales = null) |
40 | { |
41 | $this->allowedLocales = array_filter( |
42 | explode( |
43 | ',', |
44 | str_replace( |
45 | ' ', |
46 | '', |
47 | (string)$allowedLocales |
48 | ) |
49 | ) |
50 | ); |
51 | } |
52 | |
53 | public function filterByLanguageCollection(LanguageCollection $collection): LanguageCollection |
54 | { |
55 | if (empty($this->allowedLocales)) { |
56 | return $collection; |
57 | } |
58 | |
59 | $indicesToUnset = []; |
60 | |
61 | /** @var Language $item */ |
62 | foreach ($collection as $key => $item) { |
63 | if (!in_array($item->getCode(), $this->allowedLocales, true)) { |
64 | $indicesToUnset[] = $key; |
65 | } |
66 | } |
67 | |
68 | foreach ($indicesToUnset as $index) { |
69 | $collection->offsetUnset($index); |
70 | } |
71 | |
72 | return $collection; |
73 | } |
74 | |
75 | public function filterByValueCollection(ValueCollection $collection): ValueCollection |
76 | { |
77 | if (empty($this->allowedLocales)) { |
78 | return $collection; |
79 | } |
80 | |
81 | $allowedUris = []; |
82 | |
83 | foreach ($this->allowedLocales as $locale) { |
84 | $allowedUris[] = sprintf('%s%s', TaoOntology::LANGUAGE_PREFIX, $locale); |
85 | } |
86 | |
87 | foreach ($collection->getUris() as $uri) { |
88 | if (!in_array($uri, $allowedUris, true)) { |
89 | $collection->removeValueByUri($uri); |
90 | } |
91 | } |
92 | |
93 | return $collection; |
94 | } |
95 | } |