Skip to content

Allow the use of callables to determine whether a menu item is disabled or not #237

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

Closed
wants to merge 7 commits into from
Closed
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,12 @@ $menu = (new CliMenuBuilder)
->addItem('Nope can\'t see this!', $itemCallable)
->disableMenu();
})
->addItem(
'You can only select this item at the weekend!',
$itemCallable,
false,
fn() => (date('N') < 6)
)
->addLineBreak('-')
->build();
```
Expand Down
36 changes: 33 additions & 3 deletions src/Builder/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,21 @@ public function addMenuItem(MenuItemInterface $item) : self
return $this;
}

/**
* Add an item to the menu.
*
* @param string $text
* @param callable $itemCallable
* @param bool $showItemExtra
* @param bool|callable $disabled
*
* @return $this
*/
public function addItem(
string $text,
callable $itemCallable,
bool $showItemExtra = false,
bool $disabled = false
$disabled = false
) : self {
$this->addMenuItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled));

Expand All @@ -143,22 +153,42 @@ public function addItems(array $items) : self
return $this;
}

/**
* Add a checkbox to the menu.
*
* @param string $text
* @param callable $itemCallable
* @param bool $showItemExtra
* @param bool|callable $disabled
*
* @return $this
*/
public function addCheckboxItem(
string $text,
callable $itemCallable,
bool $showItemExtra = false,
bool $disabled = false
$disabled = false
) : self {
$this->addMenuItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled));

return $this;
}

/**
* Add a radio button to the menu.
*
* @param string $text
* @param callable $itemCallable
* @param bool $showItemExtra
* @param bool|callable $disabled
*
* @return $this
*/
public function addRadioItem(
string $text,
callable $itemCallable,
bool $showItemExtra = false,
bool $disabled = false
$disabled = false
) : self {
$this->addMenuItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled));

Expand Down
20 changes: 16 additions & 4 deletions src/MenuItem/CheckboxItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CheckboxItem implements MenuItemInterface
private $showItemExtra;

/**
* @var bool
* @var bool|callable
*/
private $disabled;

Expand All @@ -41,11 +41,21 @@ class CheckboxItem implements MenuItemInterface
*/
private $style;

/**
* CheckboxItem constructor.
*
* @param string $text
* @param callable $selectAction
* @param bool $showItemExtra
* @param bool|callable $disabled
*
* @return void
*/
public function __construct(
string $text,
callable $selectAction,
bool $showItemExtra = false,
bool $disabled = false
$disabled = false
) {
$this->text = $text;
$this->selectAction = $selectAction;
Expand All @@ -60,7 +70,7 @@ public function __construct(
*/
public function getRows(MenuStyle $style, bool $selected = false) : array
{
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
return (new SelectableItemRenderer())->render($style, $this, $selected, ! $this->canSelect());
}

/**
Expand Down Expand Up @@ -97,7 +107,9 @@ public function getSelectAction() : ?callable
*/
public function canSelect() : bool
{
return !$this->disabled;
return is_callable($this->disabled)
? (! call_user_func($this->disabled))
: (! $this->disabled);
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/MenuItem/RadioItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RadioItem implements MenuItemInterface
private $showItemExtra;

/**
* @var bool
* @var bool|callable
*/
private $disabled;

Expand All @@ -41,11 +41,21 @@ class RadioItem implements MenuItemInterface
*/
private $style;

/**
* RadioItem constructor.
*
* @param string $text
* @param callable $selectAction
* @param bool $showItemExtra
* @param bool|callable $disabled
*
* @return void
*/
public function __construct(
string $text,
callable $selectAction,
bool $showItemExtra = false,
bool $disabled = false
$disabled = false
) {
$this->text = $text;
$this->selectAction = $selectAction;
Expand All @@ -60,7 +70,7 @@ public function __construct(
*/
public function getRows(MenuStyle $style, bool $selected = false) : array
{
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
return (new SelectableItemRenderer())->render($style, $this, $selected, ! $this->canSelect());
}

/**
Expand Down Expand Up @@ -117,7 +127,9 @@ function (RadioItem $item) {
*/
public function canSelect() : bool
{
return !$this->disabled;
return is_callable($this->disabled)
? (! call_user_func($this->disabled))
: (! $this->disabled);
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/MenuItem/SelectableItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SelectableItem implements MenuItemInterface
private $showItemExtra;

/**
* @var bool
* @var bool|callable
*/
private $disabled;

Expand All @@ -38,11 +38,21 @@ class SelectableItem implements MenuItemInterface
*/
private $style;

/**
* SelectableItem constructor.
*
* @param string $text
* @param callable $selectAction
* @param bool $showItemExtra
* @param bool|callable $disabled
*
* @return void
*/
public function __construct(
string $text,
callable $selectAction,
bool $showItemExtra = false,
bool $disabled = false
$disabled = false
) {
$this->text = $text;
$this->selectAction = $selectAction;
Expand All @@ -57,7 +67,7 @@ public function __construct(
*/
public function getRows(MenuStyle $style, bool $selected = false) : array
{
return (new SelectableItemRenderer())->render($style, $this, $selected, $this->disabled);
return (new SelectableItemRenderer())->render($style, $this, $selected, ! $this->canSelect());
}

/**
Expand Down Expand Up @@ -89,7 +99,9 @@ public function getSelectAction() : ?callable
*/
public function canSelect() : bool
{
return !$this->disabled;
return is_callable($this->disabled)
? (! call_user_func($this->disabled))
: (! $this->disabled);
}

/**
Expand Down