Debian Codegraph @

vendor/oojs/oojs-ui/php/widgets/NumberInputWidget.php

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Input widget with a number field.
7 */
8 class NumberInputWidget extends TextInputWidget {
9
10 /** @var float|null */
11 protected $buttonStep;
12 /** @var float|null */
13 protected $pageStep;
14 /** @var bool|null */
15 protected $showButtons;
16
17 /**
18 * @param array $config Configuration options
19 * - float $config['min'] Minimum input allowed
20 * - float $config['max'] Maximum input allowed
21 * - float|null $config['step'] If specified, the field only accepts values that are
22 * multiples of this. (default: null)
23 * - float $config['buttonStep'] Delta when using the buttons or Up/Down arrow keys.
24 * Defaults to `step` if specified, otherwise `1`.
25 * - float $config['pageStep'] Delta when using the Page-up/Page-down keys.
26 * Defaults to 10 times `buttonStep`.
27 * - bool $config['showButtons'] Show increment and decrement buttons (default: true)
28 */
29 public function __construct( array $config = [] ) {
30 $config['type'] = 'number';
31
32 // Parent constructor
33 parent::__construct( $config );
34
35 if ( isset( $config['min'] ) ) {
36 $this->input->setAttributes( [ 'min' => $config['min'] ] );
37 }
38
39 if ( isset( $config['max'] ) ) {
40 $this->input->setAttributes( [ 'max' => $config['max'] ] );
41 }
42
43 $this->input->setAttributes( [ 'step' => $config['step'] ?? 'any' ] );
44
45 if ( isset( $config['buttonStep'] ) ) {
46 $this->buttonStep = $config['buttonStep'];
47 }
48 if ( isset( $config['pageStep'] ) ) {
49 $this->pageStep = $config['pageStep'];
50 }
51 if ( isset( $config['showButtons'] ) ) {
52 $this->showButtons = $config['showButtons'];
53 }
54
55 $this->addClasses( [
56 'oo-ui-numberInputWidget',
57 'oo-ui-numberInputWidget-php',
58 ] );
59 }
60
61 /** @inheritDoc */
62 public function getConfig( &$config ) {
63 $min = $this->input->getAttribute( 'min' );
64 if ( $min !== null ) {
65 $config['min'] = $min;
66 }
67 $max = $this->input->getAttribute( 'max' );
68 if ( $max !== null ) {
69 $config['max'] = $max;
70 }
71 $step = $this->input->getAttribute( 'step' );
72 if ( $step !== 'any' ) {
73 $config['step'] = $step;
74 }
75 if ( $this->pageStep !== null ) {
76 $config['pageStep'] = $this->pageStep;
77 }
78 if ( $this->buttonStep !== null ) {
79 $config['buttonStep'] = $this->buttonStep;
80 }
81 if ( $this->showButtons !== null ) {
82 $config['showButtons'] = $this->showButtons;
83 }
84 return parent::getConfig( $config );
85 }
86 }