Skip to content

Commit 71c72ad

Browse files
committed
Any, isEmpty and Count
1 parent 19fe9fb commit 71c72ad

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,9 @@ equals
309309

310310
You can also set a separator to display between widgets in a group.
311311
`Widget::group('sidebar')->setSeparator('<hr>');`
312+
313+
### Checking the state of a widget group
314+
315+
`Widget::group('sidebar')->isEmpty();`
316+
`Widget::group('sidebar')->any();`
317+
`Widget::group('sidebar')->count();`

src/WidgetGroup.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,41 @@ public function setSeparator($separator)
140140
return $this;
141141
}
142142

143+
/**
144+
* Check if there are any widgets in the group.
145+
*
146+
* @return bool
147+
*/
148+
public function any()
149+
{
150+
return ! $this->isEmpty();
151+
}
152+
153+
/**
154+
* Check if there are no widgets in the group.
155+
*
156+
* @return bool
157+
*/
158+
public function isEmpty()
159+
{
160+
return empty($this->widgets);
161+
}
162+
163+
/**
164+
* Count the number of widgets in this group.
165+
*
166+
* @return int
167+
*/
168+
public function count()
169+
{
170+
$count = 0;
171+
foreach ($this->widgets as $position => $widgets) {
172+
$count += count($widgets);
173+
}
174+
175+
return $count;
176+
}
177+
143178
/**
144179
* Add a widget with a given type to the array.
145180
*

tests/WidgetGroupTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,39 @@ public function testSeparator()
9393
'</script>'.
9494
'</div>', $output);
9595
}
96+
97+
public function testIsEmpty()
98+
{
99+
$this->assertTrue($this->widgetGroup->isEmpty());
100+
101+
$this->widgetGroup->addWidget('Slider');
102+
103+
$this->assertFalse($this->widgetGroup->isEmpty());
104+
}
105+
106+
public function testAny()
107+
{
108+
$this->assertFalse($this->widgetGroup->any());
109+
110+
$this->widgetGroup->addWidget('Slider');
111+
112+
$this->assertTrue($this->widgetGroup->any());
113+
}
114+
115+
public function testCount()
116+
{
117+
$this->assertSame(0, $this->widgetGroup->count());
118+
119+
$this->widgetGroup->addWidget('Slider');
120+
121+
$this->assertSame(1, $this->widgetGroup->count());
122+
123+
$this->widgetGroup->position(50)->addWidget('Slider');
124+
125+
$this->assertSame(2, $this->widgetGroup->count());
126+
127+
$this->widgetGroup->position(50)->addWidget('Slider');
128+
129+
$this->assertSame(3, $this->widgetGroup->count());
130+
}
96131
}

0 commit comments

Comments
 (0)