| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Purtle; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
/** |
| 8 |
* @since 0.5 |
| 9 |
* |
| 10 |
* @license GPL-2.0-or-later |
| 11 |
* @author Daniel Kinzler |
| 12 |
*/ |
| 13 |
class RdfWriterFactory { |
| 14 |
|
| 15 |
/** |
| 16 |
* Returns a list of canonical format names. |
| 17 |
* These names for internal use with getMimeTypes() and getFileExtension(), |
| 18 |
* they are not themselves MIME types or file extensions. |
| 19 |
* |
| 20 |
* @return string[] |
| 21 |
*/ |
| 22 |
public function getSupportedFormats() { |
| 23 |
return [ 'n3', 'turtle', 'ntriples', 'rdfxml', 'jsonld' ]; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Returns a list of mime types that correspond to the format. |
| 28 |
* |
| 29 |
* @param string $format a format name, as returned by getSupportedFormats() or getFormatName(). |
| 30 |
* |
| 31 |
* @throws InvalidArgumentException if $format is not a cononical format name |
| 32 |
* @return string[] |
| 33 |
*/ |
| 34 |
public function getMimeTypes( $format ) { |
| 35 |
// NOTE: Maintaining mime types and file extensions in the RdfWriter implementations |
| 36 |
// is tempting, but means we have to load all these classes to find the right |
| 37 |
// one for a requested name. Better avoid that overhead when serving lots of |
| 38 |
// HTTP requests. |
| 39 |
|
| 40 |
switch ( strtolower( $format ) ) { |
| 41 |
case 'n3': |
| 42 |
return [ 'text/n3', 'text/rdf+n3' ]; |
| 43 |
|
| 44 |
case 'turtle': |
| 45 |
return [ 'text/turtle', 'application/x-turtle' ]; |
| 46 |
|
| 47 |
case 'ntriples': |
| 48 |
return [ 'application/n-triples', 'text/n-triples', 'text/plain' ]; |
| 49 |
|
| 50 |
case 'rdfxml': |
| 51 |
return [ 'application/rdf+xml', 'application/xml', 'text/xml' ]; |
| 52 |
|
| 53 |
case 'jsonld': |
| 54 |
return [ 'application/ld+json', 'application/json' ]; |
| 55 |
|
| 56 |
default: |
| 57 |
throw new InvalidArgumentException( 'Bad format: ' . $format ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns a file extension that correspond to the format. |
| 63 |
* |
| 64 |
* @param string $format a format name, as returned by getSupportedFormats() or getFormatName(). |
| 65 |
* |
| 66 |
* @throws InvalidArgumentException if $format is not a cononical format name |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
public function getFileExtension( $format ) { |
| 70 |
switch ( strtolower( $format ) ) { |
| 71 |
case 'n3': |
| 72 |
return 'n3'; |
| 73 |
|
| 74 |
case 'turtle': |
| 75 |
return 'ttl'; |
| 76 |
|
| 77 |
case 'ntriples': |
| 78 |
return 'nt'; |
| 79 |
|
| 80 |
case 'rdfxml': |
| 81 |
return 'rdf'; |
| 82 |
|
| 83 |
case 'jsonld': |
| 84 |
return 'jsonld'; |
| 85 |
|
| 86 |
default: |
| 87 |
throw new InvalidArgumentException( 'Bad format: ' . $format ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns an RdfWriter for the given format name. |
| 93 |
* |
| 94 |
* @param string $format a format name, as returned by getSupportedFormats() or getFormatName(). |
| 95 |
* @param BNodeLabeler|null $labeler Optional labeler |
| 96 |
* |
| 97 |
* @throws InvalidArgumentException if $format is not a canonical format name |
| 98 |
* @return RdfWriter the format object, or null if not found. |
| 99 |
*/ |
| 100 |
public function getWriter( $format, ?BNodeLabeler $labeler = null ) { |
| 101 |
switch ( strtolower( $format ) ) { |
| 102 |
case 'n3': |
| 103 |
// falls through to turtle |
| 104 |
|
| 105 |
case 'turtle': |
| 106 |
return new TurtleRdfWriter( RdfWriterBase::DOCUMENT_ROLE, $labeler ); |
| 107 |
|
| 108 |
case 'ntriples': |
| 109 |
return new NTriplesRdfWriter( RdfWriterBase::DOCUMENT_ROLE, $labeler ); |
| 110 |
|
| 111 |
case 'rdfxml': |
| 112 |
return new XmlRdfWriter( RdfWriterBase::DOCUMENT_ROLE, $labeler ); |
| 113 |
|
| 114 |
case 'jsonld': |
| 115 |
return new JsonLdRdfWriter( RdfWriterBase::DOCUMENT_ROLE, $labeler ); |
| 116 |
|
| 117 |
default: |
| 118 |
throw new InvalidArgumentException( 'Bad format: ' . $format ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the canonical format name for $format. $format may be a file extension, |
| 124 |
* a mime type, or a common or canonical name of the format. |
| 125 |
* |
| 126 |
* If no format is found for $format, this method returns false. |
| 127 |
* |
| 128 |
* @param string $format the name (file extension, mime type) of the desired format. |
| 129 |
* |
| 130 |
* @return string|false the canonical format name |
| 131 |
*/ |
| 132 |
public function getFormatName( $format ) { |
| 133 |
switch ( strtolower( $format ) ) { |
| 134 |
case 'n3': |
| 135 |
case 'text/n3': |
| 136 |
case 'text/rdf+n3': |
| 137 |
return 'n3'; |
| 138 |
|
| 139 |
case 'ttl': |
| 140 |
case 'turtle': |
| 141 |
case 'text/turtle': |
| 142 |
case 'application/x-turtle': |
| 143 |
return 'turtle'; |
| 144 |
|
| 145 |
case 'nt': |
| 146 |
case 'ntriples': |
| 147 |
case 'n-triples': |
| 148 |
case 'text/plain': |
| 149 |
case 'text/n-triples': |
| 150 |
case 'application/ntriples': |
| 151 |
case 'application/n-triples': |
| 152 |
return 'ntriples'; |
| 153 |
|
| 154 |
case 'xml': |
| 155 |
case 'rdf': |
| 156 |
case 'rdfxml': |
| 157 |
case 'application/rdf+xml': |
| 158 |
case 'application/xml': |
| 159 |
case 'text/xml': |
| 160 |
return 'rdfxml'; |
| 161 |
|
| 162 |
case 'json': |
| 163 |
case 'jsonld': |
| 164 |
case 'application/ld+json': |
| 165 |
case 'application/json': |
| 166 |
return 'jsonld'; |
| 167 |
|
| 168 |
default: |
| 169 |
return false; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
} |