From 6316ae6e3f60617b9f190e7a61dc1509af91c166 Mon Sep 17 00:00:00 2001 From: talelmishali Date: Wed, 17 Aug 2022 04:14:15 -0400 Subject: [PATCH] Add callable attributes support (#12) --- src/OnboardingStep.php | 31 +++++++++++++++++++++++++++++-- src/OnboardingSteps.php | 2 +- tests/OnboardTest.php | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/OnboardingStep.php b/src/OnboardingStep.php index 4d11b1b..6c99b16 100644 --- a/src/OnboardingStep.php +++ b/src/OnboardingStep.php @@ -10,6 +10,9 @@ class OnboardingStep implements Arrayable { protected array $attributes = []; + /** @var callable|null */ + protected $callableAttributes; + /** @var callable|null */ protected $excludeIf; @@ -58,6 +61,24 @@ public function setModel(Onboardable $model): self return $this; } + public function setCallableAttributes(): void + { + if (is_null($this->callableAttributes)) { + return; + } + + $this->attributes(once(fn () => app()->call($this->callableAttributes, ['model' => $this->model]))); + } + + public function initiate(Onboardable $model): self + { + $this->setModel($model); + + $this->setCallableAttributes(); + + return $this; + } + public function excluded(): bool { if ($this->excludeIf && $this->model) { @@ -91,9 +112,15 @@ public function attribute(string $key, mixed $default = null): mixed return Arr::get($this->attributes, $key, $default); } - public function attributes(array $attributes): self + public function attributes(array|callable $attributes): self { - $this->attributes = array_merge($this->attributes, $attributes); + if (is_callable($attributes)) { + $this->callableAttributes = $attributes; + } + + if (is_array($attributes)) { + $this->attributes = array_merge($this->attributes, $attributes); + } return $this; } diff --git a/src/OnboardingSteps.php b/src/OnboardingSteps.php index f7cf5a8..fa52d6a 100644 --- a/src/OnboardingSteps.php +++ b/src/OnboardingSteps.php @@ -20,7 +20,7 @@ public function addStep(string $title): OnboardingStep public function steps(Onboardable $model): Collection { return collect($this->steps) - ->map(fn (OnboardingStep $step) => $step->setModel($model)) + ->map(fn (OnboardingStep $step) => $step->initiate($model)) ->filter(fn (OnboardingStep $step) => $step->notExcluded()); } } diff --git a/tests/OnboardTest.php b/tests/OnboardTest.php index 5366833..1e05c97 100644 --- a/tests/OnboardTest.php +++ b/tests/OnboardTest.php @@ -181,3 +181,26 @@ expect($called)->toBe(1) ->and($onboarding->finished())->toBeFalse(); }); + +test('step attrbiutes can be callable', function () { + $this->user->name = fake()->name; + + $onboardingSteps = new OnboardingSteps(); + $onboardingSteps->addStep('Step 1') + ->link('/some/url') + ->cta('Test This!') + ->attributes(function (User $model) { + return [ + 'user_name' => $model->name, + ]; + }); + + $onboarding = new OnboardingManager($this->user, $onboardingSteps); + + $step = $onboarding->steps()->first(); + + expect($step) + ->user_name->not->toBeNull() + ->user_name->toBe($this->user->name) + ->title->tobe('Step 1'); +});