Passed
Push — master ( f0d4c7...67a964 )
by Konrad
04:54
created

Quad::createFromArray()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 15
nop 2
dl 0
loc 32
rs 8.8333
ccs 0
cts 16
cp 0
crap 56
1
<?php
2
3
namespace sweetrdf\InMemoryStoreSqlite\Rdf;
4
5
/*
6
 * This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under
7
 * the terms of the GPL-3 license.
8
 *
9
 * (c) Konrad Abicht <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
use BadMethodCallException;
16
use Exception;
17
use rdfInterface\BlankNode as iBlankNode;
18
use rdfInterface\DefaultGraph as iDefaultGraph;
19
use rdfInterface\Literal as iLiteral;
20
use rdfInterface\NamedNode as iNamedNode;
21
use rdfInterface\Quad as iQuad;
22
use rdfInterface\Term as iTerm;
23
24
class Quad implements iQuad
25
{
26
    private iTerm $subject;
27
28
    private iNamedNode $predicate;
29
30
    private iTerm $object;
31
32
    private iNamedNode | iBlankNode | iDefaultGraph | null $graphIri;
0 ignored issues
show
Bug introduced by
The type sweetrdf\InMemoryStoreSqlite\Rdf\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
34 2
    public function __construct(
35
        iTerm $subject,
36
        iNamedNode $predicate,
37
        iTerm $object,
38
        iNamedNode | iBlankNode | iDefaultGraph | null $graphIri = null
39
    ) {
40 2
        if ($subject instanceof iLiteral) {
41 1
            throw new BadMethodCallException('Subject must be of type NamedNode or BlankNode');
42
        }
43 2
        $this->subject = $subject;
44 2
        $this->predicate = $predicate;
45 2
        $this->object = $object;
46 2
        $this->graphIri = $graphIri ?? new DefaultGraph();
47 2
    }
48
49
    public function __toString(): string
50
    {
51
        return rtrim("$this->subject $this->predicate $this->object $this->graphIri");
52
    }
53
54 1
    public function getType(): string
55
    {
56 1
        return \rdfInterface\TYPE_QUAD;
57
    }
58
59 1
    public function equals(iTerm $term): bool
60
    {
61 1
        return $this == $term;
62
    }
63
64 1
    public function getValue(): string
65
    {
66 1
        throw new BadMethodCallException();
67
    }
68
69 1
    public function getSubject(): iTerm
70
    {
71 1
        return $this->subject;
72
    }
73
74 1
    public function getPredicate(): iNamedNode
75
    {
76 1
        return $this->predicate;
77
    }
78
79
    public function getObject(): iTerm
80
    {
81
        return $this->object;
82
    }
83
84
    public function getGraphIri(): iNamedNode | iBlankNode
85
    {
86
        return $this->graphIri;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->graphIri could return the type rdfInterface\DefaultGraph which is incompatible with the type-hinted return rdfInterface\BlankNode|rdfInterface\NamedNode. Consider adding an additional type-check to rule them out.
Loading history...
87
    }
88
89
    public static function createFromArray(array $triple, string $graph): iQuad
90
    {
91
        /*
92
         * subject
93
         */
94
        if ('uri' == $triple['s_type']) {
95
            $s = new NamedNode($triple['s']);
96
        } elseif ('bnode' == $triple['s_type']) {
97
            $s = new BlankNode($triple['s']);
98
        } else {
99
            throw new Exception('Invalid subject type given.');
100
        }
101
102
        // predicate
103
        $p = new NamedNode($triple['p']);
104
105
        /*
106
         * object
107
         */
108
        if ('uri' == $triple['o_type']) {
109
            $o = new NamedNode($triple['o']);
110
        } elseif ('bnode' == $triple['o_type']) {
111
            $o = new BlankNode($triple['o']);
112
        } elseif ('literal' == $triple['o_type']) {
113
            $o = new Literal($triple['o'], $triple['o_lang'], $triple['o_datatype']);
114
        } else {
115
            throw new Exception('Invalid object type given.');
116
        }
117
118
        $g = !empty($graph) ? new NamedNode($graph) : new DefaultGraph();
119
120
        return new self($s, $p, $o, $g);
121
    }
122
123
    public function withSubject(iTerm $subject): iQuad
124
    {
125
        throw new Exception('withSubject not implemented yet.');
126
    }
127
128
    public function withPredicate(iNamedNode $predicate): iQuad
129
    {
130
        throw new Exception('withPredicate not implemented yet.');
131
    }
132
133
    public function withObject(iTerm $object): iQuad
134
    {
135
        throw new Exception('withObject not implemented yet.');
136
    }
137
138
    public function withGraphIri(
139
        iNamedNode | iBlankNode | iDefaultGraph | null $graphIri
140
    ): iQuad {
141
        throw new Exception('withGraphIri not implemented yet.');
142
    }
143
}
144