Passed
Push — master ( 4e8b13...a3a48f )
by Konrad
04:16
created

BaseParser   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 22.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 112
ccs 11
cts 49
cp 0.2245
rs 9.76
wmc 33

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getSimpleIndex() 0 3 1
A getQueryInfos() 0 3 1
A getTriples() 0 3 1
F _getSimpleIndex() 0 52 29
1
<?php
2
3
/*
4
 * This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under
5
 * the terms of the GPL-3 license.
6
 *
7
 * (c) Konrad Abicht <[email protected]>
8
 * (c) Benjamin Nowack
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace sweetrdf\InMemoryStoreSqlite\Parser;
15
16
use sweetrdf\InMemoryStoreSqlite\Log\Logger;
17
use sweetrdf\InMemoryStoreSqlite\NamespaceHelper;
18
use sweetrdf\InMemoryStoreSqlite\StringReader;
19
20
abstract class BaseParser
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $added_triples;
26
27
    protected string $base;
28
29
    protected string $bnode_id;
30
31
    protected array $blocks;
32
33
    protected Logger $logger;
34
35
    protected NamespaceHelper $namespaceHelper;
36
37
    /**
38
     * Query infos container.
39
     */
40
    protected array $r = [];
41
42
    protected StringReader $stringReader;
43
44
    protected array $triples = [];
45
46
    protected int $t_count = 0;
47
48 96
    public function __construct(Logger $logger, NamespaceHelper $namespaceHelper, StringReader $stringReader)
49
    {
50 96
        $this->reader = $stringReader;
0 ignored issues
show
Bug Best Practice introduced by
The property reader does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51
52 96
        $this->logger = $logger;
53
54 96
        $this->namespaceHelper = $namespaceHelper;
55
56
        // generates random prefix for blank nodes
57 96
        $this->bnode_prefix = bin2hex(random_bytes(4)).'b';
0 ignored issues
show
Bug Best Practice introduced by
The property bnode_prefix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
59 96
        $this->bnode_id = 0;
60 96
    }
61
62 94
    public function getQueryInfos()
63
    {
64 94
        return $this->r;
65
    }
66
67 2
    public function getTriples()
68
    {
69 2
        return $this->triples;
70
    }
71
72
    public function getSimpleIndex($flatten_objects = 1, $vals = ''): array
73
    {
74
        return $this->_getSimpleIndex($this->getTriples(), $flatten_objects, $vals);
75
    }
76
77
    /**
78
     * @todo port from ARC2::getSimpleIndex; refactor and merge it with $this->getSimpleIndex
79
     */
80
    private function _getSimpleIndex($triples, $flatten_objects = 1, $vals = ''): array
81
    {
82
        $r = [];
83
        foreach ($triples as $t) {
84
            $skip_t = 0;
85
            foreach (['s', 'p', 'o'] as $term) {
86
                $$term = $t[$term];
87
                /* template var */
88
                if (isset($t[$term.'_type']) && ('var' == $t[$term.'_type'])) {
89
                    $val = isset($vals[$$term]) ? $vals[$$term] : '';
90
                    $skip_t = isset($vals[$$term]) ? $skip_t : 1;
91
                    $type = '';
92
                    $type = !$type && isset($vals[$$term.' type']) ? $vals[$$term.' type'] : $type;
93
                    $type = !$type && preg_match('/^\_\:/', $val) ? 'bnode' : $type;
94
                    if ('o' == $term) {
95
                        $type = !$type && (preg_match('/\s/s', $val) || !preg_match('/\:/', $val)) ? 'literal' : $type;
96
                        $type = !$type && !preg_match('/[\/]/', $val) ? 'literal' : $type;
97
                    }
98
                    $type = !$type ? 'uri' : $type;
99
                    $t[$term.'_type'] = $type;
100
                    $$term = $val;
101
                }
102
            }
103
            if ($skip_t) {
104
                continue;
105
            }
106
            if (!isset($r[$s])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $s seems to be never defined.
Loading history...
107
                $r[$s] = [];
108
            }
109
            if (!isset($r[$s][$p])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $p seems to be never defined.
Loading history...
110
                $r[$s][$p] = [];
111
            }
112
            if ($flatten_objects) {
113
                if (!\in_array($o, $r[$s][$p])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $o does not seem to be defined for all execution paths leading up to this point.
Loading history...
114
                    $r[$s][$p][] = $o;
115
                }
116
            } else {
117
                $o = ['value' => $o];
118
                foreach (['lang', 'type', 'datatype'] as $suffix) {
119
                    if (isset($t['o_'.$suffix]) && $t['o_'.$suffix]) {
120
                        $o[$suffix] = $t['o_'.$suffix];
121
                    } elseif (isset($t['o '.$suffix]) && $t['o '.$suffix]) {
122
                        $o[$suffix] = $t['o '.$suffix];
123
                    }
124
                }
125
                if (!\in_array($o, $r[$s][$p])) {
126
                    $r[$s][$p][] = $o;
127
                }
128
            }
129
        }
130
131
        return $r;
132
    }
133
}
134