Passed
Push — master ( 598343...22ce15 )
by
unknown
03:14
created

EraParser::parseEraFromSuffix()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 8.8571
cc 3
eloc 18
nc 3
nop 1
crap 3
1
<?php
2
3
namespace ValueParsers;
4
5
/**
6
 * A rudimentary ValueParser capable of extracting era information from any date and time string. It
7
 * checks for signs as in "-150,000" as well as suffixes as in "150,000 BC".
8
 *
9
 * @since 0.8
10
 *
11
 * @license GPL-2.0+
12
 * @author Addshore
13
 * @author Thiemo Mättig
14
 */
15
class EraParser extends StringValueParser {
16
17
	const FORMAT_NAME = 'era';
18
19
	/**
20
	 * @since 0.8
21
	 */
22
	const BEFORE_COMMON_ERA = '-';
23
24
	/**
25
	 * @since 0.8
26
	 */
27
	const COMMON_ERA = '+';
28
29
	/**
30
	 * @param string $value
31
	 *
32
	 * @throws ParseException
33
	 * @return string[] Array of the parsed era constant and the value with the era stripped.
34
	 */
35 38
	protected function stringParse( $value ) {
36 38
		$value = trim( $value );
37
38 38
		$eraFromSign = $this->parseEraFromSign( $value );
39 38
		$eraFromSuffix = $this->parseEraFromSuffix( $value );
40
41 38
		if ( $eraFromSign && $eraFromSuffix ) {
42 8
			throw new ParseException(
43 8
				'Parsed two eras from the same string',
44 8
				$value,
45 8
				self::FORMAT_NAME
46
			);
47
		}
48
49
		// Default to CE
50 30
		return $eraFromSign ?: $eraFromSuffix ?: array( self::COMMON_ERA, $value );
51
	}
52
53
	/**
54
	 * @param string $value
55
	 *
56
	 * @return string[]|null Array of the era constant and the value with the era stripped, or null
57
	 * if not successful.
58
	 */
59 38
	private function parseEraFromSign( $value ) {
60 38
		$sign = substr( $value, 0, 1 );
61
62 38
		if ( $sign === self::BEFORE_COMMON_ERA || $sign === self::COMMON_ERA ) {
63
			return array(
64 13
				$sign,
65 13
				substr( $value, 1 )
66
			);
67
		}
68
69 25
		return null;
70
	}
71
72
	/**
73
	 * @param string $value
74
	 *
75
	 * @return string[]|null Array of the era constant and the value with the era stripped, or null
76
	 * if not successful.
77
	 */
78 38
	private function parseEraFromSuffix( $value ) {
79 38
		if ( preg_match(
80 38
			'/(?:B\.?\s*C\.?(?:\s*E\.?)?|Before\s+C(?:hrist|(?:ommon|urrent|hristian)\s+Era))$/i',
81 38
			$value,
82 38
			$matches,
83 38
			PREG_OFFSET_CAPTURE )
84
		) {
85
			return array(
86 16
				self::BEFORE_COMMON_ERA,
87 16
				rtrim( substr( $value, 0, $matches[0][1] ) )
88
			);
89 22
		} elseif ( preg_match(
90 22
			'/(?:C\.?\s*E\.?|A\.?\s*D\.?|C(?:ommon|urrent|hristian)\s+Era|After\s+Christ|Anno\s+Domini)$/i',
91 22
			$value,
92 22
			$matches,
93 22
			PREG_OFFSET_CAPTURE
94
		) ) {
95
			return array(
96 14
				self::COMMON_ERA,
97 14
				rtrim( substr( $value, 0, $matches[0][1] ) )
98
			);
99
		}
100
101 8
		return null;
102
	}
103
104
}
105