/** Constructor * */ public function __construct($value, $lang = null, $datatype = null) { if (EasyRdf_Utils::is_associative_array($value)) { $this->_value = isset($value['value']) ? $value['value'] : null; $this->_lang = isset($value['lang']) ? $value['lang'] : null; $this->_datatype = isset($value['datatype']) ? $value['datatype'] : null; } else { $this->_value = $value; $this->_lang = $lang ? $lang : null; $this->_datatype = $datatype ? $datatype : null; } // Automatic datatype selection if ($this->_datatype == null) { if (is_float($this->_value)) { $this->_datatype = 'xsd:decimal'; } else { if (is_int($this->_value)) { $this->_datatype = 'xsd:integer'; } else { if (is_bool($this->_value)) { $this->_datatype = 'xsd:boolean'; } } } } // Expand shortened URIs (qnames) if ($this->_datatype) { $this->_datatype = EasyRdf_Namespace::expand($this->_datatype); } }
protected function generateList($subject, $property, $list) { $current = $subject; $prop = $property; // Output a blank node for each item in the list foreach ($list as $item) { $newNode = $this->_graph->newBNodeId(); $this->addTriple($current, $prop, array('type' => 'bnode', 'value' => $newNode)); $this->addTriple($newNode, 'rdf:first', $item); $current = $newNode; $prop = 'rdf:rest'; } // Finally, terminate the list $this->addTriple($current, $prop, array('type' => 'uri', 'value' => EasyRdf_Namespace::expand('rdf:nil'))); }
public function testExpandNonString() { $this->setExpectedException('InvalidArgumentException', '$shortUri should be a string and cannot be null or empty'); EasyRdf_Namespace::expand($this); }
/** Get all the resources in the graph of a certain type * * If no resources of the type are available and empty * array is returned. * * @param string $type The type of the resource (e.g. foaf:Person) * @return array The array of resources */ public function allOfType($type) { $uri = EasyRdf_Namespace::expand($type); $resource = $this->resource($uri); return $resource->all('-rdf:type'); }
/** * Delete an existing RDF type mapping. * * @param string $type The RDF type (e.g. foaf:Person) */ public static function delete($type) { if (!is_string($type) or $type == null or $type == '') { throw new InvalidArgumentException("\$type should be a string and cannot be null or empty"); } $type = EasyRdf_Namespace::expand($type); if (isset(self::$_map[$type])) { unset(self::$_map[$type]); } }
/** Add one or more rdf:type properties to a resource * * @param string $resource The resource to add the type to * @param string $type The new type (e.g. foaf:Person) */ public function addType($resource, $types) { $this->checkResourceParam($resource, true); if (!is_array($types)) { $types = array($types); } foreach ($types as $type) { $type = EasyRdf_Namespace::expand($type); $this->add($resource, 'rdf:type', array('type' => 'uri', 'value' => $type)); } }
/** * Method to serialise an EasyRdf_Graph into RDF/PHP * * http://n2.talis.com/wiki/RDF_PHP_Specification */ protected function to_rdfphp($graph) { $rdfphp = array(); foreach ($graph->resources() as $resource) { $properties = $resource->properties(); if (count($properties) == 0) { continue; } $subj = $resource->getUri(); if (!isset($rdfphp[$subj])) { $rdfphp[$subj] = array(); } foreach ($properties as $property) { $prop = EasyRdf_Namespace::expand($property); if ($prop) { if (!isset($rdfphp[$subj][$prop])) { $rdfphp[$subj][$prop] = array(); } $objects = $resource->all($property); foreach ($objects as $obj) { if (is_object($obj) and $obj instanceof EasyRdf_Resource) { if ($obj->isBNode()) { $object = array('type' => 'bnode', 'value' => $obj->getUri()); } else { $object = array('type' => 'uri', 'value' => $obj->getUri()); } } else { $object = array('type' => 'literal', 'value' => $obj); } array_push($rdfphp[$subj][$prop], $object); } } } } return $rdfphp; }
/** Constructor for creating a new literal * * @param string $value The value of the literal * @param string $lang The natural language of the literal or null (e.g. 'en') * @param string $datatype The datatype of the literal or null (e.g. 'xsd:string') * @return object EasyRdf_Literal */ public function __construct($value, $lang = null, $datatype = null) { $this->_value = $value; $this->_lang = $lang ? $lang : null; $this->_datatype = $datatype ? $datatype : null; if ($this->_datatype) { // Expand shortened URIs (qnames) $this->_datatype = EasyRdf_Namespace::expand($this->_datatype); // Literals can not have both a language and a datatype $this->_lang = null; } else { // Set the datatype based on the subclass $class = get_class($this); if (isset(self::$_classMap[$class])) { $this->_datatype = self::$_classMap[$class]; $this->_lang = null; } } // Cast to string if it is a string if ($this->_lang or !$this->_datatype or $this->_datatype == 'http://www.w3.org/2001/XMLSchema#string') { settype($this->_value, 'string'); } }
/** * Formats a sparql query clause for limiting the search to specific concept types. * @param array $types limit search to concepts of the given type(s) * @param string $arrayClass the URI for thesaurus array class, or null if not used * @return string sparql query clause */ protected function formatTypes($types, $arrayClass) { $unprefixed_types = array(); $type = ''; if (!empty($types)) { foreach ($types as $type) { $unprefixed_types[] = EasyRdf_Namespace::expand($type); } } // extra types to query, if using thesaurus arrays and no additional type restrictions have been applied $extratypes = $arrayClass && $types === array('skos:Concept') ? "UNION { ?s a <{$arrayClass}> }" : ""; if (sizeof($unprefixed_types) === 1) { $type = '<' . $unprefixed_types[0] . '>'; } else { // multiple type limitations require setting a UNION for each of those $type = '[]'; foreach ($unprefixed_types as $utype) { $extratypes .= "\nUNION { ?s a <{$utype}> }"; } } return "{ ?s rdf:type {$type} } UNION { ?s a isothes:ConceptGroup } {$extratypes}"; }
/** Constructor for creating a new literal * * @param string $value The value of the literal * @param string $lang The natural language of the literal or null (e.g. 'en') * @param string $datatype The datatype of the literal or null (e.g. 'xsd:string') * @return object EasyRdf_Literal */ public function __construct($value, $lang = null, $datatype = null) { $this->value = $value; $this->lang = $lang ? $lang : null; $this->datatype = $datatype ? $datatype : null; if ($this->datatype) { if (is_object($this->datatype)) { // Convert objects to strings $this->datatype = strval($this->datatype); } else { // Expand shortened URIs (CURIEs) $this->datatype = EasyRdf_Namespace::expand($this->datatype); } // Literals can not have both a language and a datatype $this->lang = null; } else { // Set the datatype based on the subclass $class = get_class($this); if (isset(self::$classMap[$class])) { $this->datatype = self::$classMap[$class]; $this->lang = null; } } // Cast value to string settype($this->value, 'string'); }
/** Constructor for creating a new literal * * @param string $value The value of the literal * @param string $lang The natural language of the literal or null (e.g. 'en') * @param string $datatype The datatype of the literal or null (e.g. 'xsd:string') * @return object EasyRdf_Literal */ public function __construct($value, $lang = null, $datatype = null) { $this->value = $value; $this->lang = $lang ? $lang : null; $this->datatype = $datatype ? $datatype : null; if ($this->datatype) { if (is_object($this->datatype)) { // Convert objects to strings $this->datatype = strval($this->datatype); } else { // Expand shortened URIs (CURIEs) $this->datatype = EasyRdf_Namespace::expand($this->datatype); } // Literals can not have both a language and a datatype $this->lang = null; } else { // Set the datatype based on the subclass $class = get_class($this); if (isset(self::$classMap[$class])) { $this->datatype = self::$classMap[$class]; $this->lang = null; } } if (is_float($this->value)) { // special handling of floats, as they suffer from locale [mis]configuration $this->value = rtrim(sprintf('%F', $this->value), '0'); } else { // Cast value to string settype($this->value, 'string'); } }
public function testExpandNonString() { $this->setExpectedException('InvalidArgumentException'); EasyRdf_Namespace::expand($this); }
/** Check if a resource is of the specified type * * @param string $type The type to check (e.g. foaf:Person) * @return boolean True if resource is of specified type. */ public function is_a($type) { $type = EasyRdf_Namespace::expand($type); foreach ($this->all('rdf:type') as $t) { if ($t->getUri() == $type) { return true; } } return false; }