From cd679ce04173099ffb3debadf87012bc64d566fe Mon Sep 17 00:00:00 2001 From: Mohamed Ashraf Date: Tue, 21 Jun 2022 23:38:58 +0200 Subject: [PATCH] Excluding steps based on condition --- README.md | 9 +++++++++ src/OnboardingManager.php | 1 + src/OnboardingStep.php | 24 ++++++++++++++++++++++++ src/OnboardingSteps.php | 4 +++- tests/OnboardTest.php | 8 ++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 387779c..b7a6367 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,15 @@ $onboarding->steps()->each(function($step) { }); ``` +Excluding steps based on condition: + +```php +Onboard::addStep('Excluded Step') + ->excludeIf(function (User $model) { + return $model->isAdmin(); + }); +``` + Definining custom attributes and accessing them: ```php diff --git a/src/OnboardingManager.php b/src/OnboardingManager.php index c80b4ec..0ee9e93 100644 --- a/src/OnboardingManager.php +++ b/src/OnboardingManager.php @@ -29,6 +29,7 @@ public function finished(): bool { return $this->steps ->filter(fn (OnboardingStep $step) => $step->incomplete()) + ->filter(fn (OnboardingStep $step) => $step->notExcluded()) ->isEmpty(); } diff --git a/src/OnboardingStep.php b/src/OnboardingStep.php index 8b657f2..4bc3e84 100644 --- a/src/OnboardingStep.php +++ b/src/OnboardingStep.php @@ -9,6 +9,9 @@ class OnboardingStep { protected array $attributes = []; + /** @var callable|null */ + protected $excludeIf; + /** @var callable|null */ protected $completeIf; @@ -33,6 +36,13 @@ public function link(string $link): self return $this; } + public function excludeIf(callable $callback): self + { + $this->excludeIf = $callback; + + return $this; + } + public function completeIf(callable $callback): self { $this->completeIf = $callback; @@ -47,6 +57,20 @@ public function setModel(Onboardable $model): self return $this; } + public function excluded(): bool + { + if ($this->excludeIf && $this->model) { + return once(fn () => app()->call($this->excludeIf, ['model' => $this->model])); + } + + return false; + } + + public function notExcluded(): bool + { + return ! $this->excluded(); + } + public function complete(): bool { if ($this->completeIf && $this->model) { diff --git a/src/OnboardingSteps.php b/src/OnboardingSteps.php index 3f5de11..f7cf5a8 100644 --- a/src/OnboardingSteps.php +++ b/src/OnboardingSteps.php @@ -19,6 +19,8 @@ public function addStep(string $title): OnboardingStep public function steps(Onboardable $model): Collection { - return collect($this->steps)->map(fn (OnboardingStep $step) => $step->setModel($model)); + return collect($this->steps) + ->map(fn (OnboardingStep $step) => $step->setModel($model)) + ->filter(fn (OnboardingStep $step) => $step->notExcluded()); } } diff --git a/tests/OnboardTest.php b/tests/OnboardTest.php index 0177abd..5366833 100644 --- a/tests/OnboardTest.php +++ b/tests/OnboardTest.php @@ -48,6 +48,14 @@ return true; }); + $onboardingSteps->addStep('Excluded Step') + ->excludeIf(function () { + return true; + }) + ->completeIf(function () { + return false; + }); + $onboarding = new OnboardingManager($this->user, $onboardingSteps); expect($onboarding->finished())->toBeTrue()