class EasyRdf_Format in Zircon Profile 8.0
Same name and namespace in other branches
- 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Format.php \EasyRdf_Format
Class the represents an RDF file format.
For each format, the name, label, URIs and associated MIME Types are stored. A single parser and serialiser can also be registered to each format.
@package EasyRdf @copyright Copyright (c) 2009-2013 Nicholas J Humfrey @license http://www.opensource.org/licenses/bsd-license.php
Hierarchy
- class \EasyRdf_Format
Expanded class hierarchy of EasyRdf_Format
File
- vendor/
easyrdf/ easyrdf/ lib/ EasyRdf/ Format.php, line 49
View source
class EasyRdf_Format {
private static $formats = array();
private $name = array();
private $label = null;
private $uri = null;
private $mimeTypes = array();
private $extensions = array();
private $parserClass = null;
private $serialiserClass = null;
/** Get a list of format names
*
* @return array An array of formats name
*/
public static function getNames() {
return array_keys(self::$formats);
}
/** Get a list of all the registered formats
*
* @return array An array of format objects
*/
public static function getFormats() {
return self::$formats;
}
/** Generates an HTTP Accept header string
*
* The string will contain all of the MIME Types that we
* are able to parse.
*
* It is also possible to specify additional MIME types
* in the form array('text/plain' => 0.5) where 0.5 is the
* q value for that type. The types are sorted by q value
* before constructing the string.
*
* @param array $extraTypes extra MIME types to add
* @return string list of supported MIME types
*/
public static function getHttpAcceptHeader($extraTypes = array()) {
$accept = $extraTypes;
foreach (self::$formats as $format) {
if ($format->parserClass and count($format->mimeTypes) > 0) {
$accept = array_merge($accept, $format->mimeTypes);
}
}
arsort($accept, SORT_NUMERIC);
$acceptStr = '';
foreach ($accept as $type => $q) {
if ($acceptStr) {
$acceptStr .= ',';
}
if ($q == 1.0) {
$acceptStr .= $type;
}
else {
$acceptStr .= sprintf("%s;q=%1.1F", $type, $q);
}
}
return $acceptStr;
}
/** Check if a named graph exists
*
* @param string $name the name of the format
* @return boolean true if the format exists
*/
public static function formatExists($name) {
return array_key_exists($name, self::$formats);
}
/** Get a EasyRdf_Format from a name, uri or mime type
*
* @param string $query a query string to search for
* @return object the first EasyRdf_Format that matches the query
* @throws EasyRdf_Exception if no format is found
*/
public static function getFormat($query) {
if (!is_string($query) or $query == null or $query == '') {
throw new InvalidArgumentException("\$query should be a string and cannot be null or empty");
}
foreach (self::$formats as $format) {
if ($query == $format->name or $query == $format->uri or array_key_exists($query, $format->mimeTypes) or in_array($query, $format->extensions)) {
return $format;
}
}
# No match
throw new EasyRdf_Exception("Format is not recognised: {$query}");
}
/** Register a new format
*
* @param string $name The name of the format (e.g. ntriples)
* @param string $label The label for the format (e.g. N-Triples)
* @param string $uri The URI for the format
* @param string $mimeTypes One or more mime types for the format
* @param string $extensions One or more extensions (file suffix)
* @return object The new EasyRdf_Format object
*/
public static function register($name, $label = null, $uri = null, $mimeTypes = array(), $extensions = array()) {
if (!is_string($name) or $name == null or $name == '') {
throw new InvalidArgumentException("\$name should be a string and cannot be null or empty");
}
if (!array_key_exists($name, self::$formats)) {
self::$formats[$name] = new EasyRdf_Format($name);
}
self::$formats[$name]
->setLabel($label);
self::$formats[$name]
->setUri($uri);
self::$formats[$name]
->setMimeTypes($mimeTypes);
self::$formats[$name]
->setExtensions($extensions);
return self::$formats[$name];
}
/** Remove a format from the registry
*
* @param string $name The name of the format (e.g. ntriples)
*/
public static function unregister($name) {
unset(self::$formats[$name]);
}
/** Class method to register a parser class to a format name
*
* @param string $name The name of the format (e.g. ntriples)
* @param string $class The name of the class (e.g. EasyRdf_Parser_Ntriples)
*/
public static function registerParser($name, $class) {
if (!self::formatExists($name)) {
self::register($name);
}
self::getFormat($name)
->setParserClass($class);
}
/** Class method to register a serialiser class to a format name
*
* @param string $name The name of the format (e.g. ntriples)
* @param string $class The name of the class (e.g. EasyRdf_Serialiser_Ntriples)
*/
public static function registerSerialiser($name, $class) {
if (!self::formatExists($name)) {
self::register($name);
}
self::getFormat($name)
->setSerialiserClass($class);
}
/** Attempt to guess the document format from some content.
*
* If $filename is given, then the suffix is first used to guess the format.
*
* If the document format is not recognised, null is returned.
*
* @param string $data The document data
* @param string $filename Optional filename
* @return object EasyRdf_Format The format object
*/
public static function guessFormat($data, $filename = null) {
if (is_array($data)) {
# Data has already been parsed into RDF/PHP
return self::getFormat('php');
}
// First try and identify by the filename
if ($filename and preg_match('/\\.(\\w+)$/', $filename, $matches)) {
foreach (self::$formats as $format) {
if (in_array($matches[1], $format->extensions)) {
return $format;
}
}
}
// Then try and guess by the first 1024 bytes of content
$short = substr($data, 0, 1024);
if (preg_match('/^\\s*\\{/', $short)) {
return self::getFormat('json');
}
elseif (preg_match('/<rdf:/i', $short)) {
return self::getFormat('rdfxml');
}
elseif (preg_match('|http://www.w3.org/2005/sparql-results|', $short)) {
return self::getFormat('sparql-xml');
}
elseif (preg_match('/\\WRDFa\\W/i', $short)) {
return self::getFormat('rdfa');
}
elseif (preg_match('/<!DOCTYPE html|<html/i', $short)) {
# We don't support any other microformats embedded in HTML
return self::getFormat('rdfa');
}
elseif (preg_match('/@prefix\\s|@base\\s/', $short)) {
return self::getFormat('turtle');
}
elseif (preg_match('/^\\s*<.+> <.+>/m', $short)) {
return self::getFormat('ntriples');
}
else {
return null;
}
}
/**
* This constructor is for internal use only.
* To create a new format, use the register method.
*
* @param string $name The name of the format
* @see EasyRdf_Format::register()
* @ignore
*/
public function __construct($name) {
$this->name = $name;
$this->label = $name;
# Only a default
}
/** Get the name of a format object
*
* @return string The name of the format (e.g. rdfxml)
*/
public function getName() {
return $this->name;
}
/** Get the label for a format object
*
* @return string The format label (e.g. RDF/XML)
*/
public function getLabel() {
return $this->label;
}
/** Set the label for a format object
*
* @param string $label The new label for the format
*/
public function setLabel($label) {
if ($label) {
if (!is_string($label)) {
throw new InvalidArgumentException("\$label should be a string");
}
return $this->label = $label;
}
else {
return $this->label = null;
}
}
/** Get the URI for a format object
*
* @return string The format URI
*/
public function getUri() {
return $this->uri;
}
/** Set the URI for a format object
*
* @param string $uri The new URI for the format
*/
public function setUri($uri) {
if ($uri) {
if (!is_string($uri)) {
throw new InvalidArgumentException("\$uri should be a string");
}
return $this->uri = $uri;
}
else {
return $this->uri = null;
}
}
/** Get the default registered mime type for a format object
*
* @return string The default mime type as a string.
*/
public function getDefaultMimeType() {
$types = array_keys($this->mimeTypes);
if (isset($types[0])) {
return $types[0];
}
}
/** Get all the registered mime types for a format object
*
* @return array One or more MIME types in an array with
* the mime type as the key and q value as the value
*/
public function getMimeTypes() {
return $this->mimeTypes;
}
/** Set the MIME Types for a format object
*
* @param array $mimeTypes One or more mime types
*/
public function setMimeTypes($mimeTypes) {
if ($mimeTypes) {
if (!is_array($mimeTypes)) {
$mimeTypes = array(
$mimeTypes,
);
}
$this->mimeTypes = $mimeTypes;
}
else {
$this->mimeTypes = array();
}
}
/** Get the default registered file extension (filename suffix) for a format object
*
* @return string The default extension as a string.
*/
public function getDefaultExtension() {
if (isset($this->extensions[0])) {
return $this->extensions[0];
}
}
/** Get all the registered file extensions (filename suffix) for a format object
*
* @return array One or more extensions as an array
*/
public function getExtensions() {
return $this->extensions;
}
/** Set the file format extensions (filename suffix) for a format object
*
* @param mixed $extensions One or more file extensions
*/
public function setExtensions($extensions) {
if ($extensions) {
if (!is_array($extensions)) {
$extensions = array(
$extensions,
);
}
$this->extensions = $extensions;
}
else {
$this->extensions = array();
}
}
/** Set the parser to use for a format
*
* @param string $class The name of the class
*/
public function setParserClass($class) {
if ($class) {
if (!is_string($class)) {
throw new InvalidArgumentException("\$class should be a string");
}
$this->parserClass = $class;
}
else {
$this->parserClass = null;
}
}
/** Get the name of the class to use to parse the format
*
* @return string The name of the class
*/
public function getParserClass() {
return $this->parserClass;
}
/** Create a new parser to parse this format
*
* @return object The new parser object
*/
public function newParser() {
$parserClass = $this->parserClass;
if (!$parserClass) {
throw new EasyRdf_Exception("No parser class available for format: " . $this
->getName());
}
return new $parserClass();
}
/** Set the serialiser to use for a format
*
* @param string $class The name of the class
*/
public function setSerialiserClass($class) {
if ($class) {
if (!is_string($class)) {
throw new InvalidArgumentException("\$class should be a string");
}
$this->serialiserClass = $class;
}
else {
$this->serialiserClass = null;
}
}
/** Get the name of the class to use to serialise the format
*
* @return string The name of the class
*/
public function getSerialiserClass() {
return $this->serialiserClass;
}
/** Create a new serialiser to parse this format
*
* @return object The new serialiser object
*/
public function newSerialiser() {
$serialiserClass = $this->serialiserClass;
if (!$serialiserClass) {
throw new EasyRdf_Exception("No serialiser class available for format: " . $this
->getName());
}
return new $serialiserClass();
}
/** Magic method to return the name of the format when casted to string
*
* @return string The name of the format
*/
public function __toString() {
return $this->name;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EasyRdf_Format:: |
private | property | ||
EasyRdf_Format:: |
private static | property | ||
EasyRdf_Format:: |
private | property | ||
EasyRdf_Format:: |
private | property | ||
EasyRdf_Format:: |
private | property | ||
EasyRdf_Format:: |
private | property | ||
EasyRdf_Format:: |
private | property | ||
EasyRdf_Format:: |
private | property | ||
EasyRdf_Format:: |
public static | function | Check if a named graph exists | |
EasyRdf_Format:: |
public | function | Get the default registered file extension (filename suffix) for a format object | |
EasyRdf_Format:: |
public | function | Get the default registered mime type for a format object | |
EasyRdf_Format:: |
public | function | Get all the registered file extensions (filename suffix) for a format object | |
EasyRdf_Format:: |
public static | function | Get a EasyRdf_Format from a name, uri or mime type | |
EasyRdf_Format:: |
public static | function | Get a list of all the registered formats | |
EasyRdf_Format:: |
public static | function | Generates an HTTP Accept header string | |
EasyRdf_Format:: |
public | function | Get the label for a format object | |
EasyRdf_Format:: |
public | function | Get all the registered mime types for a format object | |
EasyRdf_Format:: |
public | function | Get the name of a format object | |
EasyRdf_Format:: |
public static | function | Get a list of format names | |
EasyRdf_Format:: |
public | function | Get the name of the class to use to parse the format | |
EasyRdf_Format:: |
public | function | Get the name of the class to use to serialise the format | |
EasyRdf_Format:: |
public | function | Get the URI for a format object | |
EasyRdf_Format:: |
public static | function | Attempt to guess the document format from some content. | |
EasyRdf_Format:: |
public | function | Create a new parser to parse this format | |
EasyRdf_Format:: |
public | function | Create a new serialiser to parse this format | |
EasyRdf_Format:: |
public static | function | Register a new format | |
EasyRdf_Format:: |
public static | function | Class method to register a parser class to a format name | |
EasyRdf_Format:: |
public static | function | Class method to register a serialiser class to a format name | |
EasyRdf_Format:: |
public | function | Set the file format extensions (filename suffix) for a format object | |
EasyRdf_Format:: |
public | function | Set the label for a format object | |
EasyRdf_Format:: |
public | function | Set the MIME Types for a format object | |
EasyRdf_Format:: |
public | function | Set the parser to use for a format | |
EasyRdf_Format:: |
public | function | Set the serialiser to use for a format | |
EasyRdf_Format:: |
public | function | Set the URI for a format object | |
EasyRdf_Format:: |
public static | function | Remove a format from the registry | |
EasyRdf_Format:: |
public | function | This constructor is for internal use only. To create a new format, use the register method. | |
EasyRdf_Format:: |
public | function | Magic method to return the name of the format when casted to string |