Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 11 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
ValidatorFactory | |
0.00% |
0 / 11 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
get | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
loadInstance | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\Validation; |
5 | |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Extension\Translate\Validation\Validators\BraceBalanceValidator; |
8 | use MediaWiki\Extension\Translate\Validation\Validators\EscapeCharacterValidator; |
9 | use MediaWiki\Extension\Translate\Validation\Validators\GettextNewlineValidator; |
10 | use MediaWiki\Extension\Translate\Validation\Validators\GettextPluralValidator; |
11 | use MediaWiki\Extension\Translate\Validation\Validators\InsertableRegexValidator; |
12 | use MediaWiki\Extension\Translate\Validation\Validators\InsertableRubyVariableValidator; |
13 | use MediaWiki\Extension\Translate\Validation\Validators\IosVariableValidator; |
14 | use MediaWiki\Extension\Translate\Validation\Validators\MatchSetValidator; |
15 | use MediaWiki\Extension\Translate\Validation\Validators\MediaWikiLinkValidator; |
16 | use MediaWiki\Extension\Translate\Validation\Validators\MediaWikiPageNameValidator; |
17 | use MediaWiki\Extension\Translate\Validation\Validators\MediaWikiParameterValidator; |
18 | use MediaWiki\Extension\Translate\Validation\Validators\MediaWikiPluralValidator; |
19 | use MediaWiki\Extension\Translate\Validation\Validators\MediaWikiTimeListValidator; |
20 | use MediaWiki\Extension\Translate\Validation\Validators\NewlineValidator; |
21 | use MediaWiki\Extension\Translate\Validation\Validators\NotEmptyValidator; |
22 | use MediaWiki\Extension\Translate\Validation\Validators\NumericalParameterValidator; |
23 | use MediaWiki\Extension\Translate\Validation\Validators\PrintfValidator; |
24 | use MediaWiki\Extension\Translate\Validation\Validators\PythonInterpolationValidator; |
25 | use MediaWiki\Extension\Translate\Validation\Validators\ReplacementValidator; |
26 | use MediaWiki\Extension\Translate\Validation\Validators\SmartFormatPluralValidator; |
27 | use MediaWiki\Extension\Translate\Validation\Validators\UnicodePluralValidator; |
28 | use MediaWiki\MediaWikiServices; |
29 | |
30 | /** |
31 | * A factory class used to instantiate instances of pre-provided Validators |
32 | * |
33 | * @author Abijeet Patro |
34 | * @license GPL-2.0-or-later |
35 | * @since 2019.06 |
36 | */ |
37 | class ValidatorFactory { |
38 | /** @var string[] */ |
39 | private static $validators = [ |
40 | 'BraceBalance' => BraceBalanceValidator::class, |
41 | 'EscapeCharacter' => EscapeCharacterValidator::class, |
42 | 'GettextNewline' => GettextNewlineValidator::class, |
43 | 'GettextPlural' => GettextPluralValidator::class, |
44 | 'InsertableRegex' => InsertableRegexValidator::class, |
45 | 'InsertableRubyVariable' => InsertableRubyVariableValidator::class, |
46 | 'IosVariable' => IosVariableValidator::class, |
47 | 'MatchSet' => MatchSetValidator::class, |
48 | 'MediaWikiLink' => MediaWikiLinkValidator::class, |
49 | 'MediaWikiPageName' => MediaWikiPageNameValidator::class, |
50 | 'MediaWikiParameter' => MediaWikiParameterValidator::class, |
51 | 'MediaWikiPlural' => [ |
52 | 'class' => MediaWikiPluralValidator::class, |
53 | 'services' => [ |
54 | 'LanguageFactory', |
55 | 'ParserFactory', |
56 | 'UserFactory' |
57 | ] |
58 | ], |
59 | 'MediaWikiTimeList' => MediaWikiTimeListValidator::class, |
60 | 'Newline' => NewlineValidator::class, |
61 | 'NotEmpty' => NotEmptyValidator::class, |
62 | 'NumericalParameter' => NumericalParameterValidator::class, |
63 | 'Printf' => PrintfValidator::class, |
64 | 'PythonInterpolation' => PythonInterpolationValidator::class, |
65 | 'Replacement' => ReplacementValidator::class, |
66 | 'SmartFormatPlural' => SmartFormatPluralValidator::class, |
67 | 'UnicodePlural' => UnicodePluralValidator::class, |
68 | ]; |
69 | |
70 | /** |
71 | * Returns a validator instance based on the id specified |
72 | * |
73 | * @param string $id Id of the pre-defined validator class |
74 | * @param mixed|null $params |
75 | * @throws InvalidArgumentException |
76 | * @return MessageValidator |
77 | */ |
78 | public static function get( $id, $params = null ) { |
79 | if ( !isset( self::$validators[ $id ] ) ) { |
80 | throw new InvalidArgumentException( "Could not find validator with id - '$id'. " ); |
81 | } |
82 | |
83 | $spec = self::$validators[ $id ]; |
84 | if ( is_string( $spec ) ) { |
85 | $spec = [ 'class' => $spec ]; |
86 | } |
87 | |
88 | if ( $params ) { |
89 | // Pass the given params as one item, instead of expanding |
90 | $spec['args'] = [ $params ]; |
91 | } |
92 | |
93 | // Phan seems to misunderstand the param type as callable instead of an array |
94 | // @phan-suppress-next-line PhanTypeInvalidCallableArraySize |
95 | return MediaWikiServices::getInstance()->getObjectFactory()->createObject( $spec ); |
96 | } |
97 | |
98 | /** |
99 | * Takes a Validator class name, and returns an instance of that class. |
100 | * |
101 | * @param string $class Custom validator class name |
102 | * @param mixed|null $params |
103 | * @throws InvalidArgumentException |
104 | * @return MessageValidator |
105 | */ |
106 | public static function loadInstance( $class, $params = null ): MessageValidator { |
107 | if ( !class_exists( $class ) ) { |
108 | throw new InvalidArgumentException( "Could not find validator class - '$class'. " ); |
109 | } |
110 | |
111 | return new $class( $params ); |
112 | } |
113 | } |