Merge "Revision: Handle all return values of Title::newFromId"
[lhc/web/wiklou.git] / includes / widget / DateTimeInputWidget.php
1 <?php
2 /**
3 * MediaWiki Widgets – DateTimeInputWidget class.
4 *
5 * @copyright 2016 MediaWiki Widgets Team and others; see AUTHORS.txt
6 * @license The MIT License (MIT); see LICENSE.txt
7 */
8 namespace MediaWiki\Widget;
9
10 use OOUI\Tag;
11
12 /**
13 * Date-time input widget.
14 */
15 class DateTimeInputWidget extends \OOUI\InputWidget {
16
17 protected $type = null;
18 protected $min = null;
19 protected $max = null;
20 protected $clearable = null;
21
22 /**
23 * @param array $config Configuration options
24 * @param string $config['type'] 'date', 'time', or 'datetime'
25 * @param string $config['min'] Minimum date, time, or datetime
26 * @param string $config['max'] Maximum date, time, or datetime
27 * @param bool $config['clearable'] Whether to provide for blanking the value.
28 */
29 public function __construct( array $config = [] ) {
30 // We need $this->type set before calling the parent constructor
31 if ( isset( $config['type'] ) ) {
32 $this->type = $config['type'];
33 } else {
34 throw new \InvalidArgumentException( '$config[\'type\'] must be specified' );
35 }
36
37 parent::__construct( $config );
38
39 // Properties, which are ignored in PHP and just shipped back to JS
40 if ( isset( $config['min'] ) ) {
41 $this->min = $config['min'];
42 }
43 if ( isset( $config['max'] ) ) {
44 $this->max = $config['max'];
45 }
46 if ( isset( $config['clearable'] ) ) {
47 $this->clearable = $config['clearable'];
48 }
49
50 // Initialization
51 $this->addClasses( [ 'mw-widgets-datetime-dateTimeInputWidget' ] );
52 }
53
54 protected function getJavaScriptClassName() {
55 return 'mw.widgets.datetime.DateTimeInputWidget';
56 }
57
58 public function getConfig( &$config ) {
59 $config['type'] = $this->type;
60 if ( $this->min !== null ) {
61 $config['min'] = $this->min;
62 }
63 if ( $this->max !== null ) {
64 $config['max'] = $this->max;
65 }
66 if ( $this->clearable !== null ) {
67 $config['clearable'] = $this->clearable;
68 }
69 return parent::getConfig( $config );
70 }
71
72 protected function getInputElement( $config ) {
73 return ( new Tag( 'input' ) )->setAttributes( [ 'type' => $this->type ] );
74 }
75 }