Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
20.00% |
6 / 30 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| LineValidator | |
20.00% |
6 / 30 |
|
0.00% |
0 / 2 |
61.20 | |
0.00% |
0 / 1 |
| validate | |
20.69% |
6 / 29 |
|
0.00% |
0 / 1 |
49.41 | |||
| getValidatorMapper | |
0.00% |
0 / 1 |
|
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) 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\Validator; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\taoQtiItem\model\import\Decorator\CvsToQtiTemplateDecorator; |
| 27 | use oat\taoQtiItem\model\import\TemplateInterface; |
| 28 | |
| 29 | class LineValidator extends ConfigurableService implements ValidatorInterface |
| 30 | { |
| 31 | /** |
| 32 | * @inheritDoc |
| 33 | */ |
| 34 | public function validate(array $content, TemplateInterface $csvTemplate): void |
| 35 | { |
| 36 | $logger = $this->getLogger(); |
| 37 | $decorator = new CvsToQtiTemplateDecorator($csvTemplate); |
| 38 | $warnings = []; |
| 39 | $errors = []; |
| 40 | |
| 41 | foreach ($decorator->getCsvColumns() as $headerRegex => $validations) { |
| 42 | $validations = $validations['value'] ?? ''; |
| 43 | |
| 44 | foreach (explode('|', $validations) as $validation) { |
| 45 | $rules = explode(':', $validation); |
| 46 | $name = array_shift($rules); |
| 47 | $validator = $this->getValidatorMapper()->getValidator($name); |
| 48 | |
| 49 | if (!$validator) { |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | $exception = null; |
| 54 | $validatedValue = $content[$headerRegex] ?? null; |
| 55 | |
| 56 | try { |
| 57 | $validator->validate($headerRegex, $validatedValue, $rules, $content); |
| 58 | } catch (ErrorValidationException $exception) { |
| 59 | $errors[] = $exception; |
| 60 | } catch (WarningValidationException $exception) { |
| 61 | $warnings[] = $exception; |
| 62 | } finally { |
| 63 | if ($exception) { |
| 64 | $logger->debug( |
| 65 | sprintf( |
| 66 | 'Tabular import: failed validation on %s by "%s" validator', |
| 67 | $validatedValue ?? '', |
| 68 | $name |
| 69 | ) |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (!empty($warnings) || !empty($errors)) { |
| 77 | throw new AggregatedValidationException($errors, $warnings); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private function getValidatorMapper(): ValidationRulesMapper |
| 82 | { |
| 83 | return $this->getServiceLocator()->get(ValidationRulesMapper::class); |
| 84 | } |
| 85 | } |