Skip to content

Commit

Permalink
allow usage of custom property labels in Graph::label() and Resource:…
Browse files Browse the repository at this point in the history
…:label()
  • Loading branch information
k00ni committed Apr 9, 2024
1 parent ac378cc commit e631e9c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
17 changes: 10 additions & 7 deletions lib/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -1606,20 +1606,23 @@ public function setType($resource, $type)
*
* @param string|null $resource
* @param string|null $lang
* @param array<non-empty-string> $labelProperties List of shortened label properties (e.g. rdfs:label)
*
* @return Literal|null an instance of Literal which contains the label or null
*/
public function label($resource = null, $lang = null)
public function label($resource = null, $lang = null, array $labelProperties = [])
{
$this->checkResourceParam($resource, true);

if ($resource) {
return $this->get(
$resource,
'skos:prefLabel|rdfs:label|foaf:name|rss:title|dc:title|dc11:title',
'literal',
$lang
);
// use custom label properties if given
if (0 < count($labelProperties)) {
$props = implode('|', $labelProperties);
} else {
$props = 'skos:prefLabel|rdfs:label|foaf:name|rss:title|dc:title|dc11:title';
}

return $this->get($resource, $props, 'literal', $lang);
} else {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,15 @@ public function primaryTopic()
* is available then it will return null.
*
* @param string|null $lang
* @param array<non-empty-string> $labelProperties List of shortened label properties (e.g. rdfs:label
*
* @return Literal|null an instance of Literal which contains the label or null
*/
public function label($lang = null)
public function label($lang = null, array $labelProperties = [])
{
$this->checkHasGraph();

return $this->graph->label($this->uri, $lang);
return $this->graph->label($this->uri, $lang, $labelProperties);
}

/** Return a human readable view of the resource and its properties
Expand Down
29 changes: 29 additions & 0 deletions tests/EasyRdf/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2076,6 +2076,35 @@ public function testLabelNoRdfsLabel()
$this->assertStringEquals('', $this->graph->label($this->uri));
}

/**
* Tests behavior of label() when a custom label property list is provided.
*/
public function testLabelWithCustomPropertyLabels()
{
// add test data to graph
$res1 = new Resource('http://foo', $this->graph);
$res1->addLiteral('rdfs:label', [
new Literal('rdfs label de', 'de'),
new Literal('rdfs label en', 'en'),
]);
$res1->addLiteral('skos:prefLabel', 'skos prefLabel');
$res1->addLiteral('http://xmlns.com/foaf/0.1/name', 'foaf name');

// checks
$this->assertStringEquals('skos prefLabel', $this->graph->label($res1->getUri()));
$this->assertStringEquals('rdfs label de', $this->graph->label($res1->getUri(), 'de'));
$this->assertStringEquals('foaf name', $this->graph->label($res1->getUri(), null, ['foaf:name']));

// include weird behavior of current EasyRdf: literal was added with full URI as property (http://xmlns.com/foaf/0.1/name)
// but you have to use shortened property URI to get label
$this->assertStringEquals('', $this->graph->label($res1->getUri(), null, ['http://xmlns.com/foaf/0.1/name']));

// include behavior check for newly introduced label properties
$res1->addLiteral('http://dummy/label', 'dummy label');
RdfNamespace::set('dummy', 'http://dummy/');
$this->assertStringEquals('dummy label', $this->graph->label($res1->getUri(), null, ['dummy:label']));
}

public function testCountTriples()
{
$this->assertSame(3, $this->graph->countTriples());
Expand Down
31 changes: 31 additions & 0 deletions tests/EasyRdf/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,37 @@ public function testLabelWithLang()
$this->assertStringEquals('Dc Title', $this->resource->label('en'));
}

/**
* Tests behavior of label() when a custom label property list is provided.
*/
public function testLabelWithCustomPropertyLabels()
{
$this->setupTestGraph();

// add test data to graph
$res1 = new Resource('http://foo', $this->graph);
$res1->addLiteral('rdfs:label', [
new Literal('rdfs label de', 'de'),
new Literal('rdfs label en', 'en'),
]);
$res1->addLiteral('skos:prefLabel', 'skos prefLabel');
$res1->addLiteral('http://xmlns.com/foaf/0.1/name', 'foaf name');

// checks
$this->assertStringEquals('skos prefLabel', $res1->label());
$this->assertStringEquals('rdfs label de', $res1->label('de'));
$this->assertStringEquals('foaf name', $res1->label(null, ['foaf:name']));

// include weird behavior of current EasyRdf: literal was added with full URI as property (http://xmlns.com/foaf/0.1/name)
// but you have to use shortened property URI to get label
$this->assertStringEquals('', $res1->label(null, ['http://xmlns.com/foaf/0.1/name']));

// include behavior check for newly introduced label properties
$res1->addLiteral('http://dummy/label', 'dummy label');
RdfNamespace::set('dummy', 'http://dummy/');
$this->assertStringEquals('dummy label', $res1->label(null, ['dummy:label']));
}

public function testDump()
{
$this->setupTestGraph();
Expand Down

0 comments on commit e631e9c

Please sign in to comment.