Skip to content

Commit 6316ae6

Browse files
authored
Add callable attributes support (#12)
1 parent b85edbb commit 6316ae6

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/OnboardingStep.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class OnboardingStep implements Arrayable
1010
{
1111
protected array $attributes = [];
1212

13+
/** @var callable|null */
14+
protected $callableAttributes;
15+
1316
/** @var callable|null */
1417
protected $excludeIf;
1518

@@ -58,6 +61,24 @@ public function setModel(Onboardable $model): self
5861
return $this;
5962
}
6063

64+
public function setCallableAttributes(): void
65+
{
66+
if (is_null($this->callableAttributes)) {
67+
return;
68+
}
69+
70+
$this->attributes(once(fn () => app()->call($this->callableAttributes, ['model' => $this->model])));
71+
}
72+
73+
public function initiate(Onboardable $model): self
74+
{
75+
$this->setModel($model);
76+
77+
$this->setCallableAttributes();
78+
79+
return $this;
80+
}
81+
6182
public function excluded(): bool
6283
{
6384
if ($this->excludeIf && $this->model) {
@@ -91,9 +112,15 @@ public function attribute(string $key, mixed $default = null): mixed
91112
return Arr::get($this->attributes, $key, $default);
92113
}
93114

94-
public function attributes(array $attributes): self
115+
public function attributes(array|callable $attributes): self
95116
{
96-
$this->attributes = array_merge($this->attributes, $attributes);
117+
if (is_callable($attributes)) {
118+
$this->callableAttributes = $attributes;
119+
}
120+
121+
if (is_array($attributes)) {
122+
$this->attributes = array_merge($this->attributes, $attributes);
123+
}
97124

98125
return $this;
99126
}

src/OnboardingSteps.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function addStep(string $title): OnboardingStep
2020
public function steps(Onboardable $model): Collection
2121
{
2222
return collect($this->steps)
23-
->map(fn (OnboardingStep $step) => $step->setModel($model))
23+
->map(fn (OnboardingStep $step) => $step->initiate($model))
2424
->filter(fn (OnboardingStep $step) => $step->notExcluded());
2525
}
2626
}

tests/OnboardTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,26 @@
181181
expect($called)->toBe(1)
182182
->and($onboarding->finished())->toBeFalse();
183183
});
184+
185+
test('step attrbiutes can be callable', function () {
186+
$this->user->name = fake()->name;
187+
188+
$onboardingSteps = new OnboardingSteps();
189+
$onboardingSteps->addStep('Step 1')
190+
->link('/some/url')
191+
->cta('Test This!')
192+
->attributes(function (User $model) {
193+
return [
194+
'user_name' => $model->name,
195+
];
196+
});
197+
198+
$onboarding = new OnboardingManager($this->user, $onboardingSteps);
199+
200+
$step = $onboarding->steps()->first();
201+
202+
expect($step)
203+
->user_name->not->toBeNull()
204+
->user_name->toBe($this->user->name)
205+
->title->tobe('Step 1');
206+
});

0 commit comments

Comments
 (0)