@@ -45,11 +45,11 @@ class WidgetGroup
4545 protected $ separator = '' ;
4646
4747 /**
48- * The number of widgets in the group.
48+ * Id that is going to be issued to the next widget when it's added to the group.
4949 *
5050 * @var int
5151 */
52- protected $ count = 0 ;
52+ protected $ nextWidgetId = 1 ;
5353
5454 /**
5555 * A callback that defines extra markup that wraps every widget in the group.
@@ -80,11 +80,13 @@ public function display()
8080
8181 $ output = '' ;
8282 $ index = 0 ;
83+ $ count = $ this ->count ();
84+
8385 foreach ($ this ->widgets as $ position => $ widgets ) {
8486 foreach ($ widgets as $ widget ) {
85- $ output .= $ this ->performWrap ($ this ->displayWidget ($ widget ), $ index , $ this -> count );
87+ $ output .= $ this ->performWrap ($ this ->displayWidget ($ widget ), $ index , $ count );
8688 $ index ++;
87- if ($ this -> count !== $ index ) {
89+ if ($ index !== $ count ) {
8890 $ output .= $ this ->separator ;
8991 }
9092 }
@@ -93,6 +95,59 @@ public function display()
9395 return $ this ->convertToViewExpression ($ output );
9496 }
9597
98+ /**
99+ * Remove a widget by its id.
100+ *
101+ * @param int $id
102+ */
103+ public function removeById ($ id )
104+ {
105+ foreach ($ this ->widgets as $ position => $ widgets ) {
106+ foreach ($ widgets as $ i => $ widget ) {
107+ if ($ widget ['id ' ] === $ id ) {
108+ unset($ this ->widgets [$ position ][$ i ]);
109+ return ;
110+ }
111+ }
112+ }
113+ }
114+
115+ /**
116+ * Remove all widgets with $name from the group.
117+ *
118+ * @param string $name
119+ */
120+ public function removeByName ($ name )
121+ {
122+ foreach ($ this ->widgets as $ position => $ widgets ) {
123+ foreach ($ widgets as $ i => $ widget ) {
124+ if ($ widget ['arguments ' ][0 ] === $ name ) {
125+ unset($ this ->widgets [$ position ][$ i ]);
126+ }
127+ }
128+ }
129+ }
130+
131+ /**
132+ * Remove all widgets from $position from the group.
133+ *
134+ * @param int|string $position
135+ */
136+ public function removeByPosition ($ position )
137+ {
138+ if (array_key_exists ($ position , $ this ->widgets )) {
139+ unset($ this ->widgets [$ position ]);
140+ }
141+ }
142+
143+ /**
144+ * Remove all widgets from the group.
145+ */
146+ public function removeAll ()
147+ {
148+ $ this ->widgets = [];
149+ }
150+
96151 /**
97152 * Set widget position.
98153 *
@@ -112,15 +167,15 @@ public function position($position)
112167 */
113168 public function addWidget ()
114169 {
115- $ this ->addWidgetWithType ('sync ' , func_get_args ());
170+ return $ this ->addWidgetWithType ('sync ' , func_get_args ());
116171 }
117172
118173 /**
119174 * Add an async widget to the group.
120175 */
121176 public function addAsyncWidget ()
122177 {
123- $ this ->addWidgetWithType ('async ' , func_get_args ());
178+ return $ this ->addWidgetWithType ('async ' , func_get_args ());
124179 }
125180
126181 /**
@@ -195,27 +250,31 @@ public function count()
195250
196251 return $ count ;
197252 }
198-
253+
199254 /**
200255 * Add a widget with a given type to the array.
201256 *
202257 * @param string $type
203- * @param array $arguments
258+ * @param array $arguments
259+ * @return int
204260 */
205261 protected function addWidgetWithType ($ type , array $ arguments = [])
206262 {
207263 if (!isset ($ this ->widgets [$ this ->position ])) {
208264 $ this ->widgets [$ this ->position ] = [];
209265 }
210266
267+ $ id = $ this ->nextWidgetId ;
211268 $ this ->widgets [$ this ->position ][] = [
269+ 'id ' => $ id ,
212270 'arguments ' => $ arguments ,
213271 'type ' => $ type ,
214272 ];
215273
216- $ this ->count ++;
217-
218274 $ this ->resetPosition ();
275+ $ this ->nextWidgetId ++;
276+
277+ return $ id ;
219278 }
220279
221280 /**
0 commit comments