Skip to content

Commit

Permalink
Merge pull request #66 from aeviiq/add-unhandled-form-status
Browse files Browse the repository at this point in the history
Added the unhandled form status and only allow multiple backwards tra…
  • Loading branch information
aeviiq authored Jul 15, 2021
2 parents 15b0a25 + 693466e commit 225e228
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src/Enum/Transition/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final class Status extends AbstractFlag

public const INVALID_FORM = 64;

public const UNHANDLED_FORM = 128;

/**
* @throws InvalidArgumentException When the given value contains an invalid flag combination.
*/
Expand Down Expand Up @@ -50,6 +52,13 @@ public function __construct(int $value)
throw new InvalidArgumentException('A transition status can not be valid form and invalid form at the same time.');
}

if ($this->isFlagSet($value, self::VALID_FORM) && $this->isFlagSet($value, self::UNHANDLED_FORM)) {
throw new InvalidArgumentException('A transition status can not be valid form and unhandled form at the same time.');
}

if ($this->isFlagSet($value, self::INVALID_FORM) && $this->isFlagSet($value, self::UNHANDLED_FORM)) {
throw new InvalidArgumentException('A transition status can not be invalid form and unhandled form at the same time.');
}

parent::__construct($value);
}
Expand Down Expand Up @@ -83,4 +92,9 @@ public function isFormValid(): bool
{
return $this->contains(new self(self::VALID_FORM));
}

public function isUnhandledForm(): bool
{
return $this->contains(new self(self::UNHANDLED_FORM));
}
}
15 changes: 10 additions & 5 deletions src/Transitioner.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ public function transition(FormFlowInterface $flow): Status
throw new TransitionException($flow, \sprintf('"%s" is an invalid requested step number in the current context.', $requestedStepNumber));
}

$handleForm = true;
while ($flow->getCurrentStepNumber() > $requestedStepNumber) {
$status = $this->backwards($flow);
$status = $this->backwards($flow, $handleForm);
$handleForm = false;
if (!$status->isSuccessful()) {
break;
}
Expand Down Expand Up @@ -148,16 +150,19 @@ public function forwards(FormFlowInterface $flow): Status
/**
* {@inheritDoc}
*/
public function backwards(FormFlowInterface $flow): Status
public function backwards(FormFlowInterface $flow, bool $handleForm = true): Status
{
$currentStep = $flow->getCurrentStep();
if ($flow->getCurrentStep() === $flow->getFirstStep()) {
throw new TransitionException($flow, 'The flow is on the first step and can not transition backwards.');
}

$form = $flow->getCurrentStepForm();
$this->submitForm($form);
$status = !$form->isSubmitted() || !$form->isValid() ? Status::INVALID_FORM : Status::VALID_FORM;
$status = Status::UNHANDLED_FORM;
if ($handleForm) {
$form = $flow->getCurrentStepForm();
$this->submitForm($form);
$status = !$form->isSubmitted() || !$form->isValid() ? Status::INVALID_FORM : Status::VALID_FORM;
}

$currentStepNumber = $currentStep->getNumber();
$event = new TransitionEvent($flow);
Expand Down
2 changes: 1 addition & 1 deletion src/TransitionerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function forwards(FormFlowInterface $flow): Status;
*
* @throws TransitionException When the current step is the first step.
*/
public function backwards(FormFlowInterface $flow): Status;
public function backwards(FormFlowInterface $flow, bool $handleForm = true): Status;

/**
* Attempts to complete the given flow.
Expand Down
8 changes: 8 additions & 0 deletions tests/Enum/Transition/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function invalidStatusCombinationProvider(): array
Status::VALID_FORM | Status::INVALID_FORM,
'A transition status can not be valid form and invalid form at the same time.',
],
'valid_form and unhandled_form' => [
Status::VALID_FORM | Status::UNHANDLED_FORM,
'A transition status can not be valid form and unhandled form at the same time.',
],
'invalid_form and unhandled_form' => [
Status::INVALID_FORM | Status::UNHANDLED_FORM,
'A transition status can not be invalid form and unhandled form at the same time.',
],
];
}

Expand Down

0 comments on commit 225e228

Please sign in to comment.