Skip to content

Adds the ability to add multiple checkbox and radio items #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,27 @@ $menu = (new CliMenuBuilder)
->build();
```

You can add multiple checkbox items at once like so:

```php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\CliMenu;

$callable = function (CliMenu $menu) {
echo 'I am alive!';
};

$menu = (new CliMenuBuilder)
->addCheckboxItems([
['Item 1', $callable],
['Item 2', $callable],
['Item 3', $callable],
])
->build();
```

When selecting an item, it will be toggled. Notice at first each item is unchecked. After selecting one it will become
checked.

Expand All @@ -597,6 +618,27 @@ $menu = (new CliMenuBuilder)
->build();
```

You can add multiple radio items at once like so:

```php
<?php

use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\CliMenu;

$callable = function (CliMenu $menu) {
echo 'I am alive!';
};

$menu = (new CliMenuBuilder)
->addRadioItems([
['Item 1', $callable],
['Item 2', $callable],
['Item 3', $callable],
])
->build();
```

When selecting an item, it will be toggled. Notice at first each item is unchecked. After selecting one it will become
checked and all other `RadioItem` within the same level will be unchecked.

Expand Down
6 changes: 4 additions & 2 deletions examples/checkbox-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
->addSubMenu('Compiled', function (CliMenuBuilder $b) use ($itemCallable) {
$b->setTitle('Compiled Languages')
->addCheckboxItem('Rust', $itemCallable)
->addCheckboxItem('C++', $itemCallable)
->addCheckboxItem('Go', $itemCallable)
->addCheckboxItem('Java', $itemCallable)
->addCheckboxItem('C', $itemCallable)
->addCheckboxItems([
['C++', $itemCallable],
['C', $itemCallable]
]);
;
})
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
Expand Down
6 changes: 4 additions & 2 deletions examples/radio-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
->addSubMenu('Compiled', function (CliMenuBuilder $b) use ($itemCallable) {
$b->setTitle('Compiled Languages')
->addRadioItem('Rust', $itemCallable)
->addRadioItem('C++', $itemCallable)
->addRadioItem('Go', $itemCallable)
->addRadioItem('Java', $itemCallable)
->addRadioItem('C', $itemCallable)
->addRadioItems([
['C++', $itemCallable],
['C', $itemCallable]
])
;
})
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
Expand Down
18 changes: 18 additions & 0 deletions src/Builder/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ public function addCheckboxItem(
return $this;
}

public function addCheckboxItems(array $items): self
{
foreach ($items as $item) {
$this->addCheckboxItem(...$item);
}

return $this;
}

public function addRadioItem(
string $text,
callable $itemCallable,
Expand All @@ -165,6 +174,15 @@ public function addRadioItem(
return $this;
}

public function addRadioItems(array $items): self
{
foreach ($items as $item) {
$this->addRadioItem(...$item);
}

return $this;
}

public function addStaticItem(string $text) : self
{
$this->addMenuItem(new StaticItem($text));
Expand Down
54 changes: 54 additions & 0 deletions test/Builder/CliMenuBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,33 @@ public function testAddMultipleItems() : void
$this->checkMenuItems($menu, $expected);
}

public function testAddMultipleCheckboxItems() : void
{
$callable = function () {
};

$builder = new CliMenuBuilder;
$builder->disableDefaultItems();
$builder->addCheckboxItems([
['Item 1', $callable],
['Item 2', $callable]
]);
$menu = $builder->build();

$expected = [
[
'class' => CheckboxItem::class,
'text' => 'Item 1',
],
[
'class' => CheckboxItem::class,
'text' => 'Item 2',
],
];

$this->checkMenuItems($menu, $expected);
}

public function testAddCheckboxItem() : void
{
$callable = function () {
Expand Down Expand Up @@ -425,6 +452,33 @@ public function testAddRadioItem() : void
$this->checkMenuItems($menu, $expected);
}

public function testAddMultipleRadioItems() : void
{
$callable = function () {
};

$builder = new CliMenuBuilder;
$builder->disableDefaultItems();
$builder->addRadioItems([
['Item 1', $callable],
['Item 2', $callable],
]);
$menu = $builder->build();

$expected = [
[
'class' => RadioItem::class,
'text' => 'Item 1',
],
[
'class' => RadioItem::class,
'text' => 'Item 2',
],
];

$this->checkMenuItems($menu, $expected);
}

public function testAddStaticItem() : void
{

Expand Down