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 'Gettext' => GettextFormat::class,
30 'Ini' => IniFormat::class,
31 'Java' => JavaFormat::class,
32 'Json' => JsonFormat::class,
33 'Yaml' => YamlFormat::class
34 ];
35 private ObjectFactory $objectFactory;
36
37 public function __construct( ObjectFactory $objectFactory ) {
38 $this->objectFactory = $objectFactory;
39 }
40
48 public function create( string $format, FileBasedMessageGroup $group ): SimpleFormat {
49 if ( !isset( self::FORMATS[ $format ] ) ) {
50 throw new InvalidArgumentException(
51 "FileFormatSupport: Unknown file format '$format' specified for group '{$group->getId()}'"
52 );
53 }
54
55 $spec = self::FORMATS[ $format ];
56 if ( is_string( $spec ) ) {
57 $spec = [ 'class' => $spec ];
58 }
59
60 // Pass the given params as one item, instead of expanding
61 $spec['args'][] = $group;
62
63 // Phan seems to misunderstand the param type as callable instead of an array
64 // @phan-suppress-next-line PhanTypeInvalidCallableArrayKey
65 return $this->objectFactory->createObject( $spec );
66 }
67
68 public function loadInstance( string $class, FileBasedMessageGroup $group ): SimpleFormat {
69 if ( !class_exists( $class ) ) {
70 throw new InvalidArgumentException(
71 "Could not find FileFormat class '$class' specified for group '{$group->getId()}'."
72 );
73 }
74
75 return new $class( $group );
76 }
77
78 public function getClassname( string $format ): string {
79 if ( !isset( self::FORMATS[ $format ] ) ) {
80 throw new InvalidArgumentException( "Unknown format $format" );
81 }
82
83 return self::FORMATS[ $format ];
84 }
85}
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...