| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* Base class for input widgets. |
| 7 |
* |
| 8 |
* @abstract |
| 9 |
*/ |
| 10 |
class InputWidget extends Widget { |
| 11 |
use TabIndexedElement; |
| 12 |
use TitledElement; |
| 13 |
use AccessKeyedElement; |
| 14 |
|
| 15 |
/* Properties */ |
| 16 |
|
| 17 |
/** |
| 18 |
* Input element. |
| 19 |
* |
| 20 |
* @var Tag |
| 21 |
*/ |
| 22 |
protected $input; |
| 23 |
|
| 24 |
/** |
| 25 |
* Input value. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $value = ''; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param array $config Configuration options |
| 33 |
* - string $config['name'] HTML input name (default: '') |
| 34 |
* - string $config['value'] Input value (default: '') |
| 35 |
* - string $config['dir'] The directionality of the input (ltr/rtl) |
| 36 |
* - string $config['inputId'] The value of the input’s HTML `id` attribute. |
| 37 |
*/ |
| 38 |
public function __construct( array $config = [] ) { |
| 39 |
// Parent constructor |
| 40 |
parent::__construct( $config ); |
| 41 |
|
| 42 |
// Properties |
| 43 |
$this->input = $this->getInputElement( $config ); |
| 44 |
|
| 45 |
// Traits |
| 46 |
$this->initializeTabIndexedElement( |
| 47 |
array_merge( [ 'tabIndexed' => $this->input ], $config ) |
| 48 |
); |
| 49 |
$this->initializeTitledElement( |
| 50 |
array_merge( [ 'titled' => $this->input ], $config ) |
| 51 |
); |
| 52 |
$this->initializeAccessKeyedElement( |
| 53 |
array_merge( [ 'accessKeyed' => $this->input ], $config ) |
| 54 |
); |
| 55 |
|
| 56 |
// Initialization |
| 57 |
if ( isset( $config['name'] ) ) { |
| 58 |
$this->input->setAttributes( [ 'name' => $config['name'] ] ); |
| 59 |
} |
| 60 |
if ( $this->isDisabled() ) { |
| 61 |
$this->input->setAttributes( [ 'disabled' => 'disabled' ] ); |
| 62 |
} |
| 63 |
$this |
| 64 |
->addClasses( [ 'oo-ui-inputWidget' ] ) |
| 65 |
->appendContent( $this->input ); |
| 66 |
$this->input->addClasses( [ 'oo-ui-inputWidget-input' ] ); |
| 67 |
$this->setValue( $config['value'] ?? null ); |
| 68 |
if ( isset( $config['dir'] ) ) { |
| 69 |
$this->setDir( $config['dir'] ); |
| 70 |
} |
| 71 |
if ( isset( $config['inputId'] ) ) { |
| 72 |
$this->setInputId( $config['inputId'] ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get input element. |
| 78 |
* |
| 79 |
* @param array $config Configuration options |
| 80 |
* @return Tag Input element |
| 81 |
*/ |
| 82 |
protected function getInputElement( $config ) { |
| 83 |
return new Tag( 'input' ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the value of the input. |
| 88 |
* |
| 89 |
* @return string Input value |
| 90 |
*/ |
| 91 |
public function getValue() { |
| 92 |
return $this->value; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Set the directionality of the input. |
| 97 |
* |
| 98 |
* @param string $dir Text directionality: 'ltr', 'rtl' or 'auto' |
| 99 |
* @return $this |
| 100 |
*/ |
| 101 |
public function setDir( $dir ) { |
| 102 |
$this->input->setAttributes( [ 'dir' => $dir ] ); |
| 103 |
return $this; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Set the value of the input. |
| 108 |
* |
| 109 |
* @param mixed $value New value should usually be a string |
| 110 |
* @return $this |
| 111 |
*/ |
| 112 |
public function setValue( $value ) { |
| 113 |
$this->value = $this->cleanUpValue( $value ); |
| 114 |
$this->input->setValue( $this->value ); |
| 115 |
return $this; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Clean up incoming value. |
| 120 |
* |
| 121 |
* Ensures value is a string, and converts null to empty string. |
| 122 |
* |
| 123 |
* @param mixed $value Original value |
| 124 |
* @return string Cleaned up value |
| 125 |
*/ |
| 126 |
protected function cleanUpValue( $value ) { |
| 127 |
if ( $value === null ) { |
| 128 |
return ''; |
| 129 |
} else { |
| 130 |
return (string)$value; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** @inheritDoc */ |
| 135 |
public function setDisabled( $disabled ) { |
| 136 |
parent::setDisabled( $disabled ); |
| 137 |
if ( isset( $this->input ) ) { |
| 138 |
if ( $this->isDisabled() ) { |
| 139 |
$this->input->setAttributes( [ 'disabled' => 'disabled' ] ); |
| 140 |
} else { |
| 141 |
$this->input->removeAttributes( [ 'disabled' ] ); |
| 142 |
} |
| 143 |
} |
| 144 |
return $this; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Set the 'id' attribute of the `<input>` element. |
| 149 |
* |
| 150 |
* @param string $id The ID of the input element |
| 151 |
* @return $this |
| 152 |
*/ |
| 153 |
public function setInputId( $id ) { |
| 154 |
$this->input->setAttributes( [ 'id' => $id ] ); |
| 155 |
return $this; |
| 156 |
} |
| 157 |
|
| 158 |
/** @inheritDoc */ |
| 159 |
public function getConfig( &$config ) { |
| 160 |
$name = $this->input->getAttribute( 'name' ); |
| 161 |
if ( $name !== null ) { |
| 162 |
$config['name'] = $name; |
| 163 |
} |
| 164 |
if ( $this->value !== '' ) { |
| 165 |
$config['value'] = $this->value; |
| 166 |
} |
| 167 |
$dir = $this->input->getAttribute( 'dir' ); |
| 168 |
if ( $dir !== null ) { |
| 169 |
$config['dir'] = $dir; |
| 170 |
} |
| 171 |
$id = $this->input->getAttribute( 'id' ); |
| 172 |
if ( $id !== null ) { |
| 173 |
$config['inputId'] = $id; |
| 174 |
} |
| 175 |
return parent::getConfig( $config ); |
| 176 |
} |
| 177 |
} |