Skip to content

Commit

Permalink
Add additional fields to JSON-LD context for search endpoint
Browse files Browse the repository at this point in the history
If additional fields are requested using the 'fields' parameter,
add them to the JSON-LD context.

Note: This will break any implementation that relies on the 'skos:'
prefix being part of the keys.
  • Loading branch information
danmichaelo committed Apr 3, 2018
1 parent 7b3a02c commit 2c245b0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
48 changes: 32 additions & 16 deletions controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,43 @@ private function constructSearchParameters($request)
return $parameters;
}

private function transformSearchResults($request, $results)
private function transformSearchResults($request, $results, $parameters)
{
// before serializing to JSON, get rid of the Vocabulary object that came with each resource
foreach ($results as &$res) {
unset($res['voc']);
}
$ret = array(
'@context' => array(
'skos' => 'http://www.w3.org/2004/02/skos/core#',
'isothes' => 'http://purl.org/iso25964/skos-thes#',
'onki' => 'http://schema.onki.fi/onki#',
'uri' => '@id',
'type' => '@type',
'results' => array(
'@id' => 'onki:results',
'@container' => '@list',
),
'prefLabel' => 'skos:prefLabel',
'altLabel' => 'skos:altLabel',
'hiddenLabel' => 'skos:hiddenLabel',

$context = array(
'skos' => 'http://www.w3.org/2004/02/skos/core#',
'isothes' => 'http://purl.org/iso25964/skos-thes#',
'onki' => 'http://schema.onki.fi/onki#',
'uri' => '@id',
'type' => '@type',
'results' => array(
'@id' => 'onki:results',
'@container' => '@list',
),
'prefLabel' => 'skos:prefLabel',
'altLabel' => 'skos:altLabel',
'hiddenLabel' => 'skos:hiddenLabel',
);
foreach ($parameters->getAdditionalFields() as $field) {

// Quick-and-dirty compactification
$context[$field] = 'skos:' . $field;
foreach ($results as &$result) {
foreach ($result as $k => $v) {
if ($k == 'skos:' . $field) {
$result[$field] = $v;
unset($result['skos:' . $field]);
}
}
}
}

$ret = array(
'@context' => $context,
'uri' => '',
'results' => $results,
);
Expand Down Expand Up @@ -171,7 +187,7 @@ public function search($request)

$parameters = $this->constructSearchParameters($request);
$results = $this->model->searchConcepts($parameters);
$ret = $this->transformSearchResults($request, $results);
$ret = $this->transformSearchResults($request, $results, $parameters);

return $this->returnJson($ret);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/RestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,33 @@ public function testDataAsJson() {

$this->assertJsonStringEqualsJsonString('{"@context":{"skos":"http://www.w3.org/2004/02/skos/core#","isothes":"http://purl.org/iso25964/skos-thes#","rdfs":"http://www.w3.org/2000/01/rdf-schema#","owl":"http://www.w3.org/2002/07/owl#","dct":"http://purl.org/dc/terms/","dc11":"http://purl.org/dc/elements/1.1/","uri":"@id","type":"@type","lang":"@language","value":"@value","graph":"@graph","label":"rdfs:label","prefLabel":"skos:prefLabel","altLabel":"skos:altLabel","hiddenLabel":"skos:hiddenLabel","broader":"skos:broader","narrower":"skos:narrower","related":"skos:related","inScheme":"skos:inScheme","exactMatch":"skos:exactMatch","closeMatch":"skos:closeMatch","broadMatch":"skos:broadMatch","narrowMatch":"skos:narrowMatch","relatedMatch":"skos:relatedMatch"},"graph":[{"uri":"http://www.skosmos.skos/test-meta/TestClass","type":"owl:Class","label":{"lang":"en","value":"Test class"}},{"uri":"http://www.skosmos.skos/test/conceptscheme","type":"skos:ConceptScheme","label":{"lang":"en","value":"Test conceptscheme"}},{"uri":"http://www.skosmos.skos/test/ta1","type":["http://www.skosmos.skos/test-meta/TestClass","skos:Concept"],"narrower":{"uri":"http://www.skosmos.skos/test/ta117"},"prefLabel":{"lang":"en","value":"Fish"}},{"uri":"http://www.skosmos.skos/test/ta115","type":["http://www.skosmos.skos/test-meta/TestClass","skos:Concept"],"prefLabel":{"lang":"en","value":"Eel"}},{"uri":"http://www.skosmos.skos/test/ta117","type":["http://www.skosmos.skos/test-meta/TestClass","skos:Concept"],"broader":{"uri":"http://www.skosmos.skos/test/ta1"},"inScheme":{"uri":"http://www.skosmos.skos/test/conceptscheme"},"prefLabel":{"lang":"en","value":"3D Bass"},"relatedMatch":{"uri":"http://www.skosmos.skos/test/ta115"}},{"uri":"skos:broader","label":{"lang":"en","value":"has broader"}},{"uri":"skos:prefLabel","label":{"lang":"en","value":"preferred label"}}]}', $out);
}

/**
* @covers RestController::search
*/
public function testSearchJsonLd() {
$request = new Request($this->model);
$request->setQueryParam('format', 'application/json');
$request->setQueryParam('query', '*bass');
$this->controller->search($request);

$out = $this->getActualOutput();

$this->assertJsonStringEqualsJsonString('{"@context":{"skos":"http:\/\/www.w3.org\/2004\/02\/skos\/core#","isothes":"http:\/\/purl.org\/iso25964\/skos-thes#","onki":"http:\/\/schema.onki.fi\/onki#","uri":"@id","type":"@type","results":{"@id":"onki:results","@container":"@list"},"prefLabel":"skos:prefLabel","altLabel":"skos:altLabel","hiddenLabel":"skos:hiddenLabel"},"uri":"","results":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta117","type":["skos:Concept","meta:TestClass"],"prefLabel":"3D Bass","lang":"en","vocab":"test"},{"uri":"http:\/\/www.skosmos.skos\/test\/ta116","type":["skos:Concept","meta:TestClass"],"prefLabel":"Bass","lang":"en","vocab":"test"},{"uri":"http:\/\/www.skosmos.skos\/test\/ta122","type":["skos:Concept","meta:TestClass"],"prefLabel":"Black sea bass","lang":"en","vocab":"test"}]}', $out);
}

/**
* @covers RestController::search
*/
public function testSearchJsonLdWithAdditionalFields() {
$request = new Request($this->model);
$request->setQueryParam('format', 'application/json');
$request->setQueryParam('query', '*bass');
$request->setQueryParam('fields', 'broader relatedMatch');
$this->controller->search($request);

$out = $this->getActualOutput();

$this->assertJsonStringEqualsJsonString('{"@context":{"skos":"http:\/\/www.w3.org\/2004\/02\/skos\/core#","isothes":"http:\/\/purl.org\/iso25964\/skos-thes#","onki":"http:\/\/schema.onki.fi\/onki#","uri":"@id","type":"@type","results":{"@id":"onki:results","@container":"@list"},"prefLabel":"skos:prefLabel","altLabel":"skos:altLabel","hiddenLabel":"skos:hiddenLabel","broader":"skos:broader","relatedMatch":"skos:relatedMatch"},"uri":"","results":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta117","type":["skos:Concept","meta:TestClass"],"broader":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta1"}],"relatedMatch":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta115"}],"prefLabel":"3D Bass","lang":"en","vocab":"test"},{"uri":"http:\/\/www.skosmos.skos\/test\/ta116","type":["skos:Concept","meta:TestClass"],"broader":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta1"}],"prefLabel":"Bass","lang":"en","vocab":"test"},{"uri":"http:\/\/www.skosmos.skos\/test\/ta122","type":["skos:Concept","meta:TestClass"],"broader":[{"uri":"http:\/\/www.skosmos.skos\/test\/ta116"}],"prefLabel":"Black sea bass","lang":"en","vocab":"test"}]}', $out);
}
}

0 comments on commit 2c245b0

Please sign in to comment.