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); }
/** Constructor for creating a new boolean literal * * If the value is not a string, then it will be converted to 'true' or 'false'. * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:boolean') * @return object EasyRdf_Literal_Boolean */ public function __construct($value, $lang = null, $datatype = null) { if (!is_string($value)) { $value = $value ? 'true' : 'false'; } parent::__construct($value, null, $datatype); }
/** Constructor for creating a new date literal * * The date is parsed and stored internally using a DateTime object. * @see DateTime * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:date') * @return object EasyRdf_Literal_Date */ public function __construct($value, $lang = null, $datatype = null) { // Convert the value into a DateTime object, if it isn't already if (!$value instanceof DateTime) { $value = new DateTime(strval($value)); } parent::__construct($value, null, $datatype); }
/** Constructor for creating a new date literal * * If the value is a DateTime object, then it will be converted to the xsd:date format. * * @see DateTime * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:date') * @return object EasyRdf_Literal_Date */ public function __construct($value, $lang = null, $datatype = null) { // Convert DateTime object into string if ($value instanceof DateTime) { $value = $value->format('Y-m-d'); } parent::__construct($value, null, $datatype); }
/** Constructor for creating a new date and time literal * * If the value is a DateTime object, then it will be converted to the xsd:dateTime format. * * @see DateTime * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:dateTime') * @return object EasyRdf_Literal_DateTime */ public function __construct($value, $lang = null, $datatype = null) { // Convert DateTime objects into string if ($value instanceof DateTime) { $iso = $value->format(DateTime::ISO8601); $value = preg_replace('/[\\+\\-]00(\\:?)00$/', 'Z', $iso); } EasyRdf_Literal::__construct($value, null, $datatype); }
/** Constructor for creating a new xsd:hexBinary literal * * @param mixed $value The value of the literal (already encoded as hexadecimal) * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:hexBinary') * @return object EasyRdf_Literal_HexBinary */ public function __construct($value, $lang = null, $datatype = null) { // Normalise the canonical representation, as specified here: // http://www.w3.org/TR/xmlschema-2/#hexBinary-canonical-repr $value = strtoupper($value); // Validate the data if (preg_match('/[^A-F0-9]/', $value)) { throw new InvalidArgumentException("Literal of type xsd:hexBinary contains non-hexadecimal characters"); } parent::__construct(strtoupper($value), null, 'xsd:hexBinary'); }
/** Constructor for creating a new date literal * * If the value is a DateTime object, then it will be converted to the xsd:date format. * If no value is given or is is null, then the current date is used. * * @see DateTime * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:date') * @return object EasyRdf_Literal_Date */ public function __construct($value = null, $lang = null, $datatype = null) { // If $value is null, use today's date if (is_null($value)) { $value = new DateTime('today'); } // Convert DateTime object into string if ($value instanceof DateTime) { $value = $value->format('Y-m-d'); } parent::__construct($value, null, $datatype); }
/** Constructor for creating a new date and time literal * * If the value is a DateTime object, then it will be converted to the xsd:dateTime format. * If no value is given or is is null, then the current time is used. * * @see DateTime * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:dateTime') * @return object EasyRdf_Literal_DateTime */ public function __construct($value = null, $lang = null, $datatype = null) { // If $value is null, use 'now' if (is_null($value)) { $value = new DateTime('now'); } // Convert DateTime objects into string if ($value instanceof DateTime) { $atom = $value->format(DateTime::ATOM); $value = preg_replace('/[\\+\\-]00(\\:?)00$/', 'Z', $atom); } EasyRdf_Literal::__construct($value, null, $datatype); }
/** Constructor for creating a new decimal literal * * @param double|int|string $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:decimal') * * @throws UnexpectedValueException * @return EasyRdf_Literal_Decimal */ public function __construct($value, $lang = null, $datatype = null) { if (is_string($value)) { self::validate($value); } elseif (is_double($value) or is_int($value)) { $locale_data = localeconv(); $value = str_replace($locale_data['decimal_point'], '.', strval($value)); } else { throw new UnexpectedValueException('EasyRdf_Literal_Decimal expects int/float/string as value'); } $value = self::canonicalise($value); parent::__construct($value, null, $datatype); }
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); }
{ return (int) $this->_value->format('H'); } /** The minutes pasts the hour as an integer * * @return integer */ public function min() { return (int) $this->_value->format('i'); } /** The seconds pasts the minute as an integer * * @return integer */ public function sec() { return (int) $this->_value->format('s'); } /** Magic method to return the value as an ISO8601 string * * @return string The date time as an ISO8601 string */ public function __toString() { $iso = $this->_value->format(DateTime::ISO8601); return preg_replace('/[\\+\\-]00(\\:?)00$/', 'Z', $iso); } } EasyRdf_Literal::setDatatypeMapping('xsd:dateTime', 'EasyRdf_Literal_DateTime');
/** * 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); }
/** Constructor for creating a new hexadecimal encoded binary blob * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:hexBinary') * @return object EasyRdf_Literal_HexBinary */ public function __construct($value, $lang = null, $datatype = null) { parent::__construct($value, null, 'xsd:hexBinary'); }
/** 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']); } }
public function testDeleteDatatypeMappingNonString() { $this->setExpectedException('InvalidArgumentException', '$datatype should be a string and cannot be null or empty'); EasyRdf_Literal::deleteDatatypeMapping(array()); }
/** Magic method to return the value of a literal as a string * * @return string The value of the literal */ public function __toString() { return isset($this->value) ? $this->value : ''; } /** Return pretty-print view of the literal * * @param string $format Either 'html' or 'text' * @param string $color The colour of the text * @return string */ public function dumpValue($format = 'html', $color = 'black') { return EasyRdf_Utils::dumpLiteralValue($this, $format, $color); } } /* Register default set of datatype classes */ EasyRdf_Literal::setDatatypeMapping('xsd:boolean', 'EasyRdf_Literal_Boolean'); EasyRdf_Literal::setDatatypeMapping('xsd:date', 'EasyRdf_Literal_Date'); EasyRdf_Literal::setDatatypeMapping('xsd:dateTime', 'EasyRdf_Literal_DateTime'); EasyRdf_Literal::setDatatypeMapping('xsd:decimal', 'EasyRdf_Literal_Decimal'); EasyRdf_Literal::setDatatypeMapping('xsd:hexBinary', 'EasyRdf_Literal_HexBinary'); EasyRdf_Literal::setDatatypeMapping('rdf:HTML', 'EasyRdf_Literal_HTML'); EasyRdf_Literal::setDatatypeMapping('xsd:integer', 'EasyRdf_Literal_Integer'); EasyRdf_Literal::setDatatypeMapping('rdf:XMLLiteral', 'EasyRdf_Literal_XML');
/** 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 */ public function addLiteral($resource, $property, $value, $lang = null) { $this->checkResourceParam($resource); $this->checkPropertyParam($property, $inverse); if (is_array($value)) { foreach ($value as $v) { $this->addLiteral($resource, $property, $v, $lang); } return; } else { if ($lang) { $value = array('type' => 'literal', 'value' => $value, 'lang' => $lang); } else { $value = array('type' => 'literal', 'value' => $value, 'datatype' => EasyRdf_Literal::getDatatypeForValue($value)); if (empty($value['datatype'])) { unset($value['datatype']); } } } return $this->add($resource, $property, $value); }
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); }
/** Constructor for creating a new decimal literal * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:decimal') * @return object EasyRdf_Literal_Decimal */ public function __construct($value, $lang = null, $datatype = null) { parent::__construct($value, null, $datatype); }
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); }
/** 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 testDumpValueWithDatatype() { $literal = new EasyRdf_Literal(1, null, 'xsd:integer'); $this->assertEquals('"1"^^xsd:integer', $literal->dumpValue(false)); $this->assertEquals("<span style='color:blue'>"1"^^xsd:integer</span>", $literal->dumpValue(true)); }