Skip to content

Commit

Permalink
feat: pass wizard to prepare payload function
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinrabe committed Feb 22, 2022
1 parent 60d79c4 commit a798c87
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class OrderWizardController extends WizardController
// Prepare a payload. This payload will be available in $wizard->payload on each step. It will be saved
// automatically after each step{number}Submit method. It is used to store each partial result until onFinish.
// The simplest payload is an array. But you could use anything you want. It only needs to be serializable!
public function preparePayload(Request $request): array
public function preparePayload(Request $request, Wizard $wizard): array
{
return [];
}
Expand Down
9 changes: 8 additions & 1 deletion src/Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ class Wizard

public int $maxSteps = 0;

public function __construct(public mixed $payload)
public mixed $payload = null;

public function __construct()
{
$this->id = (string) Str::uuid();
}

public function setPayload(mixed $payload): self {
$this->payload = $payload;
return $this;
}
}
8 changes: 5 additions & 3 deletions src/WizardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public function __invoke(Request $request, string $id = null, int $step = null):
{
// Prepare new Wizard
if ($id === null) {
$wizard = new Wizard($this->preparePayload($request));
$wizard = new Wizard();
$payload = $this->preparePayload($request, $wizard);
$wizard->setPayload($payload);
$this->repository->save($wizard);
return $this->redirect($wizard);
}
Expand Down Expand Up @@ -66,7 +68,7 @@ protected function handlePost(Request $request, Wizard $wizard, int $step): mixe
$this->{$method}($request, $wizard);
}

$wizard->step = $step+1;
$wizard->step = $step + 1;

if (method_exists($this, 'step'.$wizard->step)) {
$this->repository->save($wizard);
Expand All @@ -79,7 +81,7 @@ protected function handlePost(Request $request, Wizard $wizard, int $step): mixe
}
}

abstract function preparePayload(Request $request): mixed;
abstract function preparePayload(Request $request, Wizard $wizard): mixed;

abstract function onFinish(Request $request, Wizard $wizard): mixed;
}

0 comments on commit a798c87

Please sign in to comment.