File tree Expand file tree Collapse file tree 5 files changed +45
-1
lines changed Expand file tree Collapse file tree 5 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -163,6 +163,15 @@ $onboarding->steps()->each(function($step) {
163
163
});
164
164
```
165
165
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
+
166
175
Definining custom attributes and accessing them:
167
176
168
177
``` php
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ public function finished(): bool
29
29
{
30
30
return $ this ->steps
31
31
->filter (fn (OnboardingStep $ step ) => $ step ->incomplete ())
32
+ ->filter (fn (OnboardingStep $ step ) => $ step ->notExcluded ())
32
33
->isEmpty ();
33
34
}
34
35
Original file line number Diff line number Diff line change @@ -9,6 +9,9 @@ class OnboardingStep
9
9
{
10
10
protected array $ attributes = [];
11
11
12
+ /** @var callable|null */
13
+ protected $ excludeIf ;
14
+
12
15
/** @var callable|null */
13
16
protected $ completeIf ;
14
17
@@ -33,6 +36,13 @@ public function link(string $link): self
33
36
return $ this ;
34
37
}
35
38
39
+ public function excludeIf (callable $ callback ): self
40
+ {
41
+ $ this ->excludeIf = $ callback ;
42
+
43
+ return $ this ;
44
+ }
45
+
36
46
public function completeIf (callable $ callback ): self
37
47
{
38
48
$ this ->completeIf = $ callback ;
@@ -47,6 +57,20 @@ public function setModel(Onboardable $model): self
47
57
return $ this ;
48
58
}
49
59
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
+
50
74
public function complete (): bool
51
75
{
52
76
if ($ this ->completeIf && $ this ->model ) {
Original file line number Diff line number Diff line change @@ -19,6 +19,8 @@ public function addStep(string $title): OnboardingStep
19
19
20
20
public function steps (Onboardable $ model ): Collection
21
21
{
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 ());
23
25
}
24
26
}
Original file line number Diff line number Diff line change 48
48
return true ;
49
49
});
50
50
51
+ $ onboardingSteps ->addStep ('Excluded Step ' )
52
+ ->excludeIf (function () {
53
+ return true ;
54
+ })
55
+ ->completeIf (function () {
56
+ return false ;
57
+ });
58
+
51
59
$ onboarding = new OnboardingManager ($ this ->user , $ onboardingSteps );
52
60
53
61
expect ($ onboarding ->finished ())->toBeTrue ()
You can’t perform that action at this time.
0 commit comments