PHP DataConvertors
From n² wiki
This is now out of date see the code and documentation in SVN
In our applications, we often need to munge data into different forms, for example from RDF/XML to an array of triples, to an array of resource hashes, to turtle, to JSON. I wrote some convertor classes for my applications, but unfortunately they don't work out of the box in applications with different file structures. It would be good to be able to wrap them up as a component or set of components that can be more easily dropped into other PHP applications.
The dependencies that my convertors rely on are:
- The ARC RDF/XML parser (and possibly in the future other parsers, and processors eg for turtle, JSON, eRDF, RDFa, GRDDL)
Here are some of the convertors I am using so far
RDFXML
->value() // an RDF/XML string
->to_triples()
->to_resources() //needs namespaces array
Triples
->value() // an array of triples, as returned by the ARC RDF/XML parser
->to_resources()
ResourcesArray
->value() // an associative array like the RDF/JSON structure
->to_rdfxml()
->to_turtle()
->to_resource($uri) // creates and returns a ResourceArray object of the
->filter_by_language($lang) //returns resources with only literals of the same, or unspecified, language
->filter_by_object_value($value) // returns only resources with a given object value.
ResourceArray
->value() // an associative array like the RDF/JSON structure, containing only one resource
->property($property_qname) // returns the value of the given property of this resource.
->get_title() //returns the foaf:name, dc:title, or rdfs:label of the resource
SPARQL_XML
->value() // an XML document string
->to_boolean() //returns a boolean from an ASK query result document
->to_array() // returns an associative array from a select query result
URI
->value() // a URI string
->to_qname() //needs namespaces array
->to_term() // returns the part of the uri after the last # or /
->to_namespace() returns the part of the URI up to and including the last # or /
Qname
->value() // a qname, eg: 'rdf:type'
->to_uri() //needs namespaces array
->to_eRDF_value() // replaces the first : for a - .
These are (now) wrapped up in an object which has these convertors as properties.
The object is instantiated with an array of prefix=>namespace to be used.
$Convertor = new Convertor($namespaces_array); $select_array = $Convertor->SPARQL_XML->to_array($xml_results)->value();
So what would be good if we could discuss how to best componentise the library of convertors and any improvements to the API. For instance, I find the filters and property getters for the ResourceArray convenient, but perhaps that functionality ought to be separate from the convertors.

