|
26 | 26 |
|
27 | 27 | namespace simpleRdf; |
28 | 28 |
|
29 | | -use OutOfBoundsException; |
30 | | -use BadMethodCallException; |
31 | 29 | use rdfInterface\NamedNode; |
32 | 30 | use simpleRdf\DataFactory as DF; |
33 | 31 |
|
|
36 | 34 | * |
37 | 35 | * @author zozlak |
38 | 36 | */ |
39 | | -class RdfNamespace implements \rdfInterface\RdfNamespace { |
| 37 | +class RdfNamespace extends \rdfHelpers\RdfNamespace { |
40 | 38 |
|
41 | | - private int $n = 0; |
42 | | - |
43 | | - /** |
44 | | - * |
45 | | - * @var array<string, string> |
46 | | - */ |
47 | | - private array $namespaces = []; |
48 | | - |
49 | | - public function add(string $iriPrefix, ?string $shortName = null): string { |
50 | | - if (empty($shortName)) { |
51 | | - $shortName = 'n' . $this->n; |
52 | | - $this->n++; |
53 | | - } |
54 | | - $this->namespaces[$shortName] = $iriPrefix; |
55 | | - return $shortName; |
56 | | - } |
57 | | - |
58 | | - public function remove(string $shortName): void { |
59 | | - unset($this->namespaces[$shortName]); |
60 | | - } |
61 | | - |
62 | | - public function get(string $shortName): string { |
63 | | - if (isset($this->namespaces[$shortName])) { |
64 | | - return $this->namespaces[$shortName]; |
65 | | - } |
66 | | - throw new OutOfBoundsException('Unknown prefix'); |
67 | | - } |
68 | | - |
69 | | - public function getAll(): array { |
70 | | - return $this->namespaces; |
71 | | - } |
72 | | - |
73 | | - public function expand(string $shortIri): NamedNode { |
74 | | - $pos = strpos($shortIri, ':'); |
75 | | - if ($pos === false) { |
76 | | - throw new BadMethodCallException("parameter is not a shortened IRI"); |
77 | | - } |
78 | | - $alias = substr($shortIri, 0, $pos); |
79 | | - if (isset($this->namespaces[$alias])) { |
80 | | - return DF::namedNode($this->namespaces[$alias] . substr($shortIri, $pos + 1)); |
81 | | - } |
82 | | - throw new OutOfBoundsException('Unknown alias'); |
83 | | - } |
84 | | - |
85 | | - public function shorten(NamedNode $iri, bool $create): string { |
86 | | - $iri = (string) $iri->getValue(); |
87 | | - $n = strlen($iri); |
88 | | - $p = max(strrpos($iri, '/'), strrpos($iri, '#')); |
89 | | - if ($p + 1 >= $n) { |
90 | | - $iritmp = substr($iri, 0, $n - 1); |
91 | | - $p = max(strrpos($iritmp, '/'), strrpos($iritmp, '#')); |
92 | | - } |
93 | | - $prefix = substr($iri, 0, $p + 1); |
94 | | - $shortName = array_search($prefix, $this->namespaces); |
95 | | - if ($shortName === false) { |
96 | | - if ($create) { |
97 | | - $shortName = "n" . $this->n; |
98 | | - $this->n++; |
99 | | - $this->namespaces[$shortName] = $prefix; |
100 | | - } else { |
101 | | - throw new OutOfBoundsException("Iri doesn't match any registered prefix"); |
102 | | - } |
103 | | - } |
104 | | - return $shortName . ':' . substr($iri, $p + 1); |
| 39 | + protected function getNamedNode(string $iri): NamedNode { |
| 40 | + return DF::namedNode($iri); |
105 | 41 | } |
106 | 42 | } |
0 commit comments