Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| CsvLineConverter | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
9 | |
100.00% |
1 / 1 |
| convert | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
7 | |||
| normalizeLanguage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| normalizeShuffle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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 (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\import\Parser; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\taoQtiItem\model\import\CsvItem; |
| 27 | use oat\taoQtiItem\model\import\Decorator\CvsToQtiTemplateDecorator; |
| 28 | use oat\taoQtiItem\model\import\ItemInterface; |
| 29 | use oat\taoQtiItem\model\import\TemplateInterface; |
| 30 | use oat\taoQtiItem\model\import\Validator\AggregatedValidationException; |
| 31 | |
| 32 | class CsvLineConverter extends ConfigurableService |
| 33 | { |
| 34 | public function convert( |
| 35 | array $line, |
| 36 | TemplateInterface $template, |
| 37 | AggregatedValidationException $exception = null |
| 38 | ): ItemInterface { |
| 39 | $decorator = new CvsToQtiTemplateDecorator($template); |
| 40 | |
| 41 | $validationConfig = $decorator->getCsvColumns(); |
| 42 | $parsed = []; |
| 43 | foreach ($validationConfig as $columnName => $rules) { |
| 44 | if (!empty($rules['parser'])) { |
| 45 | /** @var ColumnParserInterface $parser */ |
| 46 | $parser = $this->getServiceLocator()->get($rules['parser']); |
| 47 | $parsed[$columnName] = $parser->parse($line, $rules, ['columnName' => $columnName]); |
| 48 | } else { |
| 49 | $isInvalid = $exception && $exception->hasColumnWarning($columnName); |
| 50 | $isOmitted = !array_key_exists($columnName, $line) || $line[$columnName] === ''; |
| 51 | $parsed[$columnName] = ($isInvalid || $isOmitted) ? ($rules['default'] ?? null) : $line[$columnName]; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return new CsvItem( |
| 56 | $parsed['name'], |
| 57 | $parsed['question'], |
| 58 | $this->normalizeShuffle($parsed['shuffle']), |
| 59 | (int)$parsed['min_choices'], |
| 60 | (int)$parsed['max_choices'], |
| 61 | $this->normalizeLanguage($parsed['language']), |
| 62 | $parsed['choice_[1-99]'], |
| 63 | $parsed['metadata_[A-Za-z0-9\-_]+'] ?? [] |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | private function normalizeLanguage(string $language): string |
| 68 | { |
| 69 | $groups = explode('-', $language); |
| 70 | |
| 71 | return sprintf('%s-%s', strtolower($groups[0] ?? ''), strtoupper($groups[1] ?? '')); |
| 72 | } |
| 73 | |
| 74 | private function normalizeShuffle($value): bool |
| 75 | { |
| 76 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
| 77 | } |
| 78 | } |