Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
55.56% |
10 / 18 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
FileFormatFactory | |
55.56% |
10 / 18 |
|
50.00% |
2 / 4 |
13.62 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
loadInstance | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getClassname | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\FileFormatSupport; |
5 | |
6 | use FileBasedMessageGroup; |
7 | use InvalidArgumentException; |
8 | use Wikimedia\ObjectFactory\ObjectFactory; |
9 | |
10 | /** |
11 | * A factory class used to instantiate instances of pre-provided File formats |
12 | * |
13 | * @author Abijeet Patro |
14 | * @author Eugene Wang'ombe |
15 | * @license GPL-2.0-or-later |
16 | * @since 2023.05 |
17 | */ |
18 | class FileFormatFactory { |
19 | /** |
20 | * @var (string|array)[] |
21 | * @phpcs-require-sorted-array |
22 | */ |
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 | |
41 | /** |
42 | * Returns a FileFormat class instance based on the $format passed |
43 | * |
44 | * @param string $format FileFormat identifier |
45 | * @param FileBasedMessageGroup $group |
46 | * @return SimpleFormat |
47 | */ |
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 | } |