From dee64db254c1c299c5837d7de3069bfb69a03007 Mon Sep 17 00:00:00 2001 From: MartkCz Date: Thu, 26 Aug 2021 13:31:16 +0200 Subject: [PATCH 1/4] standalone version --- src/Facade.php | 24 ++++++++++++++++++++++++ src/IWizard.php | 4 ++-- src/Wizard.php | 17 +++++++++++++---- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/Facade.php b/src/Facade.php index 7622856..7605a07 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -3,6 +3,8 @@ namespace Contributte\FormWizard; use Closure; +use LogicException; +use Nette\Application\UI\Form as UIForm; use Nette\Forms\Form; use Nette\SmartObject; use Nette\Utils\ArrayHash; @@ -108,6 +110,28 @@ public function isDisabled(int $step): bool return !$this->wizard->getSteps()[$step]; } + public function attached(): void + { + if ($this->wizard instanceof Wizard) { + $this->wizard->callStartup(); + } + + $form = $this->wizard->create(null, false); + + if ($form instanceof UIForm) { + throw new LogicException(sprintf('Cannot call this method on %s.', UIForm::class)); + } + + if ($form->isSubmitted()) { + $form->fireEvents(); + } + } + + public function renderCurrentComponent(): string + { + return (string) $this->getCurrentComponent(); + } + /** * @return mixed */ diff --git a/src/IWizard.php b/src/IWizard.php index 972df4a..56820f4 100644 --- a/src/IWizard.php +++ b/src/IWizard.php @@ -2,7 +2,7 @@ namespace Contributte\FormWizard; -use Nette\Application\UI\Form; +use Nette\Forms\Form; use Nette\Utils\ArrayHash; interface IWizard @@ -26,7 +26,7 @@ public function render(): void; public function reset(): void; - public function create(?string $step = null): Form; + public function create(?string $step = null, bool $defaultValues = true): Form; public function isSuccess(): bool; diff --git a/src/Wizard.php b/src/Wizard.php index eb027ab..8fda846 100644 --- a/src/Wizard.php +++ b/src/Wizard.php @@ -120,7 +120,6 @@ protected function getSection(): WizardSessionSection public function getStepCounter(): StepCounter { - $counter = 1; if ($this->stepCounter === null) { for ($counter = 1; $counter < 1000; $counter++) { if (!method_exists($this, 'createStep' . $counter) && !$this->getComponent('step' . $counter, false)) { @@ -278,7 +277,7 @@ public function reset(): void $this->stepCounter = null; } - public function create(?string $step = null): Form + public function create(?string $step = null, bool $defaultValues = true): Forms\Form { $step = (int) ($step ?? $this->getCurrentStep()); /** @var Form $form */ @@ -291,7 +290,9 @@ public function create(?string $step = null): Form } // Set submited values - $form->setValues((array) $this->getSection()->getStepValues($step)); + if ($defaultValues) { + $form->setValues((array) $this->getSection()->getStepValues($step)); + } return $form; } @@ -380,7 +381,10 @@ public function getPresenter(): ?Presenter return $this->presenter; } - protected function validateParent(IContainer $parent): void + /** + * @internal only for standalone usage, use Facade instead of + */ + public function callStartup(): void { if (!$this->startupCalled) { $this->startupCalled = true; @@ -389,4 +393,9 @@ protected function validateParent(IContainer $parent): void } } + protected function validateParent(IContainer $parent): void + { + $this->callStartup(); + } + } From b6c322234b84435e53b1fb1a9ad963b0efe5cd3f Mon Sep 17 00:00:00 2001 From: MartkCz Date: Thu, 26 Aug 2021 13:41:25 +0200 Subject: [PATCH 2/4] minimum-stability is stable --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bc5f556..692d509 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "phpstan/phpstan-strict-rules": "^0.12", "webchemistry/testing-helpers": "^3.0.0" }, - "minimum-stability": "dev", + "minimum-stability": "stable", "autoload": { "psr-4": { "Contributte\\FormWizard\\": [ From 8daddd63036ea4fcdcf8ba222911164b9390011d Mon Sep 17 00:00:00 2001 From: MartkCz Date: Thu, 26 Aug 2021 13:41:33 +0200 Subject: [PATCH 3/4] fix qa --- src/Facade.php | 3 +- src/Wizard.php | 3 +- standalone.php | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 standalone.php diff --git a/src/Facade.php b/src/Facade.php index 7605a07..9cb5b72 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -122,7 +122,8 @@ public function attached(): void throw new LogicException(sprintf('Cannot call this method on %s.', UIForm::class)); } - if ($form->isSubmitted()) { + $submitted = (bool) $form->isSubmitted(); + if ($submitted) { $form->fireEvents(); } } diff --git a/src/Wizard.php b/src/Wizard.php index 8fda846..db6e314 100644 --- a/src/Wizard.php +++ b/src/Wizard.php @@ -121,7 +121,8 @@ protected function getSection(): WizardSessionSection public function getStepCounter(): StepCounter { if ($this->stepCounter === null) { - for ($counter = 1; $counter < 1000; $counter++) { + $counter = 1; + for (; $counter < 1000; $counter++) { if (!method_exists($this, 'createStep' . $counter) && !$this->getComponent('step' . $counter, false)) { $counter--; break; diff --git a/standalone.php b/standalone.php new file mode 100644 index 0000000..af7a6e6 --- /dev/null +++ b/standalone.php @@ -0,0 +1,104 @@ + "Skip username", + 2 => "Username", + 3 => "Email", + ]; + + protected function finish(): void + { + $values = $this->getValues(); + + var_dump($values); + } + + protected function startup(): void + { + $this->skipStepIf(2, function (array $values): bool { + return isset($values[1]) && $values[1]['skip'] === true; + }); + $this->setDefaultValues(2, function (Form $form, array $values) { + $data = [ + 'username' => 'john_doe' + ]; + $form->setValues($data); + }); + } + + public function getStepData(int $step): array + { + return [ + 'name' => $this->stepNames[$step] + ]; + } + + protected function createStep1(): Form + { + $form = new Form(); + + $form->addCheckbox('skip', 'Skip username'); + + $form->addSubmit(self::NEXT_SUBMIT_NAME, 'Next'); + + return $form; + } + + protected function createStep2(): Form + { + $form = new Form(); + + $form->addText('username', 'Username') + ->setRequired(); + + $form->addSubmit(self::PREV_SUBMIT_NAME, 'Back'); + $form->addSubmit(self::NEXT_SUBMIT_NAME, 'Next'); + + return $form; + } + + protected function createStep3(): Form + { + $form = new Form(); + + $form->addText('email', 'Email') + ->setRequired(); + + $form->addSubmit(self::PREV_SUBMIT_NAME, 'Back'); + $form->addSubmit(self::FINISH_SUBMIT_NAME, 'Register'); + + return $form; + } +} + +$requestFactory = new RequestFactory(); +$httpRequest = $requestFactory->fromGlobals(); +$httpResponse = new Response(); + +$session = new Session($httpRequest, $httpResponse); +$session->start(); + +$facade = new Facade(new Wizard($session)); +$facade->attached(); + +if ($facade->isSuccess()) { + var_dump($facade->getValues()); + + echo "Wizard success\n"; +} elseif ($facade->getCurrentStep() === 1) { + // first step + echo $facade->renderCurrentComponent(); +} else { + // other steps + echo $facade->renderCurrentComponent(); +} From c9a5b8727dec9c8467ff59cdf8f4c576c244bf4e Mon Sep 17 00:00:00 2001 From: MartkCz Date: Thu, 26 Aug 2021 13:44:01 +0200 Subject: [PATCH 4/4] remove file --- standalone.php | 104 ------------------------------------------------- 1 file changed, 104 deletions(-) delete mode 100644 standalone.php diff --git a/standalone.php b/standalone.php deleted file mode 100644 index af7a6e6..0000000 --- a/standalone.php +++ /dev/null @@ -1,104 +0,0 @@ - "Skip username", - 2 => "Username", - 3 => "Email", - ]; - - protected function finish(): void - { - $values = $this->getValues(); - - var_dump($values); - } - - protected function startup(): void - { - $this->skipStepIf(2, function (array $values): bool { - return isset($values[1]) && $values[1]['skip'] === true; - }); - $this->setDefaultValues(2, function (Form $form, array $values) { - $data = [ - 'username' => 'john_doe' - ]; - $form->setValues($data); - }); - } - - public function getStepData(int $step): array - { - return [ - 'name' => $this->stepNames[$step] - ]; - } - - protected function createStep1(): Form - { - $form = new Form(); - - $form->addCheckbox('skip', 'Skip username'); - - $form->addSubmit(self::NEXT_SUBMIT_NAME, 'Next'); - - return $form; - } - - protected function createStep2(): Form - { - $form = new Form(); - - $form->addText('username', 'Username') - ->setRequired(); - - $form->addSubmit(self::PREV_SUBMIT_NAME, 'Back'); - $form->addSubmit(self::NEXT_SUBMIT_NAME, 'Next'); - - return $form; - } - - protected function createStep3(): Form - { - $form = new Form(); - - $form->addText('email', 'Email') - ->setRequired(); - - $form->addSubmit(self::PREV_SUBMIT_NAME, 'Back'); - $form->addSubmit(self::FINISH_SUBMIT_NAME, 'Register'); - - return $form; - } -} - -$requestFactory = new RequestFactory(); -$httpRequest = $requestFactory->fromGlobals(); -$httpResponse = new Response(); - -$session = new Session($httpRequest, $httpResponse); -$session->start(); - -$facade = new Facade(new Wizard($session)); -$facade->attached(); - -if ($facade->isSuccess()) { - var_dump($facade->getValues()); - - echo "Wizard success\n"; -} elseif ($facade->getCurrentStep() === 1) { - // first step - echo $facade->renderCurrentComponent(); -} else { - // other steps - echo $facade->renderCurrentComponent(); -}