Skip to content

Commit

Permalink
Merge pull request #3 from MohmmedAshraf/exclude-a-step
Browse files Browse the repository at this point in the history
Excluding steps based on condition
  • Loading branch information
riasvdv authored Jun 22, 2022
2 parents f719de6 + cd679ce commit a11b8fc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
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

0 comments on commit a11b8fc

Please sign in to comment.