3 namespace Wikimedia\Purtle;
6 * Writer interface for RDF output. RdfWriter instances are generally stateful,
7 * but should be implemented to operate in a stream-like manner with a minimum of state.
9 * This is intended to provide a "fluent interface" that allows programmers to use
10 * a turtle-like structure when generating RDF output. E.g.:
13 * $writer->prefix( 'acme', 'http://acme.test/terms/' );
14 * $writer->about( 'http://quux.test/Something' )
15 * ->say( 'acme', 'name' )->text( 'Thingy' )->text( 'Dingsda', 'de' )
16 * ->say( 'acme', 'owner' )->is( 'http://quux.test/' );
19 * To get the generated RDF output, use the drain() method.
21 * @note: The contract of this interface follows the GIGO principle, that is,
22 * implementations are not required to ensure valid output or prompt failure on
23 * invalid input. Speed should generally be favored over safety.
27 * - predicates must be qnames
28 * - no inline/nested blank nodes
31 * - no automatic conversion of iris to qnames
34 * @author Daniel Kinzler
38 //TODO: split: generic RdfWriter class with shorthands, use RdfFormatters for output
41 * Returns the local name of a blank node, for use with the "_" prefix.
43 * @param string|null $label node label, will be generated if not given.
45 * @return string A local name for the blank node, for use with the '_' prefix.
47 public function blank( $label = null );
50 * Start the document. May generate a header.
52 public function start();
55 * Finish the document. May generate a footer.
57 * This will detach all sub-writers that had earlier been returned by sub().
59 public function finish();
62 * Generates an RDF string from the current buffers state and returns it.
63 * The buffer is reset to the empty state.
64 * Before the result string is generated, implementations should close any
65 * pending syntactical structures (close tags, generate footers, etc).
67 * @return string The RDF output
69 public function drain();
72 * Declare a prefix for later use. Prefixes should be declared before being used.
73 * Should not be called after start().
75 * @param string $prefix
76 * @param string $iri a IRI
78 public function prefix( $prefix, $iri );
81 * Start an "about" (subject) clause, given a subject.
82 * Can occur at the beginning odf the output sequence, but can later only follow
83 * a call to is(), text(), or value().
84 * Should fail if called at an inappropriate time in the output sequence.
86 * @param string $base A QName prefix if $local is given, or an IRI if $local is null.
87 * @param string|null $local A QName suffix, or null if $base is an IRI.
89 * @return RdfWriter $this
91 public function about( $base, $local = null );
94 * Start a predicate clause.
95 * Can only follow a call to about() or say().
96 * Should fail if called at an inappropriate time in the output sequence.
98 * @note Unlike about() and is(), say() cannot be called with a full IRI,
99 * but must always use qname form. This is required to cater to output
100 * formats that do not allow IRIs to be used as predicates directly,
103 * @param string $base A QName prefix if $local is given, or a shorthand. MUST NOT be an IRI.
104 * @param string|null $local A QName suffix, or null if $base is a shorthand.
106 * @return RdfWriter $this
108 public function say( $base, $local = null );
111 * Produce a resource as the object of a statement.
112 * Can only follow a call to say() or a call to one of is(), text(), or value().
113 * Should fail if called at an inappropriate time in the output sequence.
115 * @param string $base A QName prefix if $local is given, or an IRI or shorthand if $local is null.
116 * @param string|null $local A QName suffix, or null if $base is an IRI or shorthand.
118 * @return RdfWriter $this
120 public function is( $base, $local = null );
123 * Produce a text literal as the object of a statement.
124 * Can only follow a call to say() or a call to one of is(), text(), or value().
125 * Should fail if called at an inappropriate time in the output sequence.
127 * @param string $text the text to be placed in the output
128 * @param string|null $language the language the text is in
130 * @return RdfWriter $this
132 public function text( $text, $language = null );
135 * Produce a typed or untyped literal as the object of a statement.
136 * Can only follow a call to say() or a call to one of is(), text(), or value().
137 * Should fail if called at an inappropriate time in the output sequence.
139 * @param string $value the value encoded as a string
140 * @param string|null $typeBase The data type's QName prefix if $typeLocal is given,
141 * or an IRI or shorthand if $typeLocal is null.
142 * @param string|null $typeLocal The data type's QName suffix,
143 * or null if $typeBase is an IRI or shorthand.
145 * @return RdfWriter $this
147 public function value( $value, $typeBase = null, $typeLocal = null );
150 * Shorthand for say( 'a' )->is( $type ).
152 * @param string $typeBase The data type's QName prefix if $typeLocal is given,
153 * or an IRI or shorthand if $typeLocal is null.
154 * @param string|null $typeLocal The data type's QName suffix,
155 * or null if $typeBase is an IRI or shorthand.
157 * @return RdfWriter $this
159 public function a( $typeBase, $typeLocal = null );
162 * Returns a document-level sub-writer.
163 * This can be used to generate parts statements out of sequence.
164 * Output generated by the sub-writer will be present in the
165 * return value of drain(), after any output generated by this
168 * @note: calling drain() on sub-writers results in undefined behavior!
169 * @note: using sub-writers after finish() has been called on this writer
170 * results in undefined behavior!
174 public function sub();
177 * Returns the MIME type of the RDF serialization the writer produces.
179 * @return string a MIME type
181 public function getMimeType();