Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| LanguageProcessor | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
| filterByLanguage | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
42 | |||
| filterByAvailableLanguage | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 | |||
| parseTranslatedValue | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| parseTranslatedLang | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2023 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\generis\model\kernel\persistence\starsql; |
| 23 | |
| 24 | class LanguageProcessor |
| 25 | { |
| 26 | public const LANGUAGE_TAGGED_VALUE_PATTERN = "/^(.*)@([a-zA-Z\\-]{5,6})$/"; |
| 27 | |
| 28 | public function filterByLanguage($entries, $allowedLanguages): array |
| 29 | { |
| 30 | if (empty($entries)) { |
| 31 | return []; |
| 32 | } |
| 33 | |
| 34 | $filteredValues = []; |
| 35 | foreach ($entries as $entry) { |
| 36 | // collect all entries with matching language or without language |
| 37 | $matchSuccess = preg_match(self::LANGUAGE_TAGGED_VALUE_PATTERN, $entry, $matches); |
| 38 | if (!$matchSuccess) { |
| 39 | $filteredValues[] = $entry; |
| 40 | } elseif (isset($matches[2]) && in_array($matches[2], $allowedLanguages)) { |
| 41 | $filteredValues[] = $matches[1]; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return $filteredValues; |
| 46 | } |
| 47 | |
| 48 | public function filterByAvailableLanguage($entries, $dataLanguage, $defaultLanguage): array |
| 49 | { |
| 50 | if (empty($entries)) { |
| 51 | return []; |
| 52 | } |
| 53 | |
| 54 | $fallbackLanguage = ''; |
| 55 | |
| 56 | foreach ($entries as $entry) { |
| 57 | preg_match(self::LANGUAGE_TAGGED_VALUE_PATTERN, $entry, $matches); |
| 58 | $entryLang = $matches[2] ?? $fallbackLanguage; |
| 59 | $sortedResults[$entryLang][] = $matches[1] ?? $entry; |
| 60 | } |
| 61 | |
| 62 | $languageOrderedEntries = [ |
| 63 | $dataLanguage, |
| 64 | $defaultLanguage, |
| 65 | $fallbackLanguage, |
| 66 | ]; |
| 67 | |
| 68 | $returnValue = []; |
| 69 | foreach ($languageOrderedEntries as $language) { |
| 70 | if (isset($sortedResults[$language])) { |
| 71 | $returnValue = $sortedResults[$language]; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return (array) $returnValue; |
| 77 | } |
| 78 | |
| 79 | public function parseTranslatedValue($value): string |
| 80 | { |
| 81 | preg_match(self::LANGUAGE_TAGGED_VALUE_PATTERN, (string)$value, $matches); |
| 82 | |
| 83 | return $matches[1] ?? (string) $value; |
| 84 | } |
| 85 | |
| 86 | public function parseTranslatedLang($value): string |
| 87 | { |
| 88 | preg_match(self::LANGUAGE_TAGGED_VALUE_PATTERN, (string)$value, $matches); |
| 89 | |
| 90 | return $matches[2] ?? ''; |
| 91 | } |
| 92 | } |