Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ValidatorFactory.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation;
5
6use InvalidArgumentException;
28use MediaWiki\MediaWikiServices;
29
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
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
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}
A factory class used to instantiate instances of pre-provided Validators.
static loadInstance( $class, $params=null)
Takes a Validator class name, and returns an instance of that class.
Ensures that only the specified escape characters are present.
Ensures that the translation has the same number of newlines as the source message at the beginning a...
A generic regex validator and insertable that can be reused by other classes.
An insertable Ruby variable validator that also acts as an InsertableSuggester.
Ensures that the translation for a message matches a value from a list.
Checks if the translation uses links that are discouraged.
An insertable wiki parameter validator that also acts as an InsertableSuggester @license GPL-2....
Handles plural validation for MediaWiki inline plural syntax.
Ensures that the translation has the same number of newlines as the source message at the beginning o...
An insertable numerical parameter validator that also acts as an InsertableSuggester @license GPL-2....
A validator that checks for missing and unknown printf formatting characters in translations.
An insertable python interpolation validator that also acts as an InsertableSuggester.
This is a very strict validator class for Unicode CLDR based plural markup.