YearMonthTimeParser::canBeMonth()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 Kreuz
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 68
	public function __construct(
42
		MonthNameProvider $monthNameProvider,
43
		ParserOptions $options = null
44
	) {
45 68
		parent::__construct( $options );
46
47 68
		$languageCode = $this->getOption( ValueParser::OPT_LANG );
48 68
		$this->monthNumbers = $monthNameProvider->getMonthNumbers( $languageCode );
49 68
		$this->isoTimestampParser = new IsoTimestampParser( null, $this->options );
50 68
	}
51
52
	/**
53
	 * @see StringValueParser::stringParse
54
	 *
55
	 * @param string $value
56
	 *
57
	 * @throws ParseException
58
	 * @return TimeValue
59
	 */
60 61
	protected function stringParse( $value ) {
61
		// Matches year and month separated by a separator.
62
		// \p{L} matches letters outside the ASCII range.
63 61
		if ( !preg_match( '/^(-?[\d\p{L}]+)\s*?[\/\-\s.,]\s*(-?[\d\p{L}]+)$/', trim( $value ), $matches ) ) {
64 11
			throw new ParseException( 'Failed to parse year and month', $value, self::FORMAT_NAME );
65
		}
66 50
		list( , $a, $b ) = $matches;
67
68 50
		$aIsInt = preg_match( '/^-?\d+$/', $a );
69 50
		$bIsInt = preg_match( '/^-?\d+$/', $b );
70
71 50
		if ( $aIsInt && $bIsInt ) {
72 30
			if ( $this->canBeMonth( $a ) ) {
73 21
				return $this->getTimeFromYearMonth( $b, $a );
74 9
			} elseif ( $this->canBeMonth( $b ) ) {
75 9
				return $this->getTimeFromYearMonth( $a, $b );
76
			}
77 20
		} elseif ( $aIsInt ) {
78 3
			$month = $this->parseMonth( $b );
79
80 3
			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...
81 3
				return $this->getTimeFromYearMonth( $a, $month );
82
			}
83 17
		} elseif ( $bIsInt ) {
84 16
			$month = $this->parseMonth( $a );
85
86 16
			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...
87 14
				return $this->getTimeFromYearMonth( $b, $month );
88
			}
89
		}
90
91 8
		throw new ParseException( 'Failed to parse year and month', $value, self::FORMAT_NAME );
92
	}
93
94
	/**
95
	 * @param string $month
96
	 *
97
	 * @return int|null
98
	 */
99 19
	private function parseMonth( $month ) {
100 19
		foreach ( $this->monthNumbers as $monthName => $i ) {
101 19
			if ( strcasecmp( $monthName, $month ) === 0 ) {
102 19
				return $i;
103
			}
104
		}
105
106 2
		return null;
107
	}
108
109
	/**
110
	 * @param string $year
111
	 * @param string $month as a canonical month number
112
	 *
113
	 * @return TimeValue
114
	 */
115 42
	private function getTimeFromYearMonth( $year, $month ) {
116 42
		if ( $year[0] !== '-' ) {
117 38
			$year = '+' . $year;
118
		}
119
120 42
		return $this->isoTimestampParser->parse( sprintf( '%s-%02s-00T00:00:00Z', $year, $month ) );
121
	}
122
123
	/**
124
	 * @param string $value
125
	 *
126
	 * @return bool can the given value be a month?
127
	 */
128 30
	private function canBeMonth( $value ) {
129 30
		return $value >= 0 && $value <= 12;
130
	}
131
132
}
133