Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| SupportedLanguageRule | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| validate | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| getLanguages | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\import\Validator\Rule; |
| 24 | |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\taoQtiItem\model\import\Validator\WarningValidationException; |
| 28 | use tao_helpers_I18n; |
| 29 | use tao_models_classes_LanguageService; |
| 30 | |
| 31 | class SupportedLanguageRule extends ConfigurableService implements ValidationRuleInterface |
| 32 | { |
| 33 | /** @var array */ |
| 34 | private $languages; |
| 35 | |
| 36 | /** |
| 37 | * @inheritDoc |
| 38 | */ |
| 39 | public function validate(string $column, $value, $rules = null, array $context = []): void |
| 40 | { |
| 41 | if (false === array_search(strtolower($value), $this->getLanguages())) { |
| 42 | $exception = new WarningValidationException( |
| 43 | '`%s` is invalid', |
| 44 | [ |
| 45 | $column, |
| 46 | ] |
| 47 | ); |
| 48 | |
| 49 | $exception->setColumn($column); |
| 50 | |
| 51 | throw $exception; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private function getLanguages(): array |
| 56 | { |
| 57 | if (null === $this->languages) { |
| 58 | $this->languages = array_map( |
| 59 | 'strtolower', |
| 60 | array_keys( |
| 61 | tao_helpers_I18n::getAvailableLangsByUsage( |
| 62 | new core_kernel_classes_Resource( |
| 63 | tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_GUI |
| 64 | ) |
| 65 | ) |
| 66 | ) |
| 67 | ); |
| 68 | } |
| 69 | return $this->languages; |
| 70 | } |
| 71 | } |