blob: 173da0ecf0f627daf262982c343970e924b12fb4 (
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extensions\Translate\MessageValidator;
use IContextSource;
use InvalidArgumentException;
use MediaWiki\Extensions\Translate\Validation\ValidationIssue;
use MediaWiki\Extensions\Translate\Validation\ValidationIssues;
/**
* Container for validation issues returned by MessageValidator.
*
* @author Abijeet Patro
* @author Niklas Laxström
* @license GPL-2.0-or-later
* @since 2020.06 (originally 2019.06)
*/
class ValidationResult {
/** @var ValidationIssues */
protected $errors;
/** @var ValidationIssues */
protected $warnings;
public function __construct( ValidationIssues $errors, ValidationIssues $warnings ) {
$this->errors = $errors;
$this->warnings = $warnings;
}
public function hasIssues(): bool {
return $this->hasWarnings() || $this->hasErrors();
}
public function getIssues(): ValidationIssues {
$issues = new ValidationIssues();
$issues->merge( $this->errors );
$issues->merge( $this->warnings );
return $issues;
}
public function hasWarnings(): bool {
return $this->warnings->hasIssues();
}
public function hasErrors(): bool {
return $this->errors->hasIssues();
}
public function getWarnings(): ValidationIssues {
return $this->warnings;
}
public function getErrors(): ValidationIssues {
return $this->errors;
}
public function getDescriptiveWarnings( IContextSource $context ): array {
return $this->expandMessages( $context, $this->warnings );
}
public function getDescriptiveErrors( IContextSource $context ): array {
return $this->expandMessages( $context, $this->errors );
}
private function expandMessages( IContextSource $context, ValidationIssues $issues ): array {
$expandMessage = function ( ValidationIssue $issue ) use ( $context ): string {
$params = $this->fixMessageParams( $context, $issue->messageParams() );
return $context->msg( $issue->messageKey() )->params( $params )->parse();
};
return array_map( $expandMessage, iterator_to_array( $issues ) );
}
private function fixMessageParams( IContextSource $context, array $params ): array {
$out = [];
$lang = $context->getLanguage();
foreach ( $params as $param ) {
if ( !is_array( $param ) ) {
$out[] = $param;
} else {
[ $type, $value ] = $param;
if ( $type === 'COUNT' ) {
$out[] = $lang->formatNum( $value );
} elseif ( $type === 'PARAMS' ) {
$out[] = $lang->commaList( $value );
} elseif ( $type === 'PLAIN-PARAMS' ) {
$value = array_map( 'wfEscapeWikiText', $value );
$out[] = $lang->commaList( $value );
} elseif ( $type === 'PLAIN' ) {
$out[] = wfEscapeWikiText( $value );
} elseif ( $type === 'MESSAGE' ) {
$messageKey = array_shift( $value );
$messageParams = $this->fixMessageParams( $context, $value );
$out[] = $context->msg( $messageKey )->params( $messageParams );
} else {
throw new InvalidArgumentException( "Unknown type $type" );
}
}
}
return $out;
}
}
|