1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Repo\Localizer; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Message; |
8
|
|
|
use ValueParsers\ParseException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Provides a Message for ParseException for localized errors. |
12
|
|
|
* |
13
|
|
|
* @license GPL-2.0-or-later |
14
|
|
|
* @author Daniel Kinzler |
15
|
|
|
* @author Katie Filbert < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class ParseExceptionLocalizer implements ExceptionLocalizer { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @see ExceptionLocalizer::getExceptionMessage() |
21
|
|
|
* |
22
|
|
|
* @param Exception $exception |
23
|
|
|
* |
24
|
|
|
* @return Message |
25
|
|
|
* @throws InvalidArgumentException |
26
|
|
|
*/ |
27
|
|
|
public function getExceptionMessage( Exception $exception ) { |
28
|
|
|
if ( !$this->hasExceptionMessage( $exception ) ) { |
29
|
|
|
throw new InvalidArgumentException( '$exception is not a ParseException' ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$msg = null; |
33
|
|
|
|
34
|
|
|
/** @var ParseException $exception */ |
35
|
|
|
'@phan-var ParseException $exception'; |
36
|
|
|
$expectedFormat = $exception->getExpectedFormat(); |
37
|
|
|
if ( $expectedFormat !== null ) { |
38
|
|
|
// Messages: |
39
|
|
|
// wikibase-parse-error-coordinate |
40
|
|
|
// wikibase-parse-error-entity-id |
41
|
|
|
// wikibase-parse-error-quantity |
42
|
|
|
// wikibase-parse-error-time |
43
|
|
|
$msg = new Message( 'wikibase-parse-error-' . $expectedFormat ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ( !( $msg instanceof Message ) || !$msg->exists() ) { |
|
|
|
|
47
|
|
|
$msg = new Message( 'wikibase-parse-error' ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $msg; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @see ExceptionLocalizer::getExceptionMessage() |
55
|
|
|
* |
56
|
|
|
* @param Exception $exception |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function hasExceptionMessage( Exception $exception ) { |
61
|
|
|
return $exception instanceof ParseException; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|