Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
FileFormatFactory.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\FileFormatSupport;
5
7use InvalidArgumentException;
8use Wikimedia\ObjectFactory\ObjectFactory;
9
23 private const FORMATS = [
24 'Amd' => AmdFormat::class,
25 'AndroidXml' => AndroidXmlFormat::class,
26 'Apple' => AppleFormat::class,
27 'Dtd' => DtdFormat::class,
28 'FlatPhp' => FlatPhpFormat::class,
29 'Fluent' => FluentFormat::class,
30 'Gettext' => GettextFormat::class,
31 'Ini' => IniFormat::class,
32 'Java' => JavaFormat::class,
33 'Json' => JsonFormat::class,
34 'Yaml' => YamlFormat::class
35 ];
36
37 public function __construct(
38 private readonly ObjectFactory $objectFactory,
39 ) {
40 }
41
49 public function create( string $format, FileBasedMessageGroup $group ): SimpleFormat {
50 if ( !isset( self::FORMATS[$format] ) ) {
51 throw new InvalidArgumentException(
52 "FileFormatSupport: Unknown file format '$format' specified for group '{$group->getId()}'"
53 );
54 }
55
56 $spec = self::FORMATS[$format];
57 if ( is_string( $spec ) ) {
58 $spec = [ 'class' => $spec ];
59 }
60
61 // Pass the given params as one item, instead of expanding
62 $spec['args'][] = $group;
63
64 return $this->objectFactory->createObject( $spec );
65 }
66
67 public function loadInstance( string $class, FileBasedMessageGroup $group ): SimpleFormat {
68 if ( !class_exists( $class ) ) {
69 throw new InvalidArgumentException(
70 "Could not find FileFormat class '$class' specified for group '{$group->getId()}'."
71 );
72 }
73
74 return new $class( $group );
75 }
76
77 public function getClassname( string $format ): string {
78 if ( !isset( self::FORMATS[$format] ) ) {
79 throw new InvalidArgumentException( "Unknown format $format" );
80 }
81
82 return self::FORMATS[$format];
83 }
84}
This class implements default behavior for file based message groups.
A factory class used to instantiate instances of pre-provided File formats.
create(string $format, FileBasedMessageGroup $group)
Returns a FileFormat class instance based on the $format passed.
A very basic FileFormatSupport module that implements some basic functionality and a simple binary ba...