Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
InsertableRegexValidator.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
6use InvalidArgumentException;
12
21 private $validationRegex;
22
23 public function __construct( $params ) {
24 parent::__construct( $params );
25
26 if ( is_string( $params ) ) {
27 $this->validationRegex = $params;
28 } elseif ( is_array( $params ) ) {
29 $this->validationRegex = $params['regex'] ?? null;
30 }
31
32 if ( $this->validationRegex === null ) {
33 throw new InvalidArgumentException( 'The configuration for InsertableRegexValidator does not ' .
34 'specify a regular expression.' );
35 }
36 }
37
38 public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
39 $issues = new ValidationIssues();
40
41 preg_match_all( $this->validationRegex, $message->definition(), $definitionMatch );
42 preg_match_all( $this->validationRegex, $message->translation(), $translationMatch );
43 $definitionVariables = $definitionMatch[0];
44 $translationVariables = $translationMatch[0];
45
46 $missingVariables = array_diff( $definitionVariables, $translationVariables );
47 if ( $missingVariables ) {
48 $issue = new ValidationIssue(
49 'variable',
50 'missing',
51 'translate-checks-parameters',
52 [
53 [ 'PLAIN-PARAMS', $missingVariables ],
54 [ 'COUNT', count( $missingVariables ) ]
55 ]
56 );
57
58 $issues->add( $issue );
59 }
60
61 $unknownVariables = array_diff( $translationVariables, $definitionVariables );
62 if ( $unknownVariables ) {
63 $issue = new ValidationIssue(
64 'variable',
65 'unknown',
66 'translate-checks-parameters-unknown',
67 [
68 [ 'PLAIN-PARAMS', $unknownVariables ],
69 [ 'COUNT', count( $unknownVariables ) ]
70 ]
71 );
72
73 $issues->add( $issue );
74 }
75
76 return $issues;
77 }
78}
Interface for message objects used by MessageCollection.
Definition Message.php:13
Regex InsertablesSuggester implementation that can be extended or used for insertables in message gro...
A generic regex validator and insertable that can be reused by other classes.