Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| MetadataValuesExtractor | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| extract | |
100.00% |
14 / 14 |
|
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) 2022 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\StatisticalMetadata\Import\Extractor; |
| 24 | |
| 25 | use core_kernel_classes_Property; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use oat\tao\model\StatisticalMetadata\Import\Exception\AbstractValidationException; |
| 28 | use oat\tao\model\StatisticalMetadata\Import\Exception\AggregatedValidationException; |
| 29 | use oat\tao\model\StatisticalMetadata\Import\Validator\ResourceMetadataRelationValidator; |
| 30 | |
| 31 | class MetadataValuesExtractor |
| 32 | { |
| 33 | /** @var MetadataHeadersExtractor */ |
| 34 | private $metadataHeadersExtractor; |
| 35 | |
| 36 | /** @var ResourceMetadataRelationValidator */ |
| 37 | private $resourceMetadataRelationValidator; |
| 38 | |
| 39 | public function __construct( |
| 40 | MetadataHeadersExtractor $metadataHeadersExtractor, |
| 41 | ResourceMetadataRelationValidator $resourceMetadataRelationValidator |
| 42 | ) { |
| 43 | $this->metadataHeadersExtractor = $metadataHeadersExtractor; |
| 44 | $this->resourceMetadataRelationValidator = $resourceMetadataRelationValidator; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param core_kernel_classes_Property[] $metadataProperties |
| 49 | */ |
| 50 | public function extract(array $record, core_kernel_classes_Resource $resource, array $metadataProperties): array |
| 51 | { |
| 52 | $values = []; |
| 53 | $exceptions = []; |
| 54 | $metadataHeaders = $this->metadataHeadersExtractor->extract(array_keys($record)); |
| 55 | |
| 56 | foreach ($metadataHeaders as $metadataHeader) { |
| 57 | $value = trim($record[$metadataHeader]); |
| 58 | |
| 59 | if ($value !== '') { |
| 60 | try { |
| 61 | $metadataProperty = $metadataProperties[$metadataHeader]; |
| 62 | $this->resourceMetadataRelationValidator->validate($resource, $metadataProperty); |
| 63 | $values[$metadataProperty->getUri()] = $value; |
| 64 | } catch (AbstractValidationException $exception) { |
| 65 | $exceptions[] = $exception->setColumn($metadataHeader); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (!empty($exceptions)) { |
| 71 | throw new AggregatedValidationException($exceptions, []); |
| 72 | } |
| 73 | |
| 74 | return $values; |
| 75 | } |
| 76 | } |