Passed
Push — extract-store ( f24e42...0c7df1 )
by Konrad
04:35
created

BaseParser::getSimpleIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\NamespaceHelper;
17
use sweetrdf\InMemoryStoreSqlite\StringReader;
18
19
abstract class BaseParser
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $added_triples;
25
26
    protected string $base;
27
28
    protected string $bnode_id;
29
30
    protected array $blocks;
31
32
    private array $errors = [];
33
34
    /**
35
     * @var array<string, string>
36
     */
37
    protected array $prefixes;
38
39
    /**
40
     * Query infos container.
41
     */
42
    protected array $r = [];
43
44
    protected array $triples = [];
45
46
    protected int $t_count = 0;
47
48 106
    public function __construct()
49
    {
50
        // TODO pass logger as parameter
51 106
        $this->reader = new 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...
52
53
        /*
54
         * @todo make it a constructor param
55
         */
56 106
        $this->prefixes = (new NamespaceHelper())->getNamespaces();
57
58
        // generates random prefix for blank nodes
59 106
        $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...
60
61 106
        $this->bnode_id = 0;
62 106
    }
63
64
    /**
65
     * @todo replace by Logger
66
     */
67 19
    protected function addError(string $error): void
68
    {
69 19
        $this->errors[] = $error;
70 19
    }
71
72
    /**
73
     * @todo replace by Logger
74
     */
75 106
    public function getErrors(): array
76
    {
77 106
        return $this->errors;
78
    }
79
80 106
    public function getQueryInfos()
81
    {
82 106
        return $this->r;
83
    }
84
85 23
    public function getTriples()
86
    {
87 23
        return $this->triples;
88
    }
89
90 23
    public function getSimpleIndex($flatten_objects = 1, $vals = ''): array
91
    {
92 23
        return $this->_getSimpleIndex($this->getTriples(), $flatten_objects, $vals);
93
    }
94
95
    /**
96
     * @todo port from ARC2::getSimpleIndex; refactor and merge it with $this->getSimpleIndex
97
     */
98 23
    private function _getSimpleIndex($triples, $flatten_objects = 1, $vals = ''): array
99
    {
100 23
        $r = [];
101 23
        foreach ($triples as $t) {
102 23
            $skip_t = 0;
103 23
            foreach (['s', 'p', 'o'] as $term) {
104 23
                $$term = $t[$term];
105
                /* template var */
106 23
                if (isset($t[$term.'_type']) && ('var' == $t[$term.'_type'])) {
107
                    $val = isset($vals[$$term]) ? $vals[$$term] : '';
108
                    $skip_t = isset($vals[$$term]) ? $skip_t : 1;
109
                    $type = '';
110
                    $type = !$type && isset($vals[$$term.' type']) ? $vals[$$term.' type'] : $type;
111
                    $type = !$type && preg_match('/^\_\:/', $val) ? 'bnode' : $type;
112
                    if ('o' == $term) {
113
                        $type = !$type && (preg_match('/\s/s', $val) || !preg_match('/\:/', $val)) ? 'literal' : $type;
114
                        $type = !$type && !preg_match('/[\/]/', $val) ? 'literal' : $type;
115
                    }
116
                    $type = !$type ? 'uri' : $type;
117
                    $t[$term.'_type'] = $type;
118
                    $$term = $val;
119
                }
120
            }
121 23
            if ($skip_t) {
122
                continue;
123
            }
124 23
            if (!isset($r[$s])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $s seems to be never defined.
Loading history...
125 23
                $r[$s] = [];
126
            }
127 23
            if (!isset($r[$s][$p])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $p seems to be never defined.
Loading history...
128 23
                $r[$s][$p] = [];
129
            }
130 23
            if ($flatten_objects) {
131 23
                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...
132 23
                    $r[$s][$p][] = $o;
133
                }
134
            } else {
135
                $o = ['value' => $o];
136
                foreach (['lang', 'type', 'datatype'] as $suffix) {
137
                    if (isset($t['o_'.$suffix]) && $t['o_'.$suffix]) {
138
                        $o[$suffix] = $t['o_'.$suffix];
139
                    } elseif (isset($t['o '.$suffix]) && $t['o '.$suffix]) {
140
                        $o[$suffix] = $t['o '.$suffix];
141
                    }
142
                }
143
                if (!\in_array($o, $r[$s][$p])) {
144
                    $r[$s][$p][] = $o;
145
                }
146
            }
147
        }
148
149 23
        return $r;
150
    }
151
}
152