Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.77% |
30 / 31 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
ErrorReporter | |
96.77% |
30 / 31 |
|
66.67% |
2 / 3 |
8 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getHtml | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
4.00 | |||
getJSONValidatorLog | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Kartographer\Tag; |
4 | |
5 | use InvalidArgumentException; |
6 | use MediaWiki\Html\Html; |
7 | use MediaWiki\Language\Language; |
8 | use StatusValue; |
9 | |
10 | /** |
11 | * @license MIT |
12 | */ |
13 | class ErrorReporter { |
14 | |
15 | /** @var Language|string */ |
16 | private $language; |
17 | |
18 | /** |
19 | * @param Language|string $languageCode |
20 | */ |
21 | public function __construct( $languageCode ) { |
22 | $this->language = $languageCode; |
23 | } |
24 | |
25 | /** |
26 | * @param StatusValue $status |
27 | * @param string $tag |
28 | * @return string HTML |
29 | */ |
30 | public function getHtml( StatusValue $status, string $tag ): string { |
31 | $errors = $status->getErrors(); |
32 | if ( !$errors ) { |
33 | throw new InvalidArgumentException( 'Attempt to report error when none took place' ); |
34 | } |
35 | |
36 | if ( count( $errors ) > 1 ) { |
37 | $html = ''; |
38 | foreach ( $errors as $err ) { |
39 | $html .= Html::rawElement( 'li', [], wfMessage( $err['message'], $err['params'] ) |
40 | ->inLanguage( $this->language )->parse() ) . "\n"; |
41 | } |
42 | $msg = wfMessage( 'kartographer-error-context-multi', "<$tag>" ) |
43 | ->rawParams( Html::rawElement( 'ul', [], $html ) ); |
44 | } else { |
45 | $errorText = wfMessage( $errors[0]['message'], $errors[0]['params'] ) |
46 | ->inLanguage( $this->language )->parse(); |
47 | $msg = wfMessage( 'kartographer-error-context', "<$tag>" ) |
48 | ->rawParams( $errorText ); |
49 | } |
50 | |
51 | return Html::rawElement( 'div', [ 'class' => 'mw-kartographer-error' ], |
52 | $msg->inLanguage( $this->language )->escaped() . |
53 | $this->getJSONValidatorLog( $status->getValue()['schema-errors'] ?? [] ) |
54 | ); |
55 | } |
56 | |
57 | /** |
58 | * @param array[] $errors |
59 | * @return string HTML |
60 | */ |
61 | private function getJSONValidatorLog( array $errors ): string { |
62 | if ( !$errors ) { |
63 | return ''; |
64 | } |
65 | |
66 | $log = "\n"; |
67 | /** These errors come from {@see \JsonSchema\Constraints\BaseConstraint::addError} */ |
68 | foreach ( $errors as $error ) { |
69 | $log .= Html::element( 'li', [], |
70 | $error['pointer'] . wfMessage( 'colon-separator' )->text() . $error['message'] |
71 | ) . "\n"; |
72 | } |
73 | return Html::rawElement( 'ul', [ 'class' => [ |
74 | 'mw-kartographer-error-log', |
75 | 'mw-collapsible', |
76 | 'mw-collapsed', |
77 | ] ], $log ); |
78 | } |
79 | |
80 | } |