Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CustomizedRdfImporter | |
0.00% |
0 / 20 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| importFromFile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| importFileContent | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| 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; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\import; |
| 24 | |
| 25 | use core_kernel_classes_Class as RdfClass; |
| 26 | use EasyRdf\Format; |
| 27 | use EasyRdf\Graph; |
| 28 | use oat\generis\model\OntologyAwareTrait; |
| 29 | use oat\generis\model\OntologyRdf; |
| 30 | use oat\oatbox\reporting\Report; |
| 31 | use tao_models_classes_import_RdfImporter; |
| 32 | use tao_models_classes_Parser; |
| 33 | |
| 34 | /** |
| 35 | * Purpose of this class extension is to avoid creating new identifiers for imported resources as it is done in parent |
| 36 | */ |
| 37 | class CustomizedRdfImporter extends tao_models_classes_import_RdfImporter |
| 38 | { |
| 39 | use OntologyAwareTrait; |
| 40 | |
| 41 | private Graph $easyRdfGraph; |
| 42 | |
| 43 | public function __construct(Graph $easyRdfGraph) |
| 44 | { |
| 45 | $this->easyRdfGraph = $easyRdfGraph; |
| 46 | } |
| 47 | |
| 48 | public function importFromFile(RdfClass $targetClass, string $filePath): Report |
| 49 | { |
| 50 | $parser = new tao_models_classes_Parser($filePath, ['extension' => 'rdf']); |
| 51 | $parser->validate(); |
| 52 | if (!$parser->isValid()) { |
| 53 | return Report::createError('Import failed') |
| 54 | ->add($parser->getReport()); |
| 55 | } |
| 56 | |
| 57 | return $this->importFileContent($parser->getContent(), $targetClass); |
| 58 | } |
| 59 | |
| 60 | private function importFileContent($content, $targetClass): Report |
| 61 | { |
| 62 | $report = Report::createInfo('Imported data:'); |
| 63 | $this->easyRdfGraph->parse($content); |
| 64 | |
| 65 | $map = [ |
| 66 | OntologyRdf::RDF_PROPERTY => OntologyRdf::RDF_PROPERTY |
| 67 | ]; |
| 68 | |
| 69 | foreach ($this->easyRdfGraph->resources() as $resource) { |
| 70 | $map[$resource->getUri()] = $resource->getUri(); |
| 71 | } |
| 72 | |
| 73 | $data = $this->easyRdfGraph->serialise(Format::getFormat('php')); |
| 74 | |
| 75 | foreach ($data as $subjectUri => $propertiesValues) { |
| 76 | $report->add( |
| 77 | $this->importProperties($this->getResource($subjectUri), $propertiesValues, $map, $targetClass) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | return $report; |
| 82 | } |
| 83 | } |