Completed
Branch master (6ee3f9)
by
unknown
29:15
created

HTMLDateTimeField::parseDate()   B

Complexity

Conditions 5
Paths 13

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 13
nop 1
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * A field that will contain a date and/or time
5
 *
6
 * Currently recognizes only {YYYY}-{MM}-{DD}T{HH}:{MM}:{SS.S*}Z formatted dates.
7
 *
8
 * Besides the parameters recognized by HTMLTextField, additional recognized
9
 * parameters in the field descriptor array include:
10
 *  type - 'date', 'time', or 'datetime'
11
 *  min - The minimum date to allow, in any recognized format.
12
 *  max - The maximum date to allow, in any recognized format.
13
 *  placeholder - The default comes from the htmlform-(date|time|datetime)-placeholder message.
14
 *
15
 * The result is a formatted date.
16
 *
17
 * @note This widget is not likely to work well in non-OOUI forms.
18
 */
19
class HTMLDateTimeField extends HTMLTextField {
20
	protected static $patterns = [
21
		'date' => '[0-9]{4}-[01][0-9]-[0-3][0-9]',
22
		'time' => '[0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?',
23
		'datetime' => '[0-9]{4}-[01][0-9]-[0-3][0-9][T ][0-2][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9]+)?Z?',
24
	];
25
26
	protected $mType = 'datetime';
27
28
	public function __construct( $params ) {
29
		parent::__construct( $params );
30
31
		$this->mType = array_key_exists( 'type', $params )
32
			? $params['type']
33
			: 'datetime';
34
35
		if ( !in_array( $this->mType, [ 'date', 'time', 'datetime' ] ) ) {
36
			throw new InvalidArgumentException( "Invalid type '$this->mType'" );
37
		}
38
39
		$this->mClass .= ' mw-htmlform-datetime-field';
40
	}
41
42
	public function getAttributes( array $list ) {
43
		$parentList = array_diff( $list, [ 'min', 'max' ] );
44
		$ret = parent::getAttributes( $parentList );
45
46
		if ( in_array( 'placeholder', $list ) && !isset( $ret['placeholder'] ) ) {
47
			// Messages: htmlform-date-placeholder htmlform-time-placeholder htmlform-datetime-placeholder
48
			$ret['placeholder'] = $this->msg( "htmlform-{$this->mType}-placeholder" )->text();
49
		}
50
51 View Code Duplication
		if ( in_array( 'min', $list ) && isset( $this->mParams['min'] ) ) {
52
			$min = $this->parseDate( $this->mParams['min'] );
53
			if ( $min ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $min of type false|integer 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...
54
				$ret['min'] = $this->formatDate( $min );
55
				// Because Html::expandAttributes filters it out
56
				$ret['data-min'] = $ret['min'];
57
			}
58
		}
59 View Code Duplication
		if ( in_array( 'max', $list ) && isset( $this->mParams['max'] ) ) {
60
			$max = $this->parseDate( $this->mParams['max'] );
61
			if ( $max ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $max of type false|integer 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...
62
				$ret['max'] = $this->formatDate( $max );
63
				// Because Html::expandAttributes filters it out
64
				$ret['data-max'] = $ret['max'];
65
			}
66
		}
67
68
		$ret['step'] = 1;
69
		// Because Html::expandAttributes filters it out
70
		$ret['data-step'] = 1;
71
72
		$ret['type'] = $this->mType;
73
		$ret['pattern'] = static::$patterns[$this->mType];
74
75
		return $ret;
76
	}
77
78
	function loadDataFromRequest( $request ) {
79
		if ( !$request->getCheck( $this->mName ) ) {
80
			return $this->getDefault();
81
		}
82
83
		$value = $request->getText( $this->mName );
84
		$date = $this->parseDate( $value );
85
		return $date ? $this->formatDate( $date ) : $value;
86
	}
87
88
	function validate( $value, $alldata ) {
89
		$p = parent::validate( $value, $alldata );
90
91
		if ( $p !== true ) {
92
			return $p;
93
		}
94
95
		if ( $value === '' ) {
96
			// required was already checked by parent::validate
97
			return true;
98
		}
99
100
		$date = $this->parseDate( $value );
101
		if ( !$date ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type false|integer is loosely compared to false; 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...
102
			// Messages: htmlform-date-invalid htmlform-time-invalid htmlform-datetime-invalid
103
			return $this->msg( "htmlform-{$this->mType}-invalid" )->parseAsBlock();
104
		}
105
106 View Code Duplication
		if ( isset( $this->mParams['min'] ) ) {
107
			$min = $this->parseDate( $this->mParams['min'] );
108
			if ( $min && $date < $min ) {
109
				// Messages: htmlform-date-toolow htmlform-time-toolow htmlform-datetime-toolow
110
				return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) )
111
					->parseAsBlock();
112
			}
113
		}
114
115 View Code Duplication
		if ( isset( $this->mParams['max'] ) ) {
116
			$max = $this->parseDate( $this->mParams['max'] );
117
			if ( $max && $date > $max ) {
118
				// Messages: htmlform-date-toohigh htmlform-time-toohigh htmlform-datetime-toohigh
119
				return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) )
120
					->parseAsBlock();
121
			}
122
		}
123
124
		return true;
125
	}
126
127
	protected function parseDate( $value ) {
128
		$value = trim( $value );
129
		if ( $value === '' ) {
130
			return false;
131
		}
132
133
		if ( $this->mType === 'date' ) {
134
			$value .= ' T00:00:00+0000';
135
		}
136
		if ( $this->mType === 'time' ) {
137
			$value = '1970-01-01 ' . $value . '+0000';
138
		}
139
140
		try {
141
			$date = new DateTime( $value, new DateTimeZone( 'GMT' ) );
142
			return $date->getTimestamp();
143
		} catch ( Exception $ex ) {
144
			return false;
145
		}
146
	}
147
148
	protected function formatDate( $value ) {
149
		switch ( $this->mType ) {
150
			case 'date':
151
				return gmdate( 'Y-m-d', $value );
152
153
			case 'time':
154
				return gmdate( 'H:i:s', $value );
155
156
			case 'datetime':
157
				return gmdate( 'Y-m-d\\TH:i:s\\Z', $value );
158
		}
159
	}
160
161
	public function getInputOOUI( $value ) {
162
		$params = [
163
			'type' => $this->mType,
164
			'value' => $value,
165
			'name' => $this->mName,
166
			'id' => $this->mID,
167
		];
168
169 View Code Duplication
		if ( isset( $this->mParams['min'] ) ) {
170
			$min = $this->parseDate( $this->mParams['min'] );
171
			if ( $min ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $min of type false|integer 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...
172
				$params['min'] = $this->formatDate( $min );
173
			}
174
		}
175 View Code Duplication
		if ( isset( $this->mParams['max'] ) ) {
176
			$max = $this->parseDate( $this->mParams['max'] );
177
			if ( $max ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $max of type false|integer 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...
178
				$params['max'] = $this->formatDate( $max );
179
			}
180
		}
181
182
		return new MediaWiki\Widget\DateTimeInputWidget( $params );
183
	}
184
185
	protected function getOOUIModules() {
186
		return [ 'mediawiki.widgets.datetime' ];
187
	}
188
189
	protected function shouldInfuseOOUI() {
190
		return true;
191
	}
192
193
}
194