Skip to content
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

Excluding steps based on condition #3

Merged
merged 1 commit into from
Jun 22, 2022
Merged
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/OnboardingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function finished(): bool
{
return $this->steps
->filter(fn (OnboardingStep $step) => $step->incomplete())
->filter(fn (OnboardingStep $step) => $step->notExcluded())
->isEmpty();
}

Expand Down
24 changes: 24 additions & 0 deletions src/OnboardingStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class OnboardingStep
{
protected array $attributes = [];

/** @var callable|null */
protected $excludeIf;

/** @var callable|null */
protected $completeIf;

Expand All @@ -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;
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/OnboardingSteps.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
8 changes: 8 additions & 0 deletions tests/OnboardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down