Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed May 23, 2017
1 parent a29b16c commit 4a8d7b4
Show file tree
Hide file tree
Showing 9 changed files with 602 additions and 197 deletions.
28 changes: 26 additions & 2 deletions Classes/Condition/Processor/ConditionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Romm\Formz\Form\Definition\Condition\ActivationInterface;
use Romm\Formz\Form\Definition\Field\Field;
use Romm\Formz\Form\Definition\Field\Validation\Validator;
use Romm\Formz\Form\Definition\Step\Step\ConditionalStepDefinition;
use Romm\Formz\Form\Definition\Step\Step\Substep\ConditionalSubstepDefinition;
use Romm\Formz\Form\FormObject\FormObject;

Expand All @@ -41,6 +42,11 @@ class ConditionProcessor
*/
private $conditionTrees = [];

/**
* @var ConditionTree[]
*/
private $stepTree = [];

/**
* @var ConditionTree[]
*/
Expand Down Expand Up @@ -108,8 +114,26 @@ public function getActivationConditionTreeForValidator(Validator $validator)
}

/**
* Returns the condition tree for a given validator instance, giving access
* to CSS, JavaScript and PHP transpiled results.
* @todo
*
* @param ConditionalStepDefinition $step
* @return ConditionTree
*/
public function getActivationConditionTreeForStep(ConditionalStepDefinition $step)
{
$key = 'step-' . serialize($step);

if (false === array_key_exists($key, $this->stepTree)) {
$this->stepTree[$key] = $this->getConditionTree($step->getActivation());
}

$this->stepTree[$key]->injectDependencies($this, $step->getActivation());

return $this->stepTree[$key];
}

/**
* @todo
*
* @param ConditionalSubstepDefinition $substep
* @return ConditionTree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,22 @@

namespace Romm\Formz\Form\Definition\Step\Step;

use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent;
use Romm\Formz\Form\Definition\Condition\Activation;

class NextSteps extends AbstractFormDefinitionComponent
class ConditionalStepDefinition extends StepDefinition
{
/**
* @var \Romm\Formz\Form\Definition\Step\Step\StepDefinition
* @var \Romm\Formz\Form\Definition\Condition\Activation
* @validate NotEmpty
* @validate Romm.Formz:Internal\ConditionIsValid
*/
protected $default;
protected $activation;

/**
* @return StepDefinition
* @return Activation
*/
public function getDefaultStep()
public function getActivation()
{
return $this->default;
}

/**
* @return StepDefinition[]
*/
public function getAllSteps()
{
return [$this->getDefaultStep()];
return $this->activation;
}
}
118 changes: 112 additions & 6 deletions Classes/Form/Definition/Step/Step/StepDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ class StepDefinition extends AbstractFormDefinitionComponent
protected $step;

/**
* @var \Romm\Formz\Form\Definition\Step\Step\NextSteps
* @var \Romm\Formz\Form\Definition\Step\Step\StepDefinition
*/
protected $next;

/**
* @var \Romm\Formz\Form\Definition\Step\Step\ConditionalStepDefinition[]
*/
protected $detour;

/**
* @var \Romm\Formz\Form\Definition\Step\Step\ConditionalStepDefinition[]
*/
protected $divergence;

/**
* @return Step
*/
Expand All @@ -46,21 +56,58 @@ function (Steps $steps) {
);
}

public function getStepLevel()
{
$level = 1;

if ($this->hasPreviousDefinition()) {
$this->getPreviousDefinition()->alongParents(
function ($parent) use (&$level) {
if ($parent instanceof self) {
$level += $parent->getStepWeight();
} elseif ($parent instanceof Steps) {
return false;
}

return true;
}
);
}

return $level;
}

public function getStepWeight()
{
$weight = 1;
$childWeight = $this->hasNextStep() ? 1 : 0;

if ($this->hasDetour()) {
foreach ($this->getDetourSteps() as $detourStep) {
$childWeight = max($childWeight, $detourStep->getStepWeight());
}
}

return $weight + $childWeight;
}

/**
* @return bool
*/
public function hasNextSteps()
public function hasNextStep()
{
return null !== $this->next;
return null !== $this->next
|| false === empty($this->detour)
|| false === empty($this->divergence);
}

/**
* @return NextSteps
* @return StepDefinition
* @throws EntryNotFoundException
*/
public function getNextSteps()
public function getNextStep()
{
if (false === $this->hasNextSteps()) {
if (false === $this->hasNextStep()) {
throw EntryNotFoundException::nextStepsNotFound($this);
}

Expand Down Expand Up @@ -90,4 +137,63 @@ public function getPreviousDefinition()

return $previousStepDefinition;
}

/**
* @return ConditionalStepDefinition[]
*/
public function getDetourSteps()
{
return $this->detour;
}

/**
* @return bool
*/
public function hasDetour()
{
return false === empty($this->detour);
}

/**
* @return bool
*/
public function isInDetour()
{
return $this instanceof ConditionalStepDefinition
|| $this->hasParent(ConditionalStepDefinition::class);
}

public function getDetourRootStep()
{
if (false === $this->isInDetour()) {
throw new \Exception('todo'); // @todo
}

$stepDefinition = $this;

while ($stepDefinition->hasParent(ConditionalStepDefinition::class)) {
$stepDefinition = $stepDefinition->getFirstParent(ConditionalStepDefinition::class);
}

/** @var StepDefinition $stepDefinition */
$stepDefinition = $stepDefinition->getFirstParent(StepDefinition::class);

return $stepDefinition->getNextStep();
}

/**
* @return ConditionalStepDefinition[]
*/
public function getDivergenceSteps()
{
return $this->divergence;
}

/**
* @return bool
*/
public function hasDivergence()
{
return false === empty($this->divergence);
}
}
92 changes: 79 additions & 13 deletions Classes/Form/FormObject/Service/Step/FormStepPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Romm\Formz\Form\FormObject\Service\Step;

use Romm\Formz\Form\Definition\Step\Step\Step;
use Romm\Formz\Form\Definition\Step\Step\StepDefinition;

/**
* This object is stored in a form metadata, and contains important information
Expand All @@ -38,6 +39,11 @@ class FormStepPersistence
*/
protected $validatedSteps = [];

/**
* @var array
*/
protected $stepLevels = [];

/**
* @var array
*/
Expand All @@ -57,11 +63,13 @@ public function __construct($configurationHash)
}

/**
* @param Step $step
* @param StepDefinition $stepDefinition
*/
public function markStepAsValidated(Step $step)
public function markStepAsValidated(StepDefinition $stepDefinition)
{
$this->validatedSteps[] = $step->getIdentifier();
$identifier = $stepDefinition->getStep()->getIdentifier();
$this->validatedSteps[$identifier] = $identifier;
$this->stepLevels[$stepDefinition->getStepLevel()] = $stepDefinition->getStep()->getIdentifier();
}

/**
Expand All @@ -74,30 +82,79 @@ public function stepWasValidated(Step $step)
}

/**
* @param Step $step
* @param array $formValues
* @param StepDefinition $stepDefinition
*/
public function addStepFormValues(Step $step, array $formValues)
public function setStepLevel(StepDefinition $stepDefinition)
{
$this->stepsFormValues[$step->getIdentifier()] = $formValues;
$this->stepLevels[$stepDefinition->getStepLevel()] = $stepDefinition->getStep()->getIdentifier();
}

/**
* @param Step $step
* @param int $level
* @return bool
*/
public function hasStepFormValues(Step $step)
public function hasStepIdentifierAtLevel($level)
{
return true === array_key_exists($step->getIdentifier(), $this->stepsFormValues);
return isset($this->stepLevels[$level]);
}

/**
* @param Step $step
* @param int $level
* @return string
*/
public function getStepIdentifierAtLevel($level)
{
if (false === $this->hasStepIdentifierAtLevel($level)) {
throw new \Exception('todo'); // @todo
}

return $this->stepLevels[$level];
}

/**
* @param StepDefinition $stepDefinition
* @param array $formValues
*/
public function addStepFormValues(StepDefinition $stepDefinition, array $formValues)
{
$this->stepsFormValues[$stepDefinition->getStep()->getIdentifier()] = $formValues;
}

/**
* @param StepDefinition $stepDefinition
* @return bool
*/
public function hasStepFormValues(StepDefinition $stepDefinition)
{
return true === array_key_exists($stepDefinition->getStep()->getIdentifier(), $this->stepsFormValues);
}

/**
* @param StepDefinition $stepDefinition
* @return array
*/
public function getStepFormValues(Step $step)
public function getStepFormValues(StepDefinition $stepDefinition)
{
return $this->stepsFormValues[$step->getIdentifier()];
if (false === $this->hasStepFormValues($stepDefinition)) {
throw new \Exception('todo'); // @todo
}

return $this->stepsFormValues[$stepDefinition->getStep()->getIdentifier()];
}

/**
* @return array
*/
public function getMergedFormValues()
{
$formValues = [];

foreach ($this->stepsFormValues as $stepFormValues) {
unset($stepFormValues['__identity']);
$formValues = array_merge($formValues, $stepFormValues);
}

return $formValues;
}

/**
Expand Down Expand Up @@ -131,6 +188,15 @@ public function getObjectHash()
public function refreshObjectHash($hash)
{
$this->objectHash = $hash;
$this->resetValidationData();
}

/**
* @todo
*/
public function resetValidationData()
{
$this->validatedSteps = [];
$this->stepLevels = [];
}
}

0 comments on commit 4a8d7b4

Please sign in to comment.