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 private array $possibleValues;
21 private array $normalizedValues;
22 private bool $caseSensitive;
23
24 public function __construct( array $params ) {
25 $this->possibleValues = $params['values'] ?? [];
26 $this->caseSensitive = (bool)( $params['caseSensitive'] ?? true );
27
28 if ( $this->possibleValues === [] ) {
29 throw new InvalidArgumentException( 'No values provided for MatchSet validator.' );
30 }
31
32 if ( $this->caseSensitive ) {
33 $this->normalizedValues = $this->possibleValues;
34 } else {
35 $this->normalizedValues = array_map( 'strtolower', $this->possibleValues );
36 }
37 }
38
39 public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
40 $issues = new ValidationIssues();
41
42 $translation = $message->translation();
43 if ( $this->caseSensitive ) {
44 $translation = strtolower( $translation );
45 }
46
47 if ( !in_array( $translation, $this->normalizedValues, true ) ) {
48 $issue = new ValidationIssue(
49 'value-not-present',
50 'invalid',
51 'translate-checks-value-not-present',
52 [
53 [ 'PLAIN-PARAMS', $this->possibleValues ],
54 [ 'COUNT', count( $this->possibleValues ) ]
55 ]
56 );
57
58 $issues->add( $issue );
59 }
60
61 return $issues;
62 }
63}
Interface for message objects used by MessageCollection.
Definition Message.php:13
Ensures that the translation for a message matches a value from a list.