Passed
Push — extract-store ( ec1a3e...70fc7f )
by Konrad
04:10
created

BaseParser   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 59.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 115
ccs 29
cts 49
cp 0.5918
rs 9.76
c 1
b 0
f 0
wmc 33

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSimpleIndex() 0 3 1
A getQueryInfos() 0 3 1
A getTriples() 0 3 1
A __construct() 0 14 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
    /**
36
     * @var array<string, string>
37
     */
38
    protected array $prefixes;
39
40
    /**
41
     * Query infos container.
42
     */
43
    protected array $r = [];
44
45
    protected array $triples = [];
46
47
    protected int $t_count = 0;
48
49 100
    public function __construct(Logger $logger)
50
    {
51
        // TODO pass as constructor param
52 100
        $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...
53
54 100
        $this->logger = $logger;
55
56
        // TODO make it a constructor param
57 100
        $this->prefixes = (new NamespaceHelper())->getNamespaces();
58
59
        // generates random prefix for blank nodes
60 100
        $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...
61
62 100
        $this->bnode_id = 0;
63 100
    }
64
65 100
    public function getQueryInfos()
66
    {
67 100
        return $this->r;
68
    }
69
70 23
    public function getTriples()
71
    {
72 23
        return $this->triples;
73
    }
74
75 23
    public function getSimpleIndex($flatten_objects = 1, $vals = ''): array
76
    {
77 23
        return $this->_getSimpleIndex($this->getTriples(), $flatten_objects, $vals);
78
    }
79
80
    /**
81
     * @todo port from ARC2::getSimpleIndex; refactor and merge it with $this->getSimpleIndex
82
     */
83 23
    private function _getSimpleIndex($triples, $flatten_objects = 1, $vals = ''): array
84
    {
85 23
        $r = [];
86 23
        foreach ($triples as $t) {
87 23
            $skip_t = 0;
88 23
            foreach (['s', 'p', 'o'] as $term) {
89 23
                $$term = $t[$term];
90
                /* template var */
91 23
                if (isset($t[$term.'_type']) && ('var' == $t[$term.'_type'])) {
92
                    $val = isset($vals[$$term]) ? $vals[$$term] : '';
93
                    $skip_t = isset($vals[$$term]) ? $skip_t : 1;
94
                    $type = '';
95
                    $type = !$type && isset($vals[$$term.' type']) ? $vals[$$term.' type'] : $type;
96
                    $type = !$type && preg_match('/^\_\:/', $val) ? 'bnode' : $type;
97
                    if ('o' == $term) {
98
                        $type = !$type && (preg_match('/\s/s', $val) || !preg_match('/\:/', $val)) ? 'literal' : $type;
99
                        $type = !$type && !preg_match('/[\/]/', $val) ? 'literal' : $type;
100
                    }
101
                    $type = !$type ? 'uri' : $type;
102
                    $t[$term.'_type'] = $type;
103
                    $$term = $val;
104
                }
105
            }
106 23
            if ($skip_t) {
107
                continue;
108
            }
109 23
            if (!isset($r[$s])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $s seems to be never defined.
Loading history...
110 23
                $r[$s] = [];
111
            }
112 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...
113 23
                $r[$s][$p] = [];
114
            }
115 23
            if ($flatten_objects) {
116 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...
117 23
                    $r[$s][$p][] = $o;
118
                }
119
            } else {
120
                $o = ['value' => $o];
121
                foreach (['lang', 'type', 'datatype'] as $suffix) {
122
                    if (isset($t['o_'.$suffix]) && $t['o_'.$suffix]) {
123
                        $o[$suffix] = $t['o_'.$suffix];
124
                    } elseif (isset($t['o '.$suffix]) && $t['o '.$suffix]) {
125
                        $o[$suffix] = $t['o '.$suffix];
126
                    }
127
                }
128
                if (!\in_array($o, $r[$s][$p])) {
129
                    $r[$s][$p][] = $o;
130
                }
131
            }
132
        }
133
134 23
        return $r;
135
    }
136
}
137