Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupFactory.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupConfiguration;
5
6use InvalidArgumentException;
9
26 public function __construct(
27 private readonly MessageGroupTypeRegistry $typeRegistry
28 ) {
29 }
30
42 public function createGroup( array $conf ): MessageGroup {
43 $class = $this->resolveClass( $conf );
44 return $this->instantiate( $class, $conf );
45 }
46
57 public function resolveClass( array $conf ): string {
58 $basic = $conf['BASIC'] ?? [];
59 $hasType = isset( $basic['type'] );
60 $hasClass = isset( $basic['class'] );
61
62 if ( $hasType && $hasClass ) {
64 "Message group configuration must not specify both 'type' and 'class' " .
65 "(group id: '{$basic['id']}')"
66 );
67 }
68
69 if ( !$hasType && !$hasClass ) {
71 "Message group configuration must specify either 'type' or 'class' " .
72 "(group id: '{$basic['id']}')"
73 );
74 }
75
76 if ( $hasType ) {
77 return $this->typeRegistry->getClass( $basic['type'] );
78 }
79
80 return $basic['class'];
81 }
82
91 private function instantiate( string $class, array $conf ): MessageGroup {
92 if ( !class_exists( $class ) ) {
93 throw new InvalidArgumentException(
94 "Message group implementation class '$class' does not exist or cannot be autoloaded"
95 );
96 }
97
98 if ( !is_subclass_of( $class, MessageGroupBase::class ) ) {
99 throw new InvalidArgumentException(
100 "Message group implementation class '$class' must extend MessageGroupBase"
101 );
102 }
103
104 return MessageGroupBase::newFromConf( $class, $conf );
105 }
106}
Canonical factory for constructing YAML-configured message groups.
resolveClass(array $conf)
Resolve the implementation class from the configuration selector.
createGroup(array $conf)
Construct and initialise a MessageGroupBase instance from a parsed group configuration array.
This class implements some basic functions that wrap around the YAML message group configurations.