class Graph

Container for collection of EasyRdf\Resource objects.

Methods

__construct(string $uri = null, string $data = null, string $format = null)

Constructor

static Graph
newAndLoad(string $uri, string|null $format = null)

Create a new graph and load RDF data from a URI into it

resource(string $uri = null, mixed $types = array())

Get or create a resource stored in a graph

classForResource($uri)

Work out the class to instantiate a resource as

newBNode(mixed $types = array())

Create a new blank node in the graph and return it.

string
newBNodeId()

Create a new unique blank node identifier and return it.

integer
parse(string $data, string $format = null, string $uri = null)

Parse some RDF data into the graph object.

integer
parseFile(string $filename, string $format = null, string $uri = null)

Parse a file containing RDF data into the graph object.

integer
load(string $uri = null, string $format = null)

Load RDF data into the graph from a URI.

Resource[]
resources()

Get an associative array of all the resources stored in the graph.

Resource[]
resourcesMatching(string $property, mixed $value = null)

Get an arry of resources matching a certain property and optional value.

string
getUri()

Get the URI of the graph

checkResourceParam($resource, $allowNull = false)

Check that a URI/resource parameter is valid, and convert it to a string

checkSinglePropertyParam($property, $inverse)

Check that a single URI/property parameter (not a property path) is valid, and expand it if required

checkValueParam($value)

Check that a value parameter is valid, and convert it to an associative array if needed

mixed
get(string $resource, string $propertyPath, string $type = null, string $lang = null)

Get a single value for a property of a resource

mixed
getSingleProperty(string $resource, string $property, string $type = null, string $lang = null)

Get a single value for a property of a resource

getLiteral(string $resource, string|array $property, string $lang = null)

Get a single literal value for a property of a resource

getResource(string $resource, string|array $property)

Get a single resource value for a property of a resource

propertyValuesArray($resource, $property, $inverse = false)

Return all the values for a particular property of a resource

arrayToObject($data)

Get an EasyRdf\Resource or EasyRdf\Literal object from an associative array.

array
all(string $resource, string $propertyPath, string $type = null, string $lang = null)

Get all values for a property path

array
allForSingleProperty(string $resource, string $property, string $type = null, string $lang = null)

Get all values for a single property of a resource

array
allLiterals(string $resource, string $property, string $lang = null)

Get all literal values for a property of a resource

array
allResources(string $resource, string $property)

Get all resources for a property of a resource

array
allOfType(string $type)

Get all the resources in the graph of a certain type

integer
countValues(string $resource, string $property, string $type = null, string $lang = null)

Count the number of values for a property of a resource

string
join(mixed $resource, string $property, string $glue = ' ', string $lang = null)

Concatenate all values for a property of a resource into a string.

integer
add(mixed $resource, mixed $property, mixed $value)

Add data to the graph

integer
addLiteral(mixed $resource, mixed $property, mixed $value, string $lang = null)

Add a literal value as a property of a resource

integer
addResource(mixed $resource, mixed $property, mixed $resource2)

Add a resource as a property of another resource

integer
set(string $resource, string $property, mixed $value)

Set a value for a property

integer
delete(mixed $resource, string $property, mixed $value = null)

Delete a property (or optionally just a specific value)

integer
deleteSingleProperty(mixed $resource, string $property, mixed $value = null)

Delete a property (or optionally just a specific value)

integer
deleteResource(mixed $resource, mixed $property, mixed $resource2)

Delete a resource from a property of another resource

integer
deleteLiteral(mixed $resource, mixed $property, mixed $value, string $lang = null)

Delete a literal value from a property of a resource

deleteInverse($resource, $property, $value)

This function is for internal use only.

boolean
isEmpty()

Check if the graph contains any statements

array
properties(string $resource)

Get a list of all the shortened property names (qnames) for a resource.

array
propertyUris(string $resource)

Get a list of the full URIs for the properties of a resource.

array
reversePropertyUris(string $resource)

Get a list of the full URIs for the properties that point to a resource.

boolean
hasProperty(mixed $resource, string $property, mixed $value = null)

Check to see if a property exists for a resource.

mixed
serialise(mixed $format, array $options = array())

Serialise the graph into RDF

string
dump(string $format = 'html')

Return a human readable view of all the resources in the graph

string
dumpResource(mixed $resource, string $format = 'html')

Return a human readable view of a resource and its properties

string
type(string|null $resource = null)

Get the resource type of the graph

typeAsResource(mixed $resource = null)

Get the resource type of the graph as a EasyRdf\Resource

array
types(string|null $resource = null)

Get a list of types for a resource

Resource[]
typesAsResources(string|null $resource = null)

Get the resource types of the graph as a EasyRdf\Resource

boolean
isA(string $resource, string $type)

Check if a resource is of the specified type

integer
addType(string $resource, string $types)

Add one or more rdf:type properties to a resource

integer
setType(string $resource, string $type)

Change the rdf:type property for a resource

string
label(string|null $resource = null, string|null $lang = null)

Get a human readable label for a resource

primaryTopic(mixed $resource = null)

Get the primary topic of the graph

array
toRdfPhp()

Returns the graph as a RDF/PHP associative array

integer
countTriples()

Calculates the number of triples in the graph

string
__toString()

Magic method to return URI of resource when casted to string

string
__get(string $name)

Magic method to get a property of the graph

integer
__set(string $name, string $value)

Magic method to set the value for a property of the graph

boolean
__isset(string $name)

Magic method to check if a property exists

integer
__unset(string $name)

Magic method to delete a property of the graph

Details

at line 84
Graph __construct(string $uri = null, string $data = null, string $format = null)

Constructor

If no URI is given then an unnamed graph is created.

The $data parameter is optional and will be parsed into the graph if given.

The data format is optional and should be specified if it can't be guessed by EasyRdf.

Parameters

string $uri The URI of the graph
string $data Data for the graph
string $format The document type of the data (e.g. rdfxml)

Return Value

Graph

at line 115
static Graph newAndLoad(string $uri, string|null $format = null)

Create a new graph and load RDF data from a URI into it

This static function is shorthand for: $graph = new \EasyRdf\Graph($uri); $graph->load($uri, $format);

The document format is optional but should be specified if it can't be guessed or got from the HTTP headers.

If the document format is given, then the HTTP Accept header is set to the MIME type of the requested format.

Parameters

string $uri The URI of the data to load
string|null $format Optional format of the data (eg. rdfxml or text/turtle)

Return Value

Graph The new the graph object

at line 137
Resource resource(string $uri = null, mixed $types = array())

Get or create a resource stored in a graph

If the resource did not previously exist, then a new resource will be created. If you provide an RDF type and that type is registered with the EasyRdf\TypeMapper, then the resource will be an instance of the class registered.

If URI is null, then the URI of the graph is used.

Parameters

string $uri The URI of the resource
mixed $types RDF type of a new resource (e.g. foaf:Person)

Return Value

Resource

Exceptions

InvalidArgumentException

at line 166
protected classForResource($uri)

Work out the class to instantiate a resource as

@ignore

Parameters

$uri

at line 202
Resource newBNode(mixed $types = array())

Create a new blank node in the graph and return it.

If you provide an RDF type and that type is registered with the EasyRdf\TypeMapper, then the resource will be an instance of the class registered.

Parameters

mixed $types RDF type of a new blank node (e.g. foaf:Person)

Return Value

Resource The new blank node

at line 212
string newBNodeId()

Create a new unique blank node identifier and return it.

Return Value

string The new blank node identifier (e.g. _:genid1)

at line 227
integer parse(string $data, string $format = null, string $uri = null)

Parse some RDF data into the graph object.

Parameters

string $data Data to parse for the graph
string $format Optional format of the data
string $uri The URI of the data to load

Return Value

integer The number of triples added to the graph

Exceptions

Exception

at line 257
integer parseFile(string $filename, string $format = null, string $uri = null)

Parse a file containing RDF data into the graph object.

Parameters

string $filename The path of the file to load
string $format Optional format of the file
string $uri The URI of the file to load

Return Value

integer The number of triples added to the graph

at line 288
integer load(string $uri = null, string $format = null)

Load RDF data into the graph from a URI.

If no URI is given, then the URI of the graph will be used.

The document format is optional but should be specified if it can't be guessed or got from the HTTP headers.

If the document format is given, then the HTTP Accept header is set to the MIME type of the requested format.

Parameters

string $uri The URI of the data to load
string $format Optional format of the data (eg. rdfxml or text/turtle)

Return Value

integer The number of triples added to the graph

Exceptions

Exception
Exception

at line 378
Resource[] resources()

Get an associative array of all the resources stored in the graph.

The keys of the array is the URI of the EasyRdf\Resource.

Return Value

Resource[]

at line 412
Resource[] resourcesMatching(string $property, mixed $value = null)

Get an arry of resources matching a certain property and optional value.

For example this routine could be used as a way of getting everyone who has name: $people = $graph->resourcesMatching('foaf:name')

Or everyone who is male: $people = $graph->resourcesMatching('foaf:gender', 'male');

Or all homepages: $people = $graph->resourcesMatching('^foaf:homepage');

Parameters

string $property The property to check.
mixed $value Optional, the value of the propery to check for.

Return Value

Resource[]

at line 447
string getUri()

Get the URI of the graph

Return Value

string The URI of the graph

at line 455
protected checkResourceParam($resource, $allowNull = false)

Check that a URI/resource parameter is valid, and convert it to a string

@ignore

Parameters

$resource
$allowNull

at line 496
protected checkSinglePropertyParam($property, $inverse)

Check that a single URI/property parameter (not a property path) is valid, and expand it if required

@ignore

Parameters

$property
$inverse

at line 530
protected checkValueParam($value)

Check that a value parameter is valid, and convert it to an associative array if needed

@ignore

Parameters

$value

at line 603
mixed get(string $resource, string $propertyPath, string $type = null, string $lang = null)

Get a single value for a property of a resource

If multiple values are set for a property then the value returned may be arbitrary.

If $property is an array, then the first item in the array that matches a property that exists is returned.

This method will return null if the property does not exist.

Parameters

string $resource The URI of the resource (e.g. http://example.com/joe#me)
string $propertyPath A valid property path
string $type The type of value to filter by (e.g. literal or resource)
string $lang The language to filter by (e.g. en)

Return Value

mixed A value associated with the property

Exceptions

InvalidArgumentException

at line 657
protected mixed getSingleProperty(string $resource, string $property, string $type = null, string $lang = null)

Get a single value for a property of a resource

Parameters

string $resource The URI of the resource (e.g. http://example.com/joe#me)
string $property The name of the property (e.g. foaf:name)
string $type The type of value to filter by (e.g. literal or resource)
string $lang The language to filter by (e.g. en)

Return Value

mixed A value associated with the property

at line 706
Literal getLiteral(string $resource, string|array $property, string $lang = null)

Get a single literal value for a property of a resource

If multiple values are set for a property then the value returned may be arbitrary.

This method will return null if there is not literal value for the property.

Parameters

string $resource The URI of the resource (e.g. http://example.com/joe#me)
string|array $property The name of the property (e.g. foaf:name)
string $lang The language to filter by (e.g. en)

Return Value

Literal Literal value associated with the property

at line 724
Resource getResource(string $resource, string|array $property)

Get a single resource value for a property of a resource

If multiple values are set for a property then the value returned may be arbitrary.

This method will return null if there is not resource for the property.

Parameters

string $resource The URI of the resource (e.g. http://example.com/joe#me)
string|array $property The name of the property (e.g. foaf:name)

Return Value

Resource Resource associated with the property

at line 732
protected propertyValuesArray($resource, $property, $inverse = false)

Return all the values for a particular property of a resource

@ignore

Parameters

$resource
$property
$inverse

at line 755
protected arrayToObject($data)

Get an EasyRdf\Resource or EasyRdf\Literal object from an associative array.

@ignore

Parameters

$data

at line 780
array all(string $resource, string $propertyPath, string $type = null, string $lang = null)

Get all values for a property path

This method will return an empty array if the property does not exist.

Parameters

string $resource The URI of the resource (e.g. http://example.com/joe#me)
string $propertyPath A valid property path
string $type The type of value to filter by (e.g. literal)
string $lang The language to filter by (e.g. en)

Return Value

array An array of values associated with the property

Exceptions

InvalidArgumentException

at line 840
protected array allForSingleProperty(string $resource, string $property, string $type = null, string $lang = null)

Get all values for a single property of a resource

Parameters

string $resource The URI of the resource (e.g. http://example.com/joe#me)
string $property The name of the property (e.g. foaf:name)
string $type The type of value to filter by (e.g. literal)
string $lang The language to filter by (e.g. en)

Return Value

array An array of values associated with the property

at line 883
array allLiterals(string $resource, string $property, string $lang = null)

Get all literal values for a property of a resource

This method will return an empty array if the resource does not has any literal values for that property.

Parameters

string $resource The URI of the resource (e.g. http://example.com/joe#me)
string $property The name of the property (e.g. foaf:name)
string $lang The language to filter by (e.g. en)

Return Value

array An array of values associated with the property

at line 898
array allResources(string $resource, string $property)

Get all resources for a property of a resource

This method will return an empty array if the resource does not has any resources for that property.

Parameters

string $resource The URI of the resource (e.g. http://example.com/joe#me)
string $property The name of the property (e.g. foaf:name)

Return Value

array An array of values associated with the property

at line 912
array allOfType(string $type)

Get all the resources in the graph of a certain type

If no resources of the type are available and empty array is returned.

Parameters

string $type The type of the resource (e.g. foaf:Person)

Return Value

array The array of resources

at line 926
integer countValues(string $resource, string $property, string $type = null, string $lang = null)

Count the number of values for a property of a resource

Parameters

string $resource The URI of the resource (e.g. http://example.com/joe#me)
string $property The name of the property (e.g. foaf:name)
string $type The type of value to filter by (e.g. literal)
string $lang The language to filter by (e.g. en)

Return Value

integer The number of values for this property

at line 943
string join(mixed $resource, string $property, string $glue = ' ', string $lang = null)

Concatenate all values for a property of a resource into a string.

The default is to join the values together with a space character. This method will return an empty string if the property does not exist.

Parameters

mixed $resource The resource to get the property on
string $property The name of the property (e.g. foaf:name)
string $glue The string to glue the values together with.
string $lang The language to filter by (e.g. en)

Return Value

string Concatenation of all the values.

at line 961
integer add(mixed $resource, mixed $property, mixed $value)

Add data to the graph

The resource can either be a resource or the URI of a resource.

Example: $graph->add("http://www.example.com", 'dc:title', 'Title of Page');

Parameters

mixed $resource The resource to add data to
mixed $property The property name
mixed $value The new value for the property

Return Value

integer The number of values added (1 or 0)

at line 1010
integer addLiteral(mixed $resource, mixed $property, mixed $value, string $lang = null)

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');

Parameters

mixed $resource The resource to add data to
mixed $property The property name
mixed $value The value or values for the property
string $lang The language of the literal

Return Value

integer The number of values added

at line 1040
integer addResource(mixed $resource, mixed $property, mixed $resource2)

Add a resource as a property of another resource

The resource can either be a resource or the URI of a resource.

Example: $graph->add("http://example.com/bob", 'foaf:knows', 'http://example.com/alice');

Parameters

mixed $resource The resource to add data to
mixed $property The property name
mixed $resource2 The resource to be value of the property

Return Value

integer The number of values added

at line 1066
integer set(string $resource, string $property, mixed $value)

Set a value for a property

The new value will replace the existing values for the property.

Parameters

string $resource The resource to set the property on
string $property The name of the property (e.g. foaf:name)
mixed $value The value for the property

Return Value

integer The number of values added (1 or 0)

at line 1088
integer delete(mixed $resource, string $property, mixed $value = null)

Delete a property (or optionally just a specific value)

Parameters

mixed $resource The resource to delete the property from
string $property The name of the property (e.g. foaf:name)
mixed $value The value to delete (null to delete all values)

Return Value

integer The number of values deleted

Exceptions

InvalidArgumentException

at line 1121
integer deleteSingleProperty(mixed $resource, string $property, mixed $value = null)

Delete a property (or optionally just a specific value)

Parameters

mixed $resource The resource to delete the property from
string $property The name of the property (e.g. foaf:name)
mixed $value The value to delete (null to delete all values)

Return Value

integer The number of values deleted

at line 1170
integer deleteResource(mixed $resource, mixed $property, mixed $resource2)

Delete a resource from a property of another resource

The resource can either be a resource or the URI of a resource.

Example: $graph->delete("http://example.com/bob", 'foaf:knows', 'http://example.com/alice');

Parameters

mixed $resource The resource to delete data from
mixed $property The property name
mixed $resource2 The resource value of the property to be deleted

Return Value

integer

at line 1198
integer deleteLiteral(mixed $resource, mixed $property, mixed $value, string $lang = null)

Delete a literal value from a property of a resource

Example: $graph->delete("http://www.example.com", 'dc:title', 'Title of Page');

Parameters

mixed $resource The resource to add data to
mixed $property The property name
mixed $value The value of the property
string $lang The language of the literal

Return Value

integer

at line 1217
protected deleteInverse($resource, $property, $value)

This function is for internal use only.

Deletes an inverse property from a resource.

Parameters

$resource
$property
$value

at line 1238
boolean isEmpty()

Check if the graph contains any statements

Return Value

boolean True if the graph contains no statements

at line 1251
array properties(string $resource)

Get a list of all the shortened property names (qnames) for a resource.

This method will return an empty array if the resource has no properties.

Parameters

string $resource

Return Value

array Array of shortened URIs

at line 1275
array propertyUris(string $resource)

Get a list of the full URIs for the properties of a resource.

This method will return an empty array if the resource has no properties.

Parameters

string $resource

Return Value

array Array of full URIs

at line 1292
array reversePropertyUris(string $resource)

Get a list of the full URIs for the properties that point to a resource.

Parameters

string $resource

Return Value

array Array of full property URIs

at line 1318
boolean hasProperty(mixed $resource, string $property, mixed $value = null)

Check to see if a property exists for a resource.

This method will return true if the property exists. If the value parameter is given, then it will only return true if the value also exists for that property.

By providing a value parameter you can use this function to check to see if a triple exists in the graph.

Parameters

mixed $resource The resource to check
string $property The name of the property (e.g. foaf:name)
mixed $value An optional value of the property

Return Value

boolean True if value the property exists.

at line 1359
mixed serialise(mixed $format, array $options = array())

Serialise the graph into RDF

The $format parameter can be an EasyRdf\Format object, a format name, a mime type or a file extension.

Example: $turtle = $graph->serialise('turtle');

Parameters

mixed $format The format to serialise to
array $options Serialiser-specific options, for fine-tuning the output

Return Value

mixed The serialised graph

at line 1378
string dump(string $format = 'html')

Return a human readable view of all the resources in the graph

This method is intended to be a debugging aid and will return a pretty-print view of all the resources and their properties.

Parameters

string $format Either 'html' or 'text'

Return Value

string

at line 1405
string dumpResource(mixed $resource, string $format = 'html')

Return a human readable view of a resource and its properties

This method is intended to be a debugging aid and will print a resource and its properties.

Parameters

mixed $resource The resource to dump
string $format Either 'html' or 'text'

Return Value

string

at line 1468
string type(string|null $resource = null)

Get the resource type of the graph

The type will be a shortened URI as a string. If the graph has multiple types then the type returned may be arbitrary. This method will return null if the resource has no type.

Parameters

string|null $resource

Return Value

string A type assocated with the resource (e.g. foaf:Document)

at line 1489
Resource typeAsResource(mixed $resource = null)

Get the resource type of the graph as a EasyRdf\Resource

If the graph has multiple types then the type returned may be arbitrary. This method will return null if the resource has no type.

Parameters

mixed $resource

Return Value

Resource A type associated with the resource

at line 1511
array types(string|null $resource = null)

Get a list of types for a resource

The types will each be a shortened URI as a string. This method will return an empty array if the resource has no types.

If $resource is null, then it will get the types for the URI of the graph.

Parameters

string|null $resource

Return Value

array All types assocated with the resource (e.g. foaf:Person)

at line 1530
Resource[] typesAsResources(string|null $resource = null)

Get the resource types of the graph as a EasyRdf\Resource

Parameters

string|null $resource

Return Value

Resource[]

at line 1548
boolean isA(string $resource, string $type)

Check if a resource is of the specified type

Parameters

string $resource The resource to check the type of
string $type The type to check (e.g. foaf:Person)

Return Value

boolean True if resource is of specified type

at line 1568
integer addType(string $resource, string $types)

Add one or more rdf:type properties to a resource

Parameters

string $resource The resource to add the type to
string $types One or more types to add (e.g. foaf:Person)

Return Value

integer The number of types added

at line 1595
integer setType(string $resource, string $type)

Change the rdf:type property for a resource

Note that if the resource object has already previously been created, then the PHP class of the resource will not change.

Parameters

string $resource The resource to change the type of
string $type The new type (e.g. foaf:Person)

Return Value

integer The number of types added

at line 1615
string label(string|null $resource = null, string|null $lang = null)

Get a human readable label for a resource

This method will check a number of properties for a resource (in the order: skos:prefLabel, rdfs:label, foaf:name, dc:title) and return an approriate first that is available. If no label is available then it will return null.

Parameters

string|null $resource
string|null $lang

Return Value

string A label for the resource.

at line 1637
Resource primaryTopic(mixed $resource = null)

Get the primary topic of the graph

Parameters

mixed $resource

Return Value

Resource The primary topic of the document.

at line 1656
array toRdfPhp()

Returns the graph as a RDF/PHP associative array

Return Value

array The contents of the graph as an array.

at line 1665
integer countTriples()

Calculates the number of triples in the graph

Return Value

integer The number of triples in the graph.

at line 1680
string __toString()

Magic method to return URI of resource when casted to string

Return Value

string The URI of the resource

at line 1697
string __get(string $name)

Magic method to get a property of the graph

Note that only properties in the default namespace can be accessed in this way.

Example: $value = $graph->title;

Parameters

string $name The name of the property

Return Value

string A single value for the named property

See also

RdfNamespace::setDefault

at line 1715
integer __set(string $name, string $value)

Magic method to set the value for a property of the graph

Note that only properties in the default namespace can be accessed in this way.

Example: $graph->title = 'Title';

Parameters

string $name The name of the property
string $value The value for the property

Return Value

integer

See also

RdfNamespace::setDefault

at line 1732
boolean __isset(string $name)

Magic method to check if a property exists

Note that only properties in the default namespace can be accessed in this way.

Example: if (isset($graph->title)) { blah(); }

Parameters

string $name The name of the property

Return Value

boolean

See also

RdfNamespace::setDefault

at line 1749
integer __unset(string $name)

Magic method to delete a property of the graph

Note that only properties in the default namespace can be accessed in this way.

Example: unset($graph->title);

Parameters

string $name The name of the property

Return Value

integer

See also

RdfNamespace::setDefault