public function testSerialiseJsonLdOptions() { // Expanded form $string = $this->serialiser->serialise($this->graph, 'jsonld'); $decoded = json_decode($string, true); $this->assertArrayNotHasKey('@graph', $decoded); $this->assertInternalType('array', $decoded[1]["http://xmlns.com/foaf/0.1/age"][0]); $this->assertSame(59, $decoded[1]["http://xmlns.com/foaf/0.1/age"][0]['@value']); $this->assertArrayNotHasKey('@type', $decoded[1]["http://xmlns.com/foaf/0.1/age"][0]); // Expanded form + explicit types $string = $this->serialiser->serialise($this->graph, 'jsonld', array('expand_native_types' => true)); $decoded = json_decode($string, true); $this->assertArrayNotHasKey('@graph', $decoded); $this->assertInternalType('array', $decoded[1]["http://xmlns.com/foaf/0.1/age"][0]); $this->assertSame('59', $decoded[1]["http://xmlns.com/foaf/0.1/age"][0]['@value']); $this->assertSame('http://www.w3.org/2001/XMLSchema#integer', $decoded[1]["http://xmlns.com/foaf/0.1/age"][0]['@type']); // Compact form $string = $this->serialiser->serialise($this->graph, 'jsonld', array('compact' => true)); $decoded = json_decode($string, true); $this->assertArrayHasKey('@graph', $decoded); $this->assertSame(59, $decoded['@graph'][1]["http://xmlns.com/foaf/0.1/age"]); // Compact form + explicit types $string = $this->serialiser->serialise($this->graph, 'jsonld', array('compact' => true, 'expand_native_types' => true)); $decoded = json_decode($string, true); $this->assertArrayHasKey('@graph', $decoded); $this->assertSame('59', $decoded['@graph'][1]["http://xmlns.com/foaf/0.1/age"]['@value']); $this->assertSame('http://www.w3.org/2001/XMLSchema#integer', $decoded['@graph'][1]["http://xmlns.com/foaf/0.1/age"]['@type']); // Compact form + explicit types + context $ctx = new stdClass(); $ctx->foaf = 'http://xmlns.com/foaf/0.1/'; $ctx->xmls = 'http://www.w3.org/2001/XMLSchema#'; $string = $this->serialiser->serialise($this->graph, 'jsonld', array('compact' => true, 'expand_native_types' => true, 'context' => $ctx)); $decoded = json_decode($string, true); $this->assertArrayHasKey('@graph', $decoded); $this->assertSame('59', $decoded['@graph'][1]["foaf:age"]['@value']); $this->assertSame('xmls:integer', $decoded['@graph'][1]["foaf:age"]['@type']); }
public function testSerialiseJsonLdOptions() { // Expanded form $string = $this->serialiser->serialise($this->graph, 'jsonld'); $decoded = json_decode($string, true); $this->assertArrayNotHasKey('@graph', $decoded); $this->assertInternalType('array', $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]); $this->assertSame(59, $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]['@value']); $this->assertArrayNotHasKey('@type', $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]); // Expanded form + explicit types $string = $this->serialiser->serialise($this->graph, 'jsonld', array('expand_native_types' => true)); $decoded = json_decode($string, true); $this->assertArrayNotHasKey('@graph', $decoded); $this->assertInternalType('array', $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]); $this->assertSame('59', $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]['@value']); $this->assertSame('http://www.w3.org/2001/XMLSchema#integer', $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]['@type']); // Compact form $string = $this->serialiser->serialise($this->graph, 'jsonld', array('compact' => true)); $decoded = json_decode($string, true); $this->assertArrayHasKey('@graph', $decoded); $this->assertSame(59, $decoded['@graph'][4]["http://xmlns.com/foaf/0.1/age"]); // Compact form + explicit types $string = $this->serialiser->serialise($this->graph, 'jsonld', array('compact' => true, 'expand_native_types' => true)); $decoded = json_decode($string, true); $this->assertArrayHasKey('@graph', $decoded); $this->assertSame('59', $decoded['@graph'][4]["http://xmlns.com/foaf/0.1/age"]['@value']); $this->assertSame('http://www.w3.org/2001/XMLSchema#integer', $decoded['@graph'][4]["http://xmlns.com/foaf/0.1/age"]['@type']); // Compact form + explicit types + context $ctx = new stdClass(); $ctx->{'@context'} = new stdClass(); $ctx->{'@context'}->foaf = 'http://xmlns.com/foaf/0.1/'; $ctx->{'@context'}->xmls = 'http://www.w3.org/2001/XMLSchema#'; $string = $this->serialiser->serialise($this->graph, 'jsonld', array('compact' => true, 'expand_native_types' => true, 'context' => $ctx)); $decoded = json_decode($string, true); $this->assertArrayHasKey('@graph', $decoded); $this->assertSame('59', $decoded['@graph'][4]["foaf:age"]['@value']); $this->assertSame('xmls:integer', $decoded['@graph'][4]["foaf:age"]['@type']); // Framing // Maybe the context and original data could be packed in a fixture $frame = (object) array('@context' => (object) array('dc' => 'http://purl.org/dc/elements/1.1/', 'ex' => 'http://example.org/vocab#'), '@type' => 'ex:Library', 'ex:contains' => (object) array('@type' => 'ex:Book', 'ex:contains' => (object) array('@type' => 'ex:Chapter'))); $string = $this->serialiser->serialise($this->graph, 'jsonld', array('compact' => false, 'frame' => $frame)); $decoded = json_decode($string, true); $this->assertArrayHasKey('@graph', $decoded); $this->assertSame('http://example.org/library', $decoded['@graph'][0]['@id']); $this->assertSame('http://example.org/library/the-republic', $decoded['@graph'][0]['ex:contains']['@id']); $this->assertSame('http://example.org/library/the-republic#introduction', $decoded['@graph'][0]['ex:contains']['ex:contains']['@id']); }
/** * Update a dataset * * @param $id integer The id of the dataset * @param $config array The configuration that makes up the dataset * * @return void */ public function update($id, $config) { $uri = \URL::to('/users/' . $id); // Find the graph in the collection $graph = $this->get($id . '#agent'); $context = $this->getContext(); if (empty($graph)) { return null; } foreach ($this->getFields() as $field) { $domain = $field['domain']; if ($domain == 'foaf:Agent') { $resource = $graph->resource($uri . "#agent"); $graph->delete($resource, $field['short_sem_term']); if ($field['single_value'] && in_array($field['type'], ['string', 'text', 'list'])) { if (filter_var(trim($config[$field['var_name']]), FILTER_VALIDATE_URL)) { $graph->addResource($resource, $field['sem_term'], trim($config[$field['var_name']])); } else { $graph->add($resource, $field['sem_term'], trim($config[$field['var_name']])); } } else { if (!$field['single_value'] && in_array($field['type'], ['string', 'list'])) { if (!empty($config[$field['var_name']])) { foreach ($config[$field['var_name']] as $val) { if (filter_var($val, FILTER_VALIDATE_URL)) { $graph->addResource($resource, $field['sem_term'], $val); } else { $graph->add($resource, $field['sem_term'], $val); } } } } } } } // Delete the json entry and replace it with the updated one $collection = $this->getMongoCollection(); $serializer = new \EasyRdf_Serialiser_JsonLd(); $jsonld = $serializer->serialise($graph, 'jsonld'); $compact_document = (array) JsonLD::compact($jsonld, $context); $collection->remove(['@id' => $uri . '#agent']); $collection->insert($compact_document); }
/** * Update a dataset * * @param $id integer The id of the dataset * @param $config array The configuration that makes up the dataset * * @return void */ public function update($id, $config) { $uri = \URL::to('/datasets/' . $id); // Find the graph in the collection $graph = $this->get($id); $context = $this->getContext(); if (empty($graph)) { return null; } // Add the contributor $graph->addLiteral($uri, 'http://purl.org/dc/terms/contributor', \URL::to('/users/' . strtolower(str_replace(" ", "", $config['user'])))); foreach ($this->getFields() as $field) { $type = $field['domain']; if ($type == 'dcat:Dataset') { $resource = $graph->resource($uri . "#dataset"); } else { if ($type == 'dcat:CatalogRecord') { $resource = $graph->resource($uri); } } $graph->delete($resource, $field['short_sem_term']); if ($field['single_value'] && in_array($field['type'], ['string', 'text', 'list'])) { if (filter_var(trim($config[$field['var_name']]), FILTER_VALIDATE_URL)) { $graph->addResource($resource, $field['sem_term'], trim($config[$field['var_name']])); } else { $graph->add($resource, $field['sem_term'], trim($config[$field['var_name']])); } } else { if (!$field['single_value'] && in_array($field['type'], ['string', 'list'])) { if (!empty($config[$field['var_name']])) { foreach ($config[$field['var_name']] as $val) { if (filter_var($val, FILTER_VALIDATE_URL)) { $graph->addResource($resource, $field['sem_term'], $val); } else { $graph->add($resource, $field['sem_term'], $val); } } } } } } foreach ($graph->allOfType('dcat:Distribution') as $distribution) { $resource = $graph->resource($uri . "#dataset"); $graph->deleteResource($resource, 'dcat:distribution', $distribution->getUri()); } // Add the distribution resource foreach ($config['distributions'] as $distribution) { $id = $this->getIncrementalId(); $distr_uri = $uri . '#distribution' . $id; $distributionResource = $graph->resource($distr_uri); $distributionResource->addType('dcat:Distribution'); if (!empty($distribution['license'])) { $graph->addResource($distributionResource, 'dct:license', $distribution['license']); } if (!empty($distribution['usecases'])) { foreach ($distribution['usecases'] as $usecase) { $graph->addResource($distributionResource, 'linda:useFor', $usecase); } } // Add the distribution to the dataset $graph->addResource($uri . "#dataset", 'dcat:distribution', $distr_uri); } // Delete the json entry and replace it with the updated one $collection = $this->getMongoCollection(); $serializer = new \EasyRdf_Serialiser_JsonLd(); $jsonld = $serializer->serialise($graph, 'jsonld'); $compact_document = (array) JsonLD::compact($jsonld, $context); $collection->remove(['@graph' => ['$elemMatch' => ['@id' => $uri]]]); $collection->insert($compact_document); }