| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Purtle; |
| 4 |
|
| 5 |
/** |
| 6 |
* RdfWriter implementation for generating Turtle output. |
| 7 |
* |
| 8 |
* @license GPL-2.0-or-later |
| 9 |
* @author Daniel Kinzler |
| 10 |
*/ |
| 11 |
class TurtleRdfWriter extends N3RdfWriterBase { |
| 12 |
|
| 13 |
/** |
| 14 |
* @var bool |
| 15 |
*/ |
| 16 |
private $trustIRIs = true; |
| 17 |
|
| 18 |
/** |
| 19 |
* @return bool |
| 20 |
*/ |
| 21 |
public function getTrustIRIs() { |
| 22 |
return $this->trustIRIs; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @param bool $trustIRIs |
| 27 |
*/ |
| 28 |
public function setTrustIRIs( $trustIRIs ) { |
| 29 |
$this->trustIRIs = $trustIRIs; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @param string $role |
| 34 |
* @param BNodeLabeler|null $labeler |
| 35 |
* @param N3Quoter|null $quoter |
| 36 |
*/ |
| 37 |
public function __construct( |
| 38 |
$role = parent::DOCUMENT_ROLE, |
| 39 |
?BNodeLabeler $labeler = null, |
| 40 |
?N3Quoter $quoter = null |
| 41 |
) { |
| 42 |
parent::__construct( $role, $labeler, $quoter ); |
| 43 |
$this->transitionTable[self::STATE_OBJECT] = [ |
| 44 |
self::STATE_DOCUMENT => " .\n", |
| 45 |
self::STATE_SUBJECT => " .\n\n", |
| 46 |
self::STATE_PREDICATE => " ;\n\t", |
| 47 |
self::STATE_OBJECT => ",\n\t\t", |
| 48 |
]; |
| 49 |
$this->transitionTable[self::STATE_DOCUMENT][self::STATE_SUBJECT] = "\n"; |
| 50 |
$this->transitionTable[self::STATE_SUBJECT][self::STATE_PREDICATE] = ' '; |
| 51 |
$this->transitionTable[self::STATE_PREDICATE][self::STATE_OBJECT] = ' '; |
| 52 |
$this->transitionTable[self::STATE_START][self::STATE_DOCUMENT] = function () { |
| 53 |
$this->beginDocument(); |
| 54 |
}; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Write prefixes |
| 59 |
*/ |
| 60 |
private function beginDocument() { |
| 61 |
foreach ( $this->getPrefixes() as $prefix => $uri ) { |
| 62 |
$this->write( "@prefix $prefix: <" . $this->quoter->escapeIRI( $uri ) . "> .\n" ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @inheritDoc |
| 68 |
*/ |
| 69 |
protected function writeSubject( $base, $local = null ) { |
| 70 |
if ( $local !== null ) { |
| 71 |
$this->write( "$base:$local" ); |
| 72 |
} else { |
| 73 |
$this->writeIRI( $base, $this->trustIRIs ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @inheritDoc |
| 79 |
*/ |
| 80 |
protected function writePredicate( $base, $local = null ) { |
| 81 |
if ( $base === 'a' ) { |
| 82 |
$this->write( 'a' ); |
| 83 |
return; |
| 84 |
} |
| 85 |
if ( $local !== null ) { |
| 86 |
$this->write( "$base:$local" ); |
| 87 |
} else { |
| 88 |
$this->writeIRI( $base, $this->trustIRIs ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @inheritDoc |
| 94 |
*/ |
| 95 |
protected function writeResource( $base, $local = null ) { |
| 96 |
if ( $local !== null ) { |
| 97 |
$this->write( "$base:$local" ); |
| 98 |
} else { |
| 99 |
$this->writeIRI( $base ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @param string $role |
| 105 |
* @param BNodeLabeler $labeler |
| 106 |
* |
| 107 |
* @return RdfWriterBase |
| 108 |
*/ |
| 109 |
protected function newSubWriter( $role, BNodeLabeler $labeler ) { |
| 110 |
$writer = new self( $role, $labeler, $this->quoter ); |
| 111 |
|
| 112 |
return $writer; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @return string a MIME type |
| 117 |
*/ |
| 118 |
public function getMimeType() { |
| 119 |
return 'text/turtle; charset=UTF-8'; |
| 120 |
} |
| 121 |
|
| 122 |
} |