Skip to content

Commit 84409a0

Browse files
committed
rdfInterface\RdfNamespace moved to the rdfHelpers library
1 parent 8e519fa commit 84409a0

File tree

2 files changed

+5
-68
lines changed

2 files changed

+5
-68
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"require": {
1616
"php": ">=8.0",
1717
"zozlak/rdf-constants": "^1.1",
18-
"sweetrdf/rdf-interface": "*"
18+
"sweetrdf/rdf-helpers": ">=0.4.0",
19+
"sweetrdf/rdf-interface": ">=0.4.5"
1920
},
2021
"suggest": {
2122
"sweetrdf/quick-rdf-io": "*"

src/simpleRdf/RdfNamespace.php

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
namespace simpleRdf;
2828

29-
use OutOfBoundsException;
30-
use BadMethodCallException;
3129
use rdfInterface\NamedNode;
3230
use simpleRdf\DataFactory as DF;
3331

@@ -36,71 +34,9 @@
3634
*
3735
* @author zozlak
3836
*/
39-
class RdfNamespace implements \rdfInterface\RdfNamespace {
37+
class RdfNamespace extends \rdfHelpers\RdfNamespace {
4038

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);
10541
}
10642
}

0 commit comments

Comments
 (0)