-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathIsWizardCommand.php
More file actions
119 lines (100 loc) · 3.77 KB
/
Copy pathIsWizardCommand.php
File metadata and controls
119 lines (100 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Code16\Sharp\EntityList\Commands\Wizards;
use Code16\Sharp\Enums\CommandAction;
use Code16\Sharp\Exceptions\SharpMethodNotImplementedException;
use Code16\Sharp\Form\Layout\FormLayoutColumn;
use Code16\Sharp\Utils\Fields\FieldsContainer;
use Illuminate\Support\Str;
trait IsWizardCommand
{
protected ?WizardCommandContext $wizardCommandContext = null;
private string $key;
private array $formsBuilt = [];
protected function getWizardContext(): WizardCommandContext
{
if (! $this->wizardCommandContext) {
$this->wizardCommandContext = session()->get(sprintf('CWC.%s.%s', get_class($this), $this->getKey()));
if (! $this->wizardCommandContext) {
$this->wizardCommandContext = new WizardCommandContext();
}
}
return $this->wizardCommandContext;
}
protected function toStep(string $step): array
{
if ($this->wizardCommandContext) {
session()->put(sprintf('CWC.%s.%s', get_class($this), $this->getKey()), $this->wizardCommandContext);
}
return [
'action' => CommandAction::Step->value,
'step' => "{$step}:{$this->getKey()}",
];
}
public function extractStepFromRequest(): ?string
{
if ($step = request()->input('command_step')) {
[$step, $this->key] = explode(':', $step);
return $step;
}
return null;
}
protected function isStep(string $step): bool
{
return $this->extractStepFromRequest() == $step;
}
protected function getKey(): string
{
if (! isset($this->key)) {
$this->key = Str::random(5);
}
return $this->key;
}
public function buildFormFields(FieldsContainer $formFields): void
{
if (! $step = $this->extractStepFromRequest()) {
$this->buildFormFieldsForFirstStep($formFields);
} else {
$methodName = 'buildFormFieldsForStep'.Str::ucfirst(Str::camel($step));
if (method_exists($this, $methodName)) {
$this->$methodName($formFields);
} else {
$this->buildFormFieldsForStep($step, $formFields);
}
}
}
protected function buildFormFieldsForStep(string $step, FieldsContainer $formFields): void
{
// You can either implement this method and test $step (quick for small commands)
// or leave this and implement for each step buildFormFieldsForStepXXX
// where XXX is the camel cased name of your step
throw new SharpMethodNotImplementedException();
}
public function buildFormLayout(FormLayoutColumn &$column): void
{
if (! $step = $this->extractStepFromRequest()) {
$this->buildFormLayoutForFirstStep($column);
} else {
$methodName = 'buildFormLayoutForStep'.Str::ucfirst(Str::camel($step));
if (method_exists($this, $methodName)) {
$this->$methodName($column);
} else {
$this->buildFormLayoutForStep($step, $column);
}
}
}
protected function buildFormLayoutForFirstStep(FormLayoutColumn &$column): void {}
protected function buildFormLayoutForStep(string $step, FormLayoutColumn &$column): void {}
/**
* Override the default checkFormIsBuilt() in Wizard case, to check the form
* given the current step.
*/
protected function checkFormIsBuilt(): void
{
$step = $this->extractStepFromRequest() ?: '##first_step##';
if (! ($this->formsBuilt[$step] ?? false)) {
$this->fieldsContainer = null;
$this->buildFormFields($this->fieldsContainer());
$this->formsBuilt[$step] = count($this->fieldsContainer()->getFields()) > 0;
}
}
}