Purtle
Fast streaming RDF serializer for PHP
Loading...
Searching...
No Matches
Wikimedia\Purtle\RdfWriter Interface Reference

Writer interface for RDF output. More...

+ Inheritance diagram for Wikimedia\Purtle\RdfWriter:

Public Member Functions

 blank ( $label=null)
 Returns the local name of a blank node, for use with the "_" prefix.
 
 start ()
 Start the document.
 
 finish ()
 Finish the document.
 
 drain ()
 Generates an RDF string from the current buffers state and returns it.
 
 prefix ( $prefix, $iri)
 Declare a prefix for later use.
 
 about ( $base, $local=null)
 Start an "about" (subject) clause, given a subject.
 
 say ( $base, $local=null)
 Start a predicate clause.
 
 is ( $base, $local=null)
 Produce a resource as the object of a statement.
 
 text ( $text, $language=null)
 Produce a text literal as the object of a statement.
 
 value ( $value, $typeBase=null, $typeLocal=null)
 Produce a typed or untyped literal as the object of a statement.
 
 a ( $typeBase, $typeLocal=null)
 Shorthand for say( 'a' )->is( $type ).
 
 sub ()
 Returns a document-level sub-writer.
 
 getMimeType ()
 Returns the MIME type of the RDF serialization the writer produces.
 

Detailed Description

Writer interface for RDF output.

RdfWriter instances are generally stateful, but should be implemented to operate in a stream-like manner with a minimum of state.

This is intended to provide a "fluent interface" that allows programmers to use a turtle-like structure when generating RDF output. E.g.:

$writer->prefix( 'acme', 'http://acme.test/terms/' );
$writer->about( 'http://quux.test/Something' )
->say( 'acme', 'name' )->text( 'Thingy' )->text( 'Dingsda', 'de' )
->say( 'acme', 'owner' )->is( 'http://quux.test/' );

To get the generated RDF output, use the drain() method.

Note
: The contract of this interface follows the GIGO principle, that is, implementations are not required to ensure valid output or prompt failure on invalid input. Speed should generally be favored over safety.

Caveats:

  • no relative iris
  • predicates must be qnames
  • no inline/nested blank nodes
  • no comments
  • no collections
  • no automatic conversion of iris to qnames

@license GPL-2.0-or-later

Author
Daniel Kinzler

Member Function Documentation

◆ a()

Wikimedia\Purtle\RdfWriter::a ( $typeBase,
$typeLocal = null )

Shorthand for say( 'a' )->is( $type ).

Parameters
string$typeBaseThe data type's QName prefix if $typeLocal is given, or an IRI or shorthand if $typeLocal is null.
string | null$typeLocalThe data type's QName suffix, or null if $typeBase is an IRI or shorthand.
Returns
RdfWriter $this

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ about()

Wikimedia\Purtle\RdfWriter::about ( $base,
$local = null )

Start an "about" (subject) clause, given a subject.

Can occur at the beginning odf the output sequence, but can later only follow a call to is(), text(), or value(). Should fail if called at an inappropriate time in the output sequence.

Parameters
string$baseA QName prefix if $local is given, or an IRI if $local is null.
string | null$localA QName suffix, or null if $base is an IRI.
Returns
RdfWriter $this

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ blank()

Wikimedia\Purtle\RdfWriter::blank ( $label = null)

Returns the local name of a blank node, for use with the "_" prefix.

Parameters
string | null$labelnode label, will be generated if not given.
Returns
string A local name for the blank node, for use with the '_' prefix.

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ drain()

Wikimedia\Purtle\RdfWriter::drain ( )

Generates an RDF string from the current buffers state and returns it.

The buffer is reset to the empty state. Before the result string is generated, implementations should close any pending syntactical structures (close tags, generate footers, etc).

Returns
string The RDF output

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ finish()

Wikimedia\Purtle\RdfWriter::finish ( )

Finish the document.

May generate a footer.

This will detach all sub-writers that had earlier been returned by sub().

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ getMimeType()

Wikimedia\Purtle\RdfWriter::getMimeType ( )

Returns the MIME type of the RDF serialization the writer produces.

Returns
string a MIME type

Implemented in Wikimedia\Purtle\JsonLdRdfWriter, Wikimedia\Purtle\NTriplesRdfWriter, Wikimedia\Purtle\TurtleRdfWriter, and Wikimedia\Purtle\XmlRdfWriter.

◆ is()

Wikimedia\Purtle\RdfWriter::is ( $base,
$local = null )

Produce a resource as the object of a statement.

Can only follow a call to say() or a call to one of is(), text(), or value(). Should fail if called at an inappropriate time in the output sequence.

Parameters
string$baseA QName prefix if $local is given, or an IRI or shorthand if $local is null.
string | null$localA QName suffix, or null if $base is an IRI or shorthand.
Returns
RdfWriter $this

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ prefix()

Wikimedia\Purtle\RdfWriter::prefix ( $prefix,
$iri )

Declare a prefix for later use.

Prefixes should be declared before being used. Should not be called after start().

Parameters
string$prefix
string$iria IRI

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ say()

Wikimedia\Purtle\RdfWriter::say ( $base,
$local = null )

Start a predicate clause.

Can only follow a call to about() or say(). Should fail if called at an inappropriate time in the output sequence.

Note
Unlike about() and is(), say() cannot be called with a full IRI, but must always use qname form. This is required to cater to output formats that do not allow IRIs to be used as predicates directly, like RDF/XML.
Parameters
string$baseA QName prefix if $local is given, or a shorthand. MUST NOT be an IRI.
string | null$localA QName suffix, or null if $base is a shorthand.
Returns
RdfWriter $this

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ start()

Wikimedia\Purtle\RdfWriter::start ( )

Start the document.

May generate a header.

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ sub()

Wikimedia\Purtle\RdfWriter::sub ( )

Returns a document-level sub-writer.

This can be used to generate parts statements out of sequence. Output generated by the sub-writer will be present in the return value of drain(), after any output generated by this writer itself.

Note
calling drain() on sub-writers results in undefined behavior!
using sub-writers after finish() has been called on this writer results in undefined behavior!
Returns
RdfWriter

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ text()

Wikimedia\Purtle\RdfWriter::text ( $text,
$language = null )

Produce a text literal as the object of a statement.

Can only follow a call to say() or a call to one of is(), text(), or value(). Should fail if called at an inappropriate time in the output sequence.

Parameters
string$textthe text to be placed in the output
string | null$languagethe language the text is in
Returns
RdfWriter $this

Implemented in Wikimedia\Purtle\RdfWriterBase.

◆ value()

Wikimedia\Purtle\RdfWriter::value ( $value,
$typeBase = null,
$typeLocal = null )

Produce a typed or untyped literal as the object of a statement.

Can only follow a call to say() or a call to one of is(), text(), or value(). Should fail if called at an inappropriate time in the output sequence.

Parameters
string$valuethe value encoded as a string
string | null$typeBaseThe data type's QName prefix if $typeLocal is given, or an IRI or shorthand if $typeLocal is null.
string | null$typeLocalThe data type's QName suffix, or null if $typeBase is an IRI or shorthand.
Returns
RdfWriter $this

Implemented in Wikimedia\Purtle\RdfWriterBase.


The documentation for this interface was generated from the following file: