function testSerialiseDatatype() { $joe = $this->_graph->resource('http://example.com/joe#me'); $joe->set('foaf:foo', EasyRdf_Literal::create(1, null, 'xsd:integer')); $ntriples = $this->_serialiser->serialise($this->_graph, 'ntriples'); $this->assertStringEquals("<http://example.com/joe#me> " . "<http://xmlns.com/foaf/0.1/foo> " . "\"1\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n", $ntriples); }
/** Create a new EasyRdf_Resource or EasyRdf_Literal depending * on the type of data passed in. * * @ignore */ protected function _newTerm($data) { switch ($data['type']) { case 'bnode': return new EasyRdf_Resource('_:' . $data['value']); case 'uri': return new EasyRdf_Resource($data['value']); case 'literal': case 'typed-literal': return EasyRdf_Literal::create($data); default: throw new EasyRdf_Exception("Failed to parse SPARQL Query Results format, unknown term type: " . $data['type']); } }
/** Get an EasyRdf_Resource or EasyRdf_Literal object from an associative array. * @ignore */ protected function arrayToObject($data) { if ($data) { if ($data['type'] == 'uri' or $data['type'] == 'bnode') { return $this->resource($data['value']); } else { return EasyRdf_Literal::create($data); } } else { return null; } }
function testSerialiseUnshortenableDatatype() { $joe = $this->_graph->resource('http://example.com/joe#me'); $joe->set('foaf:foo', EasyRdf_Literal::create('foobar', null, 'http://example.com/datatype/')); $turtle = $this->_serialiser->serialise($this->_graph, 'turtle'); $this->assertContains("<http://example.com/joe#me> " . "foaf:foo " . "\"foobar\"^^<http://example.com/datatype/> .", $turtle); }
/** * Add void and hydra meta-data to an existing graph * * @param EasyRdf_Graph $graph The graph to which meta data has to be added * @param integer $count The total amount of triples that match the URI * * @return EasyRdf_Graph $graph */ public function addMetaTriples($graph, $limit, $offset, $count) { // Add the void and hydra namespace to the EasyRdf framework \EasyRdf_Namespace::set('hydra', 'http://www.w3.org/ns/hydra/core#'); \EasyRdf_Namespace::set('void', 'http://rdfs.org/ns/void#'); \EasyRdf_Namespace::set('dcterms', 'http://purl.org/dc/terms/'); // Add the meta data semantics to the graph $root = \Request::root(); $root .= '/'; $base_uri = $root . 'all'; $identifier = str_replace($root, '', $base_uri); $graph->addResource($base_uri . '#dataset', 'a', 'void:Dataset'); $graph->addResource($base_uri . '#dataset', 'a', 'hydra:Collection'); $resource = $graph->resource($base_uri); $subject_temp_mapping = $graph->newBNode(); $subject_temp_mapping->addResource('a', 'hydra:IriTemplateMapping'); $subject_temp_mapping->addLiteral('hydra:variable', 'subject'); $subject_temp_mapping->addResource('hydra:property', 'rdf:subject'); $predicate_temp_mapping = $graph->newBNode(); $predicate_temp_mapping->addResource('a', 'hydra:IriTemplateMapping'); $predicate_temp_mapping->addLiteral('hydra:variable', 'predicate'); $predicate_temp_mapping->addResource('hydra:property', 'rdf:predicate'); $object_temp_mapping = $graph->newBNode(); $object_temp_mapping->addResource('a', 'hydra:IriTemplateMapping'); $object_temp_mapping->addLiteral('hydra:variable', 'object'); $object_temp_mapping->addResource('hydra:property', 'rdf:object'); $iri_template = $graph->newBNode(); $iri_template->addResource('a', 'hydra:IriTemplate'); $iri_template->addLiteral('hydra:template', $root . 'all' . '{?subject,predicate,object}'); $iri_template->addResource('hydra:mapping', $subject_temp_mapping); $iri_template->addResource('hydra:mapping', $predicate_temp_mapping); $iri_template->addResource('hydra:mapping', $object_temp_mapping); // Add the template to the requested URI resource in the graph $graph->addResource($base_uri . '#dataset', 'hydra:search', $iri_template); $is_deferenced = false; if (strtolower(\Request::segment(1)) != 'all') { $full_url = \Request::root() . '/' . \Request::path(); $is_deferenced = true; } else { $full_url = $base_uri . '?'; } $template_url = $full_url; $templates = array('subject', 'predicate', 'object'); $has_param = false; $query_string = $_SERVER['QUERY_STRING']; $query_parts = explode('&', $query_string); foreach ($query_parts as $part) { if (!empty($part)) { $couple = explode('=', $part); if (strtolower($couple[0]) == 'subject') { $template_url .= $couple[0] . '=' . $couple[1] . '&'; $has_param = true; } if (strtolower($couple[0]) == 'predicate') { $template_url .= $couple[0] . '=' . $couple[1] . '&'; $has_param = true; } if (strtolower($couple[0]) == 'object') { $template_url .= $couple[0] . '=' . $couple[1] . '&'; $has_param = true; } $full_url .= $couple[0] . '=' . $couple[1] . '&'; } } $full_url = rtrim($full_url, '?'); $full_url = rtrim($full_url, '&'); $template_url = rtrim($template_url, '?'); $template_url = rtrim($template_url, '&'); $full_url = str_replace('#', '%23', $full_url); $template_url = str_replace('#', '%23', $template_url); if ($is_deferenced) { $full_url .= '#dataset'; } // Add paging information $graph->addLiteral($full_url, 'hydra:totalItems', \EasyRdf_Literal::create($count, null, 'xsd:integer')); $graph->addLiteral($full_url, 'void:triples', \EasyRdf_Literal::create($count, null, 'xsd:integer')); $graph->addLiteral($full_url, 'hydra:itemsPerPage', \EasyRdf_Literal::create($limit, null, 'xsd:integer')); $graph->addResource($full_url, 'void:subset', \Request::root() . '/all#dataset'); $paging_info = $this->getPagingInfo($limit, $offset, $count); foreach ($paging_info as $key => $info) { switch ($key) { case 'next': if ($has_param) { $glue = '&'; } else { $glue = '?'; } $graph->addResource($full_url, 'hydra:nextPage', $template_url . $glue . 'limit=' . $info['limit'] . '&offset=' . $info['offset']); break; case 'previous': if ($has_param) { $glue = '&'; } else { $glue = '?'; } $graph->addResource($full_url, 'hydra:previousPage', $template_url . $glue . 'limit=' . $info['limit'] . '&offset=' . $info['offset']); break; case 'last': if ($has_param) { $glue = '&'; } else { $glue = '?'; } $graph->addResource($full_url, 'hydra:lastPage', $template_url . $glue . 'limit=' . $info['limit'] . '&offset=' . $info['offset']); break; case 'first': if ($has_param) { $glue = '&'; } else { $glue = '?'; } $graph->addResource($full_url, 'hydra:firstPage', $template_url . $glue . 'limit=' . $info['limit'] . '&offset=' . $info['offset']); break; } } // Tell the agent that it's a subset $graph->addResource($root . 'all#dataset', 'void:subset', $full_url); return $graph; }
public function testSerialiseUnshortenableDatatype() { $joe = $this->graph->resource('http://example.com/joe#me'); $joe->set('foaf:foo', EasyRdf_Literal::create('foobar', null, 'http://example.com/datatype/')); $turtle = $this->serialiser->serialise($this->graph, 'turtle'); $this->assertSame("@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n\n" . "<http://example.com/joe#me> foaf:foo \"foobar\"^^<http://example.com/datatype/> .\n", $turtle); }
public function testDumpLiterals() { $graph = new EasyRdf_Graph(); $graph->add('http://example.com/joe#me', 'foaf:name', 'Joe'); $graph->add('http://example.com/joe#me', 'foaf:age', EasyRdf_Literal::create(52)); $deutschland = new EasyRdf_Literal('Deutschland', 'de'); $graph->add('http://example.com/joe#me', 'foaf:birthPlace', $deutschland); $text = $graph->dump(false); $this->assertContains('http://example.com/joe#me', $text); $this->assertContains('-> foaf:name -> "Joe"', $text); $this->assertContains('-> foaf:age -> "52"^^xsd:integer', $text); $this->assertContains('-> foaf:birthPlace -> "Deutschland"@de', $text); $html = $graph->dump(true); $this->assertContains('http://example.com/joe#me', $html); $this->assertContains('>foaf:name</span>', $html); $this->assertContains('>"Joe"</span>', $html); $this->assertContains('>foaf:age</span>', $html); $this->assertContains('>"52"^^xsd:integer</span>', $html); $this->assertContains('>foaf:birthPlace</span>', $html); $this->assertContains('>"Deutschland"@de</span>', $html); }
/** Add a literal value as a property of a resource * * The resource can either be a resource or the URI of a resource. * The value can either be a single value or an array of values. * * Example: * $graph->add("http://www.example.com", 'dc:title', 'Title of Page'); * * @param mixed $resource The resource to add data to * @param mixed $property The property name * @param mixed $value The value or values for the property * @param string $lang The language of the literal * @return integer The number of values added */ public function addLiteral($resource, $property, $value, $lang = null) { $this->checkResourceParam($resource); $this->checkSinglePropertyParam($property, $inverse); if (is_array($value)) { $added = 0; foreach ($value as $v) { $added += $this->addLiteral($resource, $property, $v, $lang); } return $added; } elseif (!is_object($value) or !$value instanceof EasyRdf_Literal) { $value = EasyRdf_Literal::create($value, $lang); } return $this->add($resource, $property, $value); }
public function testCreateCustomClass() { EasyRdf_Literal::setDatatypeMapping('ex:mytype', 'MyDatatype_Class'); $literal = EasyRdf_Literal::create('foobar', null, 'ex:mytype'); $this->assertClass('MyDatatype_Class', $literal); $this->assertStringEquals('!foobar!', $literal); $this->assertSame('foobar', $literal->getValue()); $this->assertSame('ex:mytype', $literal->getDatatype()); $this->assertSame(null, $literal->getLang()); }
public function testSerialiseRdfXmlWithXMLLiteral() { $this->graph->add('http://www.example.com/joe#me', 'foaf:bio', EasyRdf_Literal::create("<b>html</b>", null, 'rdf:XMLLiteral')); $xml = $this->serialiser->serialise($this->graph, 'rdfxml'); $this->assertContains("<foaf:bio rdf:parseType=\"Literal\"><b>html</b></foaf:bio>", $xml); }