Debian Codegraph @

vendor/oojs/oojs-ui/php/mixins/LabelElement.php

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Element containing a label.
7 *
8 * @abstract
9 */
10 trait LabelElement {
11 /**
12 * Label value.
13 *
14 * @var string|HtmlSnippet|null
15 */
16 protected $labelValue = null;
17
18 /**
19 * Label value.
20 *
21 * @var bool
22 */
23 protected $invisibleLabel = false;
24
25 /**
26 * @var Tag
27 */
28 protected $label;
29
30 /**
31 * @param array $config Configuration options
32 * - string|HtmlSnippet $config['label'] Label text
33 * - bool $config['invisibleLabel'] Whether the label should be visually hidden (but still
34 * accessible to screen-readers). (default: false)
35 */
36 public function initializeLabelElement( array $config = [] ) {
37 // Properties
38 // FIXME 'labelElement' is a very stupid way to call '$label'
39 $this->label = $config['labelElement'] ?? new Tag( 'span' );
40
41 // Initialization
42 $this->label->addClasses( [ 'oo-ui-labelElement-label' ] );
43 $this->setLabel( $config['label'] ?? null );
44 $this->setInvisibleLabel( $config['invisibleLabel'] ?? false );
45
46 $this->registerConfigCallback( function ( &$config ) {
47 if ( $this->labelValue !== null ) {
48 $config['label'] = $this->labelValue;
49 }
50 if ( $this->invisibleLabel !== false ) {
51 $config['invisibleLabel'] = $this->invisibleLabel;
52 }
53 } );
54 }
55
56 /**
57 * Set the label.
58 *
59 * An empty string will result in the label being hidden. A string containing only whitespace will
60 * be converted to a single `&nbsp;`.
61 *
62 * @param string|HtmlSnippet|null $label Label text
63 * @return $this
64 */
65 public function setLabel( $label ) {
66 $this->labelValue = (string)$label !== '' ? $label : null;
67
68 $this->label->clearContent();
69 if ( $this->labelValue !== null ) {
70 if ( is_string( $this->labelValue ) && trim( $this->labelValue ) === '' ) {
71 $this->label->appendContent( new HtmlSnippet( '&nbsp;' ) );
72 } else {
73 $this->label->appendContent( $label );
74 }
75 }
76
77 $visibleLabel = $this->labelValue !== null && !$this->invisibleLabel;
78 $this->toggleClasses( [ 'oo-ui-labelElement' ], $visibleLabel );
79
80 return $this;
81 }
82
83 /**
84 * Set whether the label should be visually hidden (but still accessible to screen-readers).
85 *
86 * An empty string will result in the label being hidden. A string containing only whitespace will
87 * be converted to a single `&nbsp;`.
88 *
89 * @param bool $invisibleLabel
90 * @return $this
91 */
92 public function setInvisibleLabel( $invisibleLabel ) {
93 $this->invisibleLabel = (bool)$invisibleLabel;
94
95 $this->label->toggleClasses( [ 'oo-ui-labelElement-invisible' ], $this->invisibleLabel );
96 // Pretend that there is no label, a lot of CSS has been written with this assumption
97 $visibleLabel = $this->labelValue !== null && !$this->invisibleLabel;
98 $this->toggleClasses( [ 'oo-ui-labelElement' ], $visibleLabel );
99
100 return $this;
101 }
102
103 /**
104 * Get the label.
105 *
106 * @return string|HtmlSnippet|null Label text
107 */
108 public function getLabel() {
109 return $this->labelValue;
110 }
111
112 /**
113 * Toggle CSS classes.
114 *
115 * @param array $classes List of classes to add
116 * @param bool|null $toggle Add classes
117 * @return $this
118 */
119 abstract public function toggleClasses( array $classes, $toggle = null );
120
121 /**
122 * @param callable $func
123 */
124 abstract public function registerConfigCallback( callable $func );
125 }