Completed
Push — master ( e3e0ad...6fbcf7 )
by
unknown
02:29
created

YearMonthTimeParser::parseMonth()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace ValueParsers;
4
5
use DataValues\TimeValue;
6
7
/**
8
 * A parser that accepts various date formats with month precision. Prefers month/year order when
9
 * both numbers are valid months, e.g. "12/10" is December 2010. Should be called before
10
 * YearTimeParser when you want to accept both formats, because strings like "1 999" may either
11
 * represent a month and a year or a year with digit grouping.
12
 *
13
 * @since 0.8.4
14
 *
15
 * @license GPL-2.0+
16
 * @author Addshore
17
 * @author Thiemo Mättig
18
 *
19
 * @todo match BCE dates in here
20
 */
21
class YearMonthTimeParser extends StringValueParser {
22
23
	const FORMAT_NAME = 'year-month';
24
25
	/**
26
	 * @var int[] Array mapping localized month names to month numbers (1 to 12).
27
	 */
28
	private $monthNumbers;
29
30
	/**
31
	 * @var ValueParser
32
	 */
33
	private $isoTimestampParser;
34
35
	/**
36
	 * @see StringValueParser::__construct
37
	 *
38
	 * @param MonthNameProvider $monthNameProvider
39
	 * @param ParserOptions|null $options
40
	 */
41
	public function __construct(
42
		MonthNameProvider $monthNameProvider,
43
		ParserOptions $options = null
44
	) {
45
		parent::__construct( $options );
46
47
		$languageCode = $this->getOption( ValueParser::OPT_LANG );
48
		$this->monthNumbers = $monthNameProvider->getMonthNumbers( $languageCode );
49
		$this->isoTimestampParser = new IsoTimestampParser( null, $this->options );
50
	}
51
52
	/**
53
	 * @see StringValueParser::stringParse
54
	 *
55
	 * @param string $value
56
	 *
57
	 * @throws ParseException
58
	 * @return TimeValue
59
	 */
60
	protected function stringParse( $value ) {
61
		//Matches Year and month separated by a separator, \p{L} matches letters outside the ASCII range
62
		if ( !preg_match( '/^(-?[\d\p{L}]+)\s*?[\/\-\s.,]\s*(-?[\d\p{L}]+)$/', trim( $value ), $matches ) ) {
63
			throw new ParseException( 'Failed to parse year and month', $value, self::FORMAT_NAME );
64
		}
65
		list( , $a, $b ) = $matches;
66
67
		$aIsInt = preg_match( '/^-?\d+$/', $a );
68
		$bIsInt = preg_match( '/^-?\d+$/', $b );
69
70
		if ( $aIsInt && $bIsInt ) {
71
			if ( $this->canBeMonth( $a ) ) {
72
				return $this->getTimeFromYearMonth( $b, $a );
73
			} elseif ( $this->canBeMonth( $b ) ) {
74
				return $this->getTimeFromYearMonth( $a, $b );
75
			}
76
		} elseif ( $aIsInt ) {
77
			$month = $this->parseMonth( $b );
78
79
			if ( $month ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $month of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
80
				return $this->getTimeFromYearMonth( $a, $month );
81
			}
82
		} elseif ( $bIsInt ) {
83
			$month = $this->parseMonth( $a );
84
85
			if ( $month ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $month of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
86
				return $this->getTimeFromYearMonth( $b, $month );
87
			}
88
		}
89
90
		throw new ParseException( 'Failed to parse year and month', $value, self::FORMAT_NAME );
91
	}
92
93
	/**
94
	 * @param string $month
95
	 *
96
	 * @return int|null
97
	 */
98
	private function parseMonth( $month ) {
99
		foreach ( $this->monthNumbers as $monthName => $i ) {
100
			if ( strcasecmp( $monthName, $month ) === 0 ) {
101
				return $i;
102
			}
103
		}
104
105
		return null;
106
	}
107
108
	/**
109
	 * @param string $year
110
	 * @param string $month as a canonical month number
111
	 *
112
	 * @return TimeValue
113
	 */
114
	private function getTimeFromYearMonth( $year, $month ) {
115
		if ( $year[0] !== '-' ) {
116
			$year = '+' . $year;
117
		}
118
119
		return $this->isoTimestampParser->parse( sprintf( '%s-%02s-00T00:00:00Z', $year, $month ) );
120
	}
121
122
	/**
123
	 * @param string $value
124
	 *
125
	 * @return bool can the given value be a month?
126
	 */
127
	private function canBeMonth( $value ) {
128
		return $value >= 0 && $value <= 12;
129
	}
130
131
}
132