]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/wikimedia/purtle/src/RdfWriterFactory.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / wikimedia / purtle / src / RdfWriterFactory.php
1 <?php
2
3 namespace Wikimedia\Purtle;
4
5 use InvalidArgumentException;
6
7 /**
8  * @since 0.5
9  *
10  * @license GPL-2.0+
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' ];
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                         default:
54                                 throw new InvalidArgumentException( 'Bad format: ' . $format );
55                 }
56         }
57
58         /**
59          * Returns a file extension that correspond to the format.
60          *
61          * @param string $format a format name, as returned by getSupportedFormats() or getFormatName().
62          *
63          * @throws InvalidArgumentException if $format is not a cononical format name
64          * @return string
65          */
66         public function getFileExtension( $format ) {
67                 switch ( strtolower( $format ) ) {
68                         case 'n3':
69                                 return 'n3';
70
71                         case 'turtle':
72                                 return 'ttl';
73
74                         case 'ntriples':
75                                 return 'nt';
76
77                         case 'rdfxml':
78                                 return 'rdf';
79
80                         default:
81                                 throw new InvalidArgumentException( 'Bad format: ' . $format );
82                 }
83         }
84
85         /**
86          * Returns an RdfWriter for the given format name.
87          *
88          * @param string $format a format name, as returned by getSupportedFormats() or getFormatName().
89          *
90          * @throws InvalidArgumentException if $format is not a cononical format name
91          * @return RdfWriter the format object, or null if not found.
92          */
93         public function getWriter( $format ) {
94                 switch ( strtolower( $format ) ) {
95                         case 'n3':
96                                 // falls through to turtle
97
98                         case 'turtle':
99                                 return new TurtleRdfWriter();
100
101                         case 'ntriples':
102                                 return new NTriplesRdfWriter();
103
104                         case 'rdfxml':
105                                 return new XmlRdfWriter();
106
107                         default:
108                                 throw new InvalidArgumentException( 'Bad format: ' . $format );
109                 }
110         }
111
112         /**
113          * Returns the canonical format name for $format. $format may be a file extension,
114          * a mime type, or a common or canonical name of the format.
115          *
116          * If no format is found for $format, this method returns false.
117          *
118          * @param string $format the name (file extension, mime type) of the desired format.
119          *
120          * @return string|false the canonical format name
121          */
122         public function getFormatName( $format ) {
123                 switch ( strtolower( $format ) ) {
124                         case 'n3':
125                         case 'text/n3':
126                         case 'text/rdf+n3':
127                                 return 'n3';
128
129                         case 'ttl':
130                         case 'turtle':
131                         case 'text/turtle':
132                         case 'application/x-turtle':
133                                 return 'turtle';
134
135                         case 'nt':
136                         case 'ntriples':
137                         case 'n-triples':
138                         case 'text/plain':
139                         case 'text/n-triples':
140                         case 'application/ntriples':
141                         case 'application/n-triples':
142                                 return 'ntriples';
143
144                         case 'xml':
145                         case 'rdf':
146                         case 'rdfxml':
147                         case 'application/rdf+xml':
148                         case 'application/xml':
149                         case 'text/xml':
150                                 return 'rdfxml';
151
152                         default:
153                                 return false;
154                 }
155         }
156
157 }