Skip to content

Commit cd679ce

Browse files
committed
Excluding steps based on condition
1 parent f719de6 commit cd679ce

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ $onboarding->steps()->each(function($step) {
163163
});
164164
```
165165

166+
Excluding steps based on condition:
167+
168+
```php
169+
Onboard::addStep('Excluded Step')
170+
->excludeIf(function (User $model) {
171+
return $model->isAdmin();
172+
});
173+
```
174+
166175
Definining custom attributes and accessing them:
167176

168177
```php

src/OnboardingManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function finished(): bool
2929
{
3030
return $this->steps
3131
->filter(fn (OnboardingStep $step) => $step->incomplete())
32+
->filter(fn (OnboardingStep $step) => $step->notExcluded())
3233
->isEmpty();
3334
}
3435

src/OnboardingStep.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class OnboardingStep
99
{
1010
protected array $attributes = [];
1111

12+
/** @var callable|null */
13+
protected $excludeIf;
14+
1215
/** @var callable|null */
1316
protected $completeIf;
1417

@@ -33,6 +36,13 @@ public function link(string $link): self
3336
return $this;
3437
}
3538

39+
public function excludeIf(callable $callback): self
40+
{
41+
$this->excludeIf = $callback;
42+
43+
return $this;
44+
}
45+
3646
public function completeIf(callable $callback): self
3747
{
3848
$this->completeIf = $callback;
@@ -47,6 +57,20 @@ public function setModel(Onboardable $model): self
4757
return $this;
4858
}
4959

60+
public function excluded(): bool
61+
{
62+
if ($this->excludeIf && $this->model) {
63+
return once(fn () => app()->call($this->excludeIf, ['model' => $this->model]));
64+
}
65+
66+
return false;
67+
}
68+
69+
public function notExcluded(): bool
70+
{
71+
return ! $this->excluded();
72+
}
73+
5074
public function complete(): bool
5175
{
5276
if ($this->completeIf && $this->model) {

src/OnboardingSteps.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public function addStep(string $title): OnboardingStep
1919

2020
public function steps(Onboardable $model): Collection
2121
{
22-
return collect($this->steps)->map(fn (OnboardingStep $step) => $step->setModel($model));
22+
return collect($this->steps)
23+
->map(fn (OnboardingStep $step) => $step->setModel($model))
24+
->filter(fn (OnboardingStep $step) => $step->notExcluded());
2325
}
2426
}

tests/OnboardTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848
return true;
4949
});
5050

51+
$onboardingSteps->addStep('Excluded Step')
52+
->excludeIf(function () {
53+
return true;
54+
})
55+
->completeIf(function () {
56+
return false;
57+
});
58+
5159
$onboarding = new OnboardingManager($this->user, $onboardingSteps);
5260

5361
expect($onboarding->finished())->toBeTrue()

0 commit comments

Comments
 (0)