Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MatchSetValidator.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
6use InvalidArgumentException;
11
19 protected $possibleValues;
21 protected $normalizedValues;
23 protected $caseSensitive;
24
25 public function __construct( array $params ) {
26 $this->possibleValues = $params['values'] ?? [];
27 $this->caseSensitive = (bool)( $params['caseSensitive'] ?? true );
28
29 if ( $this->possibleValues === [] ) {
30 throw new InvalidArgumentException( 'No values provided for MatchSet validator.' );
31 }
32
33 if ( $this->caseSensitive ) {
34 $this->normalizedValues = $this->possibleValues;
35 } else {
36 $this->normalizedValues = array_map( 'strtolower', $this->possibleValues );
37 }
38 }
39
40 public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
41 $issues = new ValidationIssues();
42
43 $translation = $message->translation();
44 if ( $this->caseSensitive ) {
45 $translation = strtolower( $translation );
46 }
47
48 if ( !in_array( $translation, $this->normalizedValues, true ) ) {
49 $issue = new ValidationIssue(
50 'value-not-present',
51 'invalid',
52 'translate-checks-value-not-present',
53 [
54 [ 'PLAIN-PARAMS', $this->possibleValues ],
55 [ 'COUNT', count( $this->possibleValues ) ]
56 ]
57 );
58
59 $issues->add( $issue );
60 }
61
62 return $issues;
63 }
64}
Interface for message objects used by MessageCollection.
Definition Message.php:13
Ensures that the translation for a message matches a value from a list.