CalendarModelParser::getCalendarModelUriFromKey()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.1111
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6
1
<?php
2
3
namespace ValueParsers;
4
5
use DataValues\TimeValue;
6
7
/**
8
 * ValueParser that parses the string representation of a calendar model.
9
 *
10
 * @since 0.2
11
 *
12
 * @license GPL-2.0+
13
 * @author Addshore
14
 * @author Thiemo Kreuz
15
 */
16
class CalendarModelParser extends StringValueParser {
17
18
	const FORMAT_NAME = 'calendar-model';
19
20
	/**
21
	 * Option to provide localized calendar model names for unlocalization. Must be an array mapping
22
	 * localized calendar model names to URIs.
23
	 *
24
	 * @see TimeFormatter::OPT_CALENDARNAMES
25
	 */
26
	const OPT_CALENDAR_MODEL_URIS = 'calendar-model-uris';
27
28
	/**
29
	 * @deprecated Do not use.
30
	 *
31
	 * Regex pattern constant matching the parable calendar models
32
	 * should be used as an insensitive to match all cases
33
	 *
34
	 * TODO: How crucial is it that this regex is in sync with the list below?
35
	 */
36
	const MODEL_PATTERN = '(Gregorian|Julian|)';
37
38
	/**
39
	 * @param ParserOptions|null $options
40
	 */
41 26
	public function __construct( ParserOptions $options = null ) {
42 26
		parent::__construct( $options );
43
44 26
		$this->defaultOption( self::OPT_CALENDAR_MODEL_URIS, array() );
45 26
	}
46
47
	/**
48
	 * @param string $value
49
	 *
50
	 * @throws ParseException
51
	 * @return string
52
	 */
53 23
	protected function stringParse( $value ) {
54 23
		$uris = $this->getOption( self::OPT_CALENDAR_MODEL_URIS );
55 23
		if ( array_key_exists( $value, $uris ) ) {
56 1
			return $uris[$value];
57
		}
58
59
		switch ( $value ) {
60 22
			case TimeValue::CALENDAR_GREGORIAN:
61 1
				return TimeValue::CALENDAR_GREGORIAN;
62 21
			case TimeValue::CALENDAR_JULIAN:
63 1
				return TimeValue::CALENDAR_JULIAN;
64
		}
65
66 20
		return $this->getCalendarModelUriFromKey( $value );
67
	}
68
69
	/**
70
	 * @param string $value
71
	 *
72
	 * @throws ParseException
73
	 * @return string|null
74
	 */
75 20
	private function getCalendarModelUriFromKey( $value ) {
76 20
		$key = strtolower( trim( $value ) );
77
78
		// TODO: What about abbreviations, e.g. "greg"?
79
		switch ( $key ) {
80 20
			case '':
81 18
			case 'gregorian':
82 15
			case 'western':
83 14
			case 'christian':
84 7
				return TimeValue::CALENDAR_GREGORIAN;
85 13
			case 'julian':
86 4
				return TimeValue::CALENDAR_JULIAN;
87
		}
88
89 9
		throw new ParseException( 'Cannot parse calendar model', $value, self::FORMAT_NAME );
90
	}
91
92
}
93