Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
FileIterator
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getIterator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 load
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
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) 2014 (original work) Open Assessment Technologies SA;
19 *
20 *
21 */
22
23namespace oat\generis\model\kernel\persistence\file;
24
25use EasyRdf\Graph;
26use core_kernel_classes_Triple;
27use IteratorAggregate;
28use ArrayIterator;
29
30class FileIterator implements IteratorAggregate
31{
32    private $triples = [];
33
34    /**
35     * @param string      $file
36     * @param string|null $forceModelId
37     *
38     * @throws \common_exception_Error
39     */
40    public function __construct(string $file, string $forceModelId = null)
41    {
42        $modelId = is_null($forceModelId) ? FileModel::getModelIdFromXml($file) : $forceModelId;
43        $this->load($modelId, $file);
44    }
45
46    /**
47     * @see IteratorAggregate::getIterator()
48     */
49    public function getIterator(): ArrayIterator
50    {
51        return new ArrayIterator($this->triples);
52    }
53
54    /**
55     * load triples from rdf file
56     *
57     * @param string $modelId
58     * @param string $file
59     */
60    protected function load($modelId, $file)
61    {
62        $easyRdf = new Graph();
63        $easyRdf->parseFile($file);
64
65        foreach ($easyRdf->toRdfPhp() as $subject => $propertiesValues) {
66            foreach ($propertiesValues as $predicate => $values) {
67                foreach ($values as $v) {
68                    $triple = new core_kernel_classes_Triple();
69                    $triple->modelid = $modelId;
70                    $triple->subject = $subject;
71                    $triple->predicate = $predicate;
72                    $triple->object = $v['value'];
73                    $triple->lg = $v['lang'] ?? null;
74                    $this->triples[] = $triple;
75                }
76            }
77        }
78    }
79}