Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ValidationResult.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation;
5
6use IContextSource;
7use InvalidArgumentException;
8
19 protected $errors;
21 protected $warnings;
22
23 public function __construct( ValidationIssues $errors, ValidationIssues $warnings ) {
24 $this->errors = $errors;
25 $this->warnings = $warnings;
26 }
27
28 public function hasIssues(): bool {
29 return $this->hasWarnings() || $this->hasErrors();
30 }
31
32 public function getIssues(): ValidationIssues {
33 $issues = new ValidationIssues();
34 $issues->merge( $this->errors );
35 $issues->merge( $this->warnings );
36 return $issues;
37 }
38
39 public function hasWarnings(): bool {
40 return $this->warnings->hasIssues();
41 }
42
43 public function hasErrors(): bool {
44 return $this->errors->hasIssues();
45 }
46
47 public function getWarnings(): ValidationIssues {
48 return $this->warnings;
49 }
50
51 public function getErrors(): ValidationIssues {
52 return $this->errors;
53 }
54
55 public function getDescriptiveWarnings( IContextSource $context ): array {
56 return $this->expandMessages( $context, $this->warnings );
57 }
58
59 public function getDescriptiveErrors( IContextSource $context ): array {
60 return $this->expandMessages( $context, $this->errors );
61 }
62
63 private function expandMessages( IContextSource $context, ValidationIssues $issues ): array {
64 $expandMessage = function ( ValidationIssue $issue ) use ( $context ): string {
65 $params = $this->fixMessageParams( $context, $issue->messageParams() );
66 return $context->msg( $issue->messageKey() )->params( $params )->parse();
67 };
68
69 return array_map( $expandMessage, iterator_to_array( $issues ) );
70 }
71
72 private function fixMessageParams( IContextSource $context, array $params ): array {
73 $out = [];
74 $lang = $context->getLanguage();
75
76 foreach ( $params as $param ) {
77 if ( !is_array( $param ) ) {
78 $out[] = $param;
79 } else {
80 [ $type, $value ] = $param;
81 if ( $type === 'COUNT' ) {
82 $out[] = $lang->formatNum( $value );
83 } elseif ( $type === 'PARAMS' ) {
84 $out[] = $lang->commaList( $value );
85 } elseif ( $type === 'PLAIN-PARAMS' ) {
86 $value = array_map( 'wfEscapeWikiText', $value );
87 $out[] = $lang->commaList( $value );
88 } elseif ( $type === 'PLAIN' ) {
89 $out[] = wfEscapeWikiText( $value );
90 } elseif ( $type === 'MESSAGE' ) {
91 $messageKey = array_shift( $value );
92 $messageParams = $this->fixMessageParams( $context, $value );
93 $out[] = $context->msg( $messageKey )->params( $messageParams );
94 } else {
95 throw new InvalidArgumentException( "Unknown type $type" );
96 }
97 }
98 }
99
100 return $out;
101 }
102}
Container for validation issues returned by MessageValidator.