Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
56.41% |
22 / 39 |
|
90.91% |
10 / 11 |
CRAP | |
0.00% |
0 / 1 |
ValidationResult | |
56.41% |
22 / 39 |
|
90.91% |
10 / 11 |
48.90 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasIssues | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
getIssues | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
hasWarnings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasErrors | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getWarnings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getErrors | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDescriptiveWarnings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDescriptiveErrors | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
expandMessages | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
fixMessageParams | |
19.05% |
4 / 21 |
|
0.00% |
0 / 1 |
41.95 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\Validation; |
5 | |
6 | use IContextSource; |
7 | use InvalidArgumentException; |
8 | |
9 | /** |
10 | * Container for validation issues returned by MessageValidator. |
11 | * |
12 | * @author Abijeet Patro |
13 | * @author Niklas Laxström |
14 | * @license GPL-2.0-or-later |
15 | * @since 2020.06 (originally 2019.06) |
16 | */ |
17 | class ValidationResult { |
18 | /** @var ValidationIssues */ |
19 | protected $errors; |
20 | /** @var ValidationIssues */ |
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 | } |