From a8409793dfea56020ea11c4e8cec572a0fc994ae Mon Sep 17 00:00:00 2001 From: Jack W-H Date: Sat, 18 Apr 2020 21:43:34 +0100 Subject: [PATCH 1/7] Allow passing in callable disabled properties --- src/Builder/CliMenuBuilder.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Builder/CliMenuBuilder.php b/src/Builder/CliMenuBuilder.php index 03a5b5a3..f7207157 100644 --- a/src/Builder/CliMenuBuilder.php +++ b/src/Builder/CliMenuBuilder.php @@ -127,7 +127,7 @@ public function addItem( string $text, callable $itemCallable, bool $showItemExtra = false, - bool $disabled = false + $disabled = false ) : self { $this->addMenuItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); @@ -147,7 +147,7 @@ public function addCheckboxItem( string $text, callable $itemCallable, bool $showItemExtra = false, - bool $disabled = false + $disabled = false ) : self { $this->addMenuItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)); @@ -158,7 +158,7 @@ public function addRadioItem( string $text, callable $itemCallable, bool $showItemExtra = false, - bool $disabled = false + $disabled = false ) : self { $this->addMenuItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled)); From 8e78863ab0f23bea60be75be14e5848e603a8cad Mon Sep 17 00:00:00 2001 From: Jack W-H Date: Sat, 18 Apr 2020 21:49:20 +0100 Subject: [PATCH 2/7] Allow callbacks to enable/disable a selectable item --- src/MenuItem/SelectableItem.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/MenuItem/SelectableItem.php b/src/MenuItem/SelectableItem.php index 72eef892..56075ea3 100644 --- a/src/MenuItem/SelectableItem.php +++ b/src/MenuItem/SelectableItem.php @@ -42,7 +42,7 @@ public function __construct( string $text, callable $selectAction, bool $showItemExtra = false, - bool $disabled = false + $disabled = false ) { $this->text = $text; $this->selectAction = $selectAction; @@ -57,7 +57,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()); } /** @@ -89,7 +89,9 @@ public function getSelectAction() : ?callable */ public function canSelect() : bool { - return !$this->disabled; + return is_callable($this->disabled) + ? (! call_user_func($this->disabled)) + : (! $this->disabled); } /** From 7a30992050e46ac598dd82c2cec57721dda5e7e0 Mon Sep 17 00:00:00 2001 From: Jack W-H Date: Sat, 18 Apr 2020 21:51:15 +0100 Subject: [PATCH 3/7] Allow callbacks to enable/disable a checkbox item --- src/MenuItem/CheckboxItem.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/MenuItem/CheckboxItem.php b/src/MenuItem/CheckboxItem.php index d094f600..7f968940 100644 --- a/src/MenuItem/CheckboxItem.php +++ b/src/MenuItem/CheckboxItem.php @@ -45,7 +45,7 @@ public function __construct( string $text, callable $selectAction, bool $showItemExtra = false, - bool $disabled = false + $disabled = false ) { $this->text = $text; $this->selectAction = $selectAction; @@ -60,7 +60,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()); } /** @@ -97,7 +97,9 @@ public function getSelectAction() : ?callable */ public function canSelect() : bool { - return !$this->disabled; + return is_callable($this->disabled) + ? (! call_user_func($this->disabled)) + : (! $this->disabled); } /** From 9703596beee5034a14d01b54f93b6a14d2ed1939 Mon Sep 17 00:00:00 2001 From: Jack W-H Date: Sat, 18 Apr 2020 21:53:23 +0100 Subject: [PATCH 4/7] Allow callbacks to enable/disable a radio item --- src/MenuItem/RadioItem.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/MenuItem/RadioItem.php b/src/MenuItem/RadioItem.php index 26745ea5..6a724c14 100644 --- a/src/MenuItem/RadioItem.php +++ b/src/MenuItem/RadioItem.php @@ -45,7 +45,7 @@ public function __construct( string $text, callable $selectAction, bool $showItemExtra = false, - bool $disabled = false + $disabled = false ) { $this->text = $text; $this->selectAction = $selectAction; @@ -60,7 +60,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()); } /** @@ -117,7 +117,9 @@ function (RadioItem $item) { */ public function canSelect() : bool { - return !$this->disabled; + return is_callable($this->disabled) + ? (! call_user_func($this->disabled)) + : (! $this->disabled); } /** From d549232d5b30723ed10a33ea62be1de478be28f8 Mon Sep 17 00:00:00 2001 From: Jack W-H Date: Sat, 18 Apr 2020 22:56:10 +0100 Subject: [PATCH 5/7] Added a callable enable/disable example to the docs --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 4e386357..4bb94518 100644 --- a/README.md +++ b/README.md @@ -791,6 +791,13 @@ $menu = (new CliMenuBuilder) ->addItem('Nope can\'t see this!', $itemCallable) ->disableMenu(); }) + ->setTitle('Basic CLI Menu Disabled Items > Dynamic Submenu') + ->addItem( + 'You can only select this item at the weekend!', + $itemCallable, + false, + fn() => (date('N') < 6) + ) ->addLineBreak('-') ->build(); ``` From ac2a06a55bc9c23777542a9ea4e65f75f2852e6f Mon Sep 17 00:00:00 2001 From: Jack W-H Date: Sat, 18 Apr 2020 22:56:59 +0100 Subject: [PATCH 6/7] Corrected a typo --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 4bb94518..781108d4 100644 --- a/README.md +++ b/README.md @@ -791,7 +791,6 @@ $menu = (new CliMenuBuilder) ->addItem('Nope can\'t see this!', $itemCallable) ->disableMenu(); }) - ->setTitle('Basic CLI Menu Disabled Items > Dynamic Submenu') ->addItem( 'You can only select this item at the weekend!', $itemCallable, From b9df9f637125949071d7139dadfc680d8b6fc6b2 Mon Sep 17 00:00:00 2001 From: Jack Webb-Heller Date: Sun, 19 Apr 2020 14:11:35 +0100 Subject: [PATCH 7/7] Updated docblocks --- src/Builder/CliMenuBuilder.php | 30 ++++++++++++++++++++++++++++++ src/MenuItem/CheckboxItem.php | 12 +++++++++++- src/MenuItem/RadioItem.php | 12 +++++++++++- src/MenuItem/SelectableItem.php | 12 +++++++++++- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/Builder/CliMenuBuilder.php b/src/Builder/CliMenuBuilder.php index f7207157..e7c60a88 100644 --- a/src/Builder/CliMenuBuilder.php +++ b/src/Builder/CliMenuBuilder.php @@ -123,6 +123,16 @@ 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, @@ -143,6 +153,16 @@ 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, @@ -154,6 +174,16 @@ public function addCheckboxItem( 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, diff --git a/src/MenuItem/CheckboxItem.php b/src/MenuItem/CheckboxItem.php index 7f968940..8dcfe8fb 100644 --- a/src/MenuItem/CheckboxItem.php +++ b/src/MenuItem/CheckboxItem.php @@ -25,7 +25,7 @@ class CheckboxItem implements MenuItemInterface private $showItemExtra; /** - * @var bool + * @var bool|callable */ private $disabled; @@ -41,6 +41,16 @@ 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, diff --git a/src/MenuItem/RadioItem.php b/src/MenuItem/RadioItem.php index 6a724c14..4284ba41 100644 --- a/src/MenuItem/RadioItem.php +++ b/src/MenuItem/RadioItem.php @@ -25,7 +25,7 @@ class RadioItem implements MenuItemInterface private $showItemExtra; /** - * @var bool + * @var bool|callable */ private $disabled; @@ -41,6 +41,16 @@ 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, diff --git a/src/MenuItem/SelectableItem.php b/src/MenuItem/SelectableItem.php index 56075ea3..cdefc398 100644 --- a/src/MenuItem/SelectableItem.php +++ b/src/MenuItem/SelectableItem.php @@ -29,7 +29,7 @@ class SelectableItem implements MenuItemInterface private $showItemExtra; /** - * @var bool + * @var bool|callable */ private $disabled; @@ -38,6 +38,16 @@ 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,