Passed
Push — master ( 58ce5b...d80a54 )
by Konrad
04:10
created

NamespaceHelper::setPrefix()   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;
15
16
/**
17
 * This class provides helpers to handle RDF namespace related operations.
18
 */
19
final class NamespaceHelper
20
{
21
    const BASE_NAMESPACE = 'sweetrdf://in-memory-store-sqlite/';
22
23
    const NAMESPACE_RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
24
    const NAMESPACE_XML = 'http://www.w3.org/XML/1998/namespace';
25
    const NAMESPACE_XSD = 'http://www.w3.org/2001/XMLSchema#';
26
27
    /**
28
     * List of known and new namespaces + prefixes.
29
     *
30
     * @var array<string,string>
31
     */
32
    private array $namespaces = [
33
        'owl:' => 'http://www.w3.org/2002/07/owl#',
34
        'rdf:' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
35
        'rdfs:' => 'http://www.w3.org/2000/01/rdf-schema#',
36
        'xsd:' => 'http://www.w3.org/2001/XMLSchema#',
37
    ];
38
39
    /**
40
     * @return array<string,string>
41
     */
42 88
    public function getNamespaces(): array
43
    {
44 88
        return $this->namespaces;
45
    }
46
47
    /**
48
     * Get namespace by prefix.
49
     */
50 19
    public function getNamespace(string $prefix): ?string
51
    {
52 19
        foreach ($this->namespaces as $p => $ns) {
53 19
            if ($prefix == $p) {
54 19
                return $ns;
55
            }
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * Get prefix by namespace.
63
     */
64
    public function getPrefix(string $namespace): ?string
65
    {
66
        foreach ($this->namespaces as $prefix => $ns) {
67
            if ($namespace == $ns) {
68
                return $prefix;
69
            }
70
        }
71
72
        return null;
73
    }
74
75 23
    public function hasPrefix(string $prefix): bool
76
    {
77 23
        return true === isset($this->namespaces[$prefix]);
78
    }
79
80 12
    public function setPrefix(string $prefix, string $namespace): void
81
    {
82 12
        $this->namespaces[$prefix] = $namespace;
83 12
    }
84
}
85