From 24788afcb1065636883132e18a3b5e4536993047 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Sat, 8 Apr 2023 16:38:09 +0200 Subject: [PATCH 1/4] Make `->setExactActive()` accept a callable as well --- src/Traits/Activatable.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Traits/Activatable.php b/src/Traits/Activatable.php index 8344e07..ee11907 100644 --- a/src/Traits/Activatable.php +++ b/src/Traits/Activatable.php @@ -73,13 +73,15 @@ public function determineActiveForUrl(string $url, string $root = '/'): void /** * Set if current Activatable should be marked as an exact url match. - * - * @param bool $exactActive - * - * @return $this */ - public function setExactActive(bool $exactActive = true): static + public function setExactActive(bool |callable $exactActive = true): static { + if (is_callable($exactActive)) { + $this->exactActive = $exactActive($this); + + return $this; + } + $this->exactActive = $exactActive; return $this; From e903ec68af35eac6d023a9a4f8c5474183939c27 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Sat, 8 Apr 2023 16:39:42 +0200 Subject: [PATCH 2/4] Add test --- tests/Traits/ActivatableTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Traits/ActivatableTest.php b/tests/Traits/ActivatableTest.php index 1d4ca38..00d5108 100644 --- a/tests/Traits/ActivatableTest.php +++ b/tests/Traits/ActivatableTest.php @@ -26,3 +26,9 @@ return false; })->isActive())->toBeFalse(); }); + +it('can be set exact active via a callable', function () { + expect($this->activatable->setExactActive(function () { + return false; + })->isExactActive())->toBeFalse(); +}); From b9fa03b744c0e718c47a8bc2098a1858d1a35fa9 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Sat, 8 Apr 2023 16:41:25 +0200 Subject: [PATCH 3/4] Fix code style --- src/Traits/Activatable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/Activatable.php b/src/Traits/Activatable.php index ee11907..d6d8f59 100644 --- a/src/Traits/Activatable.php +++ b/src/Traits/Activatable.php @@ -74,7 +74,7 @@ public function determineActiveForUrl(string $url, string $root = '/'): void /** * Set if current Activatable should be marked as an exact url match. */ - public function setExactActive(bool |callable $exactActive = true): static + public function setExactActive(bool | callable $exactActive = true): static { if (is_callable($exactActive)) { $this->exactActive = $exactActive($this); From 8080307e493f6f7976511eac647615c162a371ed Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Wed, 12 Apr 2023 12:01:30 +0200 Subject: [PATCH 4/4] simplify code --- src/Traits/Activatable.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Traits/Activatable.php b/src/Traits/Activatable.php index d6d8f59..4d92462 100644 --- a/src/Traits/Activatable.php +++ b/src/Traits/Activatable.php @@ -22,9 +22,7 @@ public function isActive(): bool public function setActive(bool | callable $active = true): static { if (is_callable($active)) { - $this->active = $active($this); - - return $this; + $active = (bool)$active($this); } $this->active = $active;