Debian Codegraph @

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

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Element containing a sequence of child elements.
7 *
8 * @abstract
9 */
10 trait GroupElement {
11 /**
12 * List of items in the group as Elements.
13 *
14 * @var Element[]
15 */
16 protected $items = [];
17
18 /**
19 * @var Tag
20 */
21 protected $group;
22
23 /**
24 * @param array $config Configuration options
25 */
26 public function initializeGroupElement( array $config = [] ) {
27 // Properties
28 $this->group = $config['group'] ?? new Tag( 'div' );
29
30 $this->registerConfigCallback( function ( &$config ) {
31 $config['items'] = $this->items;
32 } );
33 }
34
35 /**
36 * Check if there are no items.
37 *
38 * @return bool Group is empty
39 */
40 public function isEmpty() {
41 return $this->items === [];
42 }
43
44 /**
45 * Get items.
46 *
47 * @return Element[] Items
48 */
49 public function getItems() {
50 return $this->items;
51 }
52
53 /**
54 * @param mixed $data
55 * @return Element|null
56 */
57 public function findItemFromData( $data ) {
58 $items = $this->getItems();
59 // TODO: Support non-string $data using a hash (e.g. json_encode)
60 foreach ( $items as $item ) {
61 if ( $item->getData() === $data ) {
62 return $item;
63 }
64 }
65 return null;
66 }
67
68 /**
69 * Add items.
70 *
71 * Adding an existing item will move it.
72 *
73 * @param Element[] $items Items
74 * @param int|null $index Index to insert items at
75 * @return $this
76 */
77 public function addItems( array $items, $index = null ) {
78 foreach ( $items as $item ) {
79 // Check if item exists then remove it first, effectively "moving" it
80 $currentIndex = array_search( $item, $this->items, true );
81 if ( $currentIndex !== false ) {
82 $this->removeItems( [ $item ] );
83 // Adjust index to compensate for removal
84 if ( $currentIndex < $index ) {
85 $index--;
86 }
87 }
88 // Add the item
89 $item->setElementGroup( $this );
90 }
91
92 if ( $index === null || $index < 0 || $index >= count( $this->items ) ) {
93 $this->items = array_merge( $this->items, $items );
94 } else {
95 array_splice( $this->items, $index, 0, $items );
96 }
97
98 // Update actual target element contents to reflect our list
99 $this->group->clearContent();
100 $this->group->appendContent( $this->items );
101
102 return $this;
103 }
104
105 /**
106 * Remove items.
107 *
108 * @param Element[] $items Items to remove
109 * @return $this
110 */
111 public function removeItems( $items ) {
112 foreach ( $items as $item ) {
113 $index = array_search( $item, $this->items, true );
114 if ( $index !== false ) {
115 $item->setElementGroup( null );
116 array_splice( $this->items, $index, 1 );
117 }
118 }
119
120 // Update actual target element contents to reflect our list
121 $this->group->clearContent();
122 $this->group->appendContent( $this->items );
123
124 return $this;
125 }
126
127 /**
128 * Clear all items.
129 *
130 * Items will be detached, not removed, so they can be used later.
131 *
132 * @return $this
133 */
134 public function clearItems() {
135 foreach ( $this->items as $item ) {
136 $item->setElementGroup( null );
137 }
138
139 $this->items = [];
140 $this->group->clearContent();
141
142 return $this;
143 }
144
145 /**
146 * @param callable $func
147 */
148 abstract public function registerConfigCallback( callable $func );
149 }