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 RuntimeException;
29
39 protected 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' => MediaWikiPluralValidator::class,
52 'MediaWikiTimeList' => MediaWikiTimeListValidator::class,
53 'Newline' => NewlineValidator::class,
54 'NotEmpty' => NotEmptyValidator::class,
55 'NumericalParameter' => NumericalParameterValidator::class,
56 'Printf' => PrintfValidator::class,
57 'PythonInterpolation' => PythonInterpolationValidator::class,
58 'Replacement' => ReplacementValidator::class,
59 'SmartFormatPlural' => SmartFormatPluralValidator::class,
60 'UnicodePlural' => UnicodePluralValidator::class,
61 // BC: remove when unused
62 'WikiLink' => MediaWikiLinkValidator::class,
63 // BC: remove when unused
64 'WikiParameter' => MediaWikiParameterValidator::class,
65 ];
66
75 public static function get( $id, $params = null ) {
76 if ( !isset( self::$validators[ $id ] ) ) {
77 throw new InvalidArgumentException( "Could not find validator with id - '$id'. " );
78 }
79
80 return self::loadInstance( self::$validators[ $id ], $params );
81 }
82
91 public static function loadInstance( $class, $params = null ): MessageValidator {
92 if ( !class_exists( $class ) ) {
93 throw new InvalidArgumentException( "Could not find validator class - '$class'. " );
94 }
95
96 return new $class( $params );
97 }
98
105 public static function set( $id, $validator, $ns = '\\' ) {
106 if ( !class_exists( $ns . $validator ) ) {
107 throw new RuntimeException( 'Could not find validator class - ' . $ns . $validator );
108 }
109
110 self::$validators[ $id ] = $ns . $validator;
111 }
112}
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.