Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
Rdf/CommonNamespaces: improved test coverage and added optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
k00ni committed Apr 27, 2018
1 parent f63953a commit 2fd6244
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 34 deletions.
115 changes: 81 additions & 34 deletions src/Saft/Rdf/CommonNamespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class CommonNamespaces
{
protected $cache;

protected $namespaces = [
'bibo' => 'http://purl.org/ontology/bibo/',
'cc' => 'http://creativecommons.org/ns#',
Expand Down Expand Up @@ -70,6 +72,8 @@ public function __construct(array $customPrefixes = [])
foreach ($customPrefixes as $prefix => $uri) {
$this->add($prefix, $uri);
}

$this->cache = [];
}

/**
Expand All @@ -79,6 +83,8 @@ public function __construct(array $customPrefixes = [])
public function add($prefix, $uri)
{
$this->namespaces[$prefix] = $uri;

$this->cache = [];
}

/**
Expand All @@ -88,14 +94,29 @@ public function add($prefix, $uri)
*/
public function extendUri($shortenedUri)
{
$parts = explode(':', $shortenedUri);
// exactly 2 parts found. one before and one after the :
// if namespace is known (by prefix $parts[0])
if (2 == count($parts) && isset($this->namespaces[$parts[0]])) {
return str_replace($parts[0].':', $this->namespaces[$parts[0]], $shortenedUri);
$cacheId = 'extendUri_'.$shortenedUri;

if (false == isset($this->cache[$cacheId])) {
$parts = \explode(':', $shortenedUri);

// exactly 2 parts found. one before and one after the :
// if namespace is known (by prefix $parts[0])
if (2 == \count($parts) && isset($this->namespaces[$parts[0]])) {
$this->cache[$cacheId] = \str_replace($parts[0].':', $this->namespaces[$parts[0]], $shortenedUri);
} else {
$this->cache[$cacheId] = $shortenedUri;
}
}

return $shortenedUri;
return $this->cache[$cacheId];
}

/**
* @return cache
*/
public function getCache()
{
return $this->cache;
}

/**
Expand All @@ -106,25 +127,44 @@ public function getNamespaces()
return $this->namespaces;
}

/**
* @param string $uriToMatch
*
* @return null|string
*/
public function getPrefix($uriToMatch)
{
foreach ($this->namespaces as $prefix => $uri) {
if ($uriToMatch == $uri) {
return $prefix;
$cacheId = 'getPrefix_'.$uriToMatch;

if (false == isset($this->cache[$cacheId])) {
foreach ($this->namespaces as $prefix => $uri) {
if ($uriToMatch == $uri) {
$this->cache[$cacheId] = $prefix;
return $prefix;
}
}

$this->cache[$cacheId] = null;
}

return null;
return $this->cache[$cacheId];
}

public function getUri($prefix)
{
return isset($this->namespaces[$prefix]) ? $this->namespaces[$prefix] : null;
}

/**
* Very basic check if a given string is a shortened URI.
*
* @param string $string
*
* @return bool
*/
public function isShortenedUri($string)
{
return false == strpos($string, '://');
return false == \strpos($string, '://') && false !== \strpos($string, ':');
}

/**
Expand All @@ -134,33 +174,40 @@ public function isShortenedUri($string)
*/
public function shortenUri($uri)
{
$longestNamespaceInfo = null;

foreach ($this->namespaces as $ns => $nsUri) {
// prefix found
if (false !== strpos($uri, $nsUri)) {
if (null == $longestNamespaceInfo) {
$longestNamespaceInfo = [
'ns' => $ns,
'nsUri' => $nsUri,
];
continue;
}

if (strlen($nsUri) > strlen($longestNamespaceInfo['nsUri'])) {
$longestNamespaceInfo = [
'ns' => $ns,
'nsUri' => $nsUri,
];
$cacheId = 'getPrefix_'.$uri;

if (false == isset($this->cache[$cacheId])) {
$longestNamespaceInfo = null;

foreach ($this->namespaces as $ns => $nsUri) {
// prefix found
if (false !== \strpos($uri, $nsUri)) {
if (null == $longestNamespaceInfo) {
$longestNamespaceInfo = [
'ns' => $ns,
'nsUri' => $nsUri,
];
continue;
}

// if we found a longer one
if (\strlen($nsUri) > \strlen($longestNamespaceInfo['nsUri'])) {
$longestNamespaceInfo = [
'ns' => $ns,
'nsUri' => $nsUri,
];
}
}
}
}

// prefer the prefix with the longest URI to avoid results like foo:bar/fff
if (null !== $longestNamespaceInfo) {
return str_replace($longestNamespaceInfo['nsUri'], $longestNamespaceInfo['ns'].':', $uri);
// prefer the prefix with the longest URI to avoid results like foo:bar/fff
if (null !== $longestNamespaceInfo) {
$this->cache[$cacheId] = \str_replace($longestNamespaceInfo['nsUri'], $longestNamespaceInfo['ns'].':', $uri);
} else {
$this->cache[$cacheId] = $uri;
}
}

return $uri;
return $this->cache[$cacheId];
}
}
63 changes: 63 additions & 0 deletions src/Saft/Rdf/Test/CommonNamespacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,46 @@ public function testExtendUriOverlappingPrefixes2()
$this->assertEquals('http://schema.org/foo', $this->fixture->extendUri('schema:foo'));
}

/*
* Tests for getNamespaces
*/

public function testGetNamespaces()
{
$this->assertTrue(\is_array($this->fixture->getNamespaces()));
$this->assertEquals(45, \count($this->fixture->getNamespaces()));
}

/*
* Tests for getPrefix
*/

public function testGetPrefix()
{
$this->assertEquals('rdfs', $this->fixture->getPrefix('http://www.w3.org/2000/01/rdf-schema#'));
$this->assertEquals(null, $this->fixture->getPrefix('http://not-there'));
}

/*
* Tests for getUri
*/

public function testGetUri()
{
$this->assertEquals('http://www.w3.org/2000/01/rdf-schema#', $this->fixture->getUri('rdfs'));
}

/*
* Tests for isShortenedUri
*/

public function testIsShortenedUri()
{
$this->assertTrue($this->fixture->isShortenedUri('rdfs:label'));

$this->assertFalse($this->fixture->isShortenedUri('http://label'));
}

/*
* Tests for shortenUri
*/
Expand All @@ -72,4 +112,27 @@ public function testShortenUriOverlappingPrefixes()

$this->assertEquals('foo2:bar', $this->fixture->shortenUri('http://foo/baz/bar'));
}

public function testShortenUriNothingFound()
{
$this->assertEquals('http://foo/bar', $this->fixture->shortenUri('http://foo/bar'));
}

public function testShortenUriTestCache()
{
// fresh
$this->assertEquals([], $this->fixture->getCache());
$this->assertEquals('rdfs:label', $this->fixture->shortenUri('http://www.w3.org/2000/01/rdf-schema#label'));

$this->assertEquals(
[
'getPrefix_http://www.w3.org/2000/01/rdf-schema#label' => 'rdfs:label'
],
$this->fixture->getCache()
);

// cache hits
$this->assertEquals('rdfs:label', $this->fixture->shortenUri('http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertEquals('rdfs:label', $this->fixture->shortenUri('http://www.w3.org/2000/01/rdf-schema#label'));
}
}

0 comments on commit 2fd6244

Please sign in to comment.