blob: 63d1291577265e2e1f416d134159646037ea2ef4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extensions\Translate\MessageValidator\Validators;
use InvalidArgumentException;
use MediaWiki\Extensions\Translate\Validation\MessageValidator;
use MediaWiki\Extensions\Translate\Validation\ValidationIssue;
use MediaWiki\Extensions\Translate\Validation\ValidationIssues;
use TMessage;
/**
* Ensures that the translation for a message matches a value from a list.
* @license GPL-2.0-or-later
* @since 2019.12
*/
class MatchSetValidator implements MessageValidator {
/** @var string[] */
protected $possibleValues;
/** @var string[] */
protected $normalizedValues;
/** @var bool */
protected $caseSensitive;
public function __construct( array $params ) {
$this->possibleValues = $params['values'] ?? [];
$this->caseSensitive = (bool)( $params['caseSensitive'] ?? true );
if ( $this->possibleValues === [] ) {
throw new InvalidArgumentException( 'No values provided for MatchSet validator.' );
}
if ( $this->caseSensitive ) {
$this->normalizedValues = $this->possibleValues;
} else {
$this->normalizedValues = array_map( 'strtolower', $this->possibleValues );
}
}
public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues {
$issues = new ValidationIssues();
$translation = $message->translation();
if ( $this->caseSensitive ) {
$translation = strtolower( $translation );
}
if ( array_search( $translation, $this->normalizedValues, true ) === false ) {
$issue = new ValidationIssue(
'value-not-present',
'invalid',
'translate-checks-value-not-present',
[
[ 'PLAIN-PARAMS', $this->possibleValues ],
[ 'COUNT', count( $this->possibleValues ) ]
]
);
$issues->add( $issue );
}
return $issues;
}
}
|